Home > Back-end >  Am I using malloc in a wrong way?
Am I using malloc in a wrong way?

Time:09-02

I used malloc to give some space to pointer. First I wrote a line like below to give 4byte space for one integer.

int *minus = malloc(4);    // minus: 0x6a1590

and I used vscode debugging to see if it's done well. result showed like "minus: 0x6a1590".

After that I gave char pointer a space.

char *str = malloc(2);    // str : 0x6a15d0 "\300>������������������������������"

I just gave str a 2byte space but there is lots of thing in str.

Why is this happening? I thought there should be two null character in str..

CodePudding user response:

Whatever tool you are looking at to view the contents of str is assuming that it points to a string. But it doesn't point to a string, it just points to the two bytes of memory that you allocated which have no particular contents. So the information you are seeing there is meaningless.

CodePudding user response:

Might be because strings are null-terminated in C, and there is no zero-byte at the end of your 2-byte string. So it shows you all the bytes up until the first zero.

Plus, malloc is not guaranteed/required to return a block of memory filled with zeroes.

CodePudding user response:

David Schwartz gave you the right answer. Your debugger is showing the garbage that it's finding in the memory location you are pointing to with the str pointer. Here are some tips to help you.

When you declare a char* (a pointer to a character) you are defining a string. In the c language strings are terminated with a '\0' character. This is called the NULL character.

Since you didn't initialize your string after allocating its memory, there is just random junk in the memory that was assigned to you. The debugger knows that your str variable is a char* so it start reading and printing out memory interpreting each byte according to the ASCII code. It's printing out characters (one for each byte) until it finds the NULL a character.

To get a better result, change your code to the following:

char *str = malloc(2); 
*str = ''; /* set the first byte to be an empty character */
*(str 1) = '\0'; /* set the second byte to be null */

This additional two lines initialize the two bytes you've allocated to make them an empty (blank) string.

I'm gonna post some videos soon about strings and dynamic memory allocation.

CodePudding user response:

When you ask the debugger to print an int *, it prints the value of the pointer.

When you ask the debugger to print a char *, it prints the value of the character and also attempts to interpret the memory it points to as a character string. It does this because a char * is very often used to point to the first (or current, in some sense) character of a character string.

The memory provided by malloc is not guaranteed to be set to any particular value, zero or otherwise. When you print the char *, the additional output you see is due to the debugger displaying the characters it finds in memory, up to the first null character it finds. There is no additional output from printing the int * simply because the debugger does not attempt to display the contents of the memory that the int * points to.

  •  Tags:  
  • c
  • Related