Mallock vs. character arrays of a certain size

First off, I'm pretty new to C, so I probably have a fundamental misunderstanding. This code, for example:

int main() { char ack[100]; char *bar; bar = malloc(100); strncpy(ack, "testing", 7); bar = "testing"; return 0; } 

ack when checking gdb looks like this:

 (gdb) p ack $1 = "testing\000\360WV\000\000\000\000\000\277\000\000\000\000\000\000 '\000'\000\220\ 005@ ",<repeats 13 times>, "\003\ 004@ ", '\000' <repeats 13 times> "\325,\ 005@ \000\000\000\000\000H\214\246\367\377\177\000\000\220\ 005@ ", '\000' <repeats 13 times>, "P\ 004@ \000\000\000\000\000\360\342\377\377"` 

It makes sense to me how I initialized ack . I did not quite understand why bar looks like this:

 (gdb) p bar $2 = 0x40066c "testing" 

I allocated the same amount of memory (as far as I know) as I did when I requested a space for ack , but malloc has no extra garbage. As far as I understand, malloc does not perform any data initialization or anything else, so I'm a bit confused. The reason this happened is the problem that I ran into strstr. Basically, when I read data from a file (fgets), into a char array with a specific strstr () size, you didn’t work (which I assumed due to extra garbage). Working with malloc'd pointer and memory worked fine. Anyway, I have a few specific questions.

  • Is the behavior of this malloc'd variable expected? Is there some kind of optimization here (I compiled gcc but didn't do any optimizations), or does gdb not show me everything? Should there be "garbage" associated with this variable?

  • Am I using malloc correctly? Should I initialize all the memory I requested? If so, how?

Thanks!

EDIT

Thanks to all who responded! I learned a lot of things from all of you, and it was very much appreciated. Now I see a problem with the above code and the original problem that I came across with fgets () and strstr ().

+4
source share
4 answers
 bar = "testing"; 

reassigns the bar pointer to point to a static buffer containing the string "testing" , i.e. no longer points to your malloc 'd array. This is a memory leak .

To get a string in the malloc'd buffer, use strcpy , strncpy or memcpy , as you did with ack .

+10
source

This is because you (inadvertently) throw out malloc ed memory when assigned

 bar = "testing"; 

This means that bar points to a string literal and no longer (memory leak) malloc ed.

+2
source

You are disabled for a couple of points.

Firstly, the reason ack has garbage after "testing" when printing is because you never terminate it NULL. In C all lines must have a NULL end, i.e. End with \0 . You can fix this here by calling strncpy() with a length of 8 ( strlen("testing") + 1) or just using strcpy ().

Secondly, bar looks different in gdb because it is a pointer to char , and ack is a ruler of char . gdb shows the pointer value for bar , but also gives you a favor, showing you what is on the other side of this pointer, if possible, as you can see. And when you do bar = "testing;" , you redirect that bar points to an array of characters containing "testing" in memory. When you use such string literals, it automatically has the \0 character at the end, so NULL terminates and therefore no garbage appears after that.

0
source

C strings have a null delimiter (the ascii character value is zero), so the string "test" is actually 8 bytes, including the terminator.

When you print the original value, it stops printing when it reaches the terminator.

But since you only copied the first 7 characters when you print the second value, it continues to print garbage until it reaches zero in the garbage data that were there before.

-1
source

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


All Articles