Is this a bad practice? Redirection pointer pointing to an empty line to another line?

Consider this fragment,

char *p1="";
printf("p1=%p\np1=%s\n",p1,p1);

char s1[6]="abcde";
printf("s1=%p\ns1=%s\n",s1,s1);

p1=s1;

printf("p1=%p\np1=%s\n",p1,p1);

Since none of the variables are allocated on the heap, all allocated space will be freed at the end of the process.

Questions:

  • What happens to an empty string when a pointer pointing to it is redirected to another string? (According to my basic knowledge of Java, if you use Java, an empty string will have the right to collect GC. What is happening here?)
  • If the code was longer, would an empty string use memory shortly before the end?
  • Valgrind does not report a memory leak (I think this applies only to heap memory?). Is there a scenario where this coding practice causes a bottleneck?
+4
3

"" "abcde" ( ) . .

, . .

(, !). .

, , , . .

+8

"" , . , , .

, .rodata ( ) . .rodata . .rodata - .

" ". , , .

, . const char*, .

+2

p1 ; "" 1 '\0',
s1 6 ,
"abcde" 6 .

. .

, , . ( , "%p" void*).

0

Source: https://habr.com/ru/post/1541389/


All Articles