Memory allocation in char * C language

This is the correct way to allocate memory on char *.

char* sides ="5"; char* tempSides; tempSides = (char*)malloc(strlen(inSides) * sizeof(char)); 
+7
c memory-management malloc
source share
7 answers

Nearly. Lines are terminated in NULL, so you probably want to allocate extra bytes to hold NULL bytes. That is, although sides has a length of 1 character, it really is 2 bytes: { 5 , '\0' }.

So this will be:

 tempSides = (char *)malloc((strlen(sides)+1)*sizeof(char)); 

and if you want to copy it to:

 strcpy(tempSides, sides); 
+14
source share

Note that:

  • Lines have zero termination (\ 0), and strlen () does not take it into account;
  • By definition, sizeof (char) is 1 (bytes), so it is not required;
  • If you use the C compiler (not C ++), there is no need to cast it to char * ;

So this will be:

 char *tempSides = malloc(strlen(inSides) + 1); 

However, if you want to duplicate the contents of inSides , you can use strdup , for example:

 char *tempSides = strdup(inSides); if (tempSides != NULL) { // do whatever you want... free(tempSides); } 
+9
source share

As already noted, you missed the space allocation for the trailing NUL character. But I also wanted to point out a few other things that can make your code more concise.

By definition, sizeof(char) always 1, so you can shorten your selection line to:

 tempSides = (char*)malloc(strlen(inSides) + 1); 

Another thing is that it looks like what you are doing to duplicate a row. There is a built-in function that does this for you:

 tempSides = strdup(inSides); 

This allows you to get the length by highlighting the correct number of bytes and copying the data.

+3
source share

There is a problem with this. tempSides will point to an uninitialized memory block of size 1. If you are going to copy the line of sides to tempSides, you will need to set the size one byte longer to hold the zero delimiter for the line. The value returned by strlen () does not include the null delimiter at the end of the line.

+1
source share

No, not at all. As others have already noted, you need to allocate space for the NUL terminator.

In addition, you should usually not discard returns from malloc . It may hide the error when you forget the #include correct header. Multiplying by sizeof(char) also pointless, since standards (both C and C ++) define sizeof(char) always equal to 1.

Finally, every call to malloc should include a result check. I would include all of this in a function:

 char *dupe_string(char const *string) { char *temp; if (NULL!=(temp=malloc(strlen(string)+1))) strcpy(temp, string); return temp; } 
+1
source share

Multiplying the number of elements by sizeof(char) is a matter of personal preference, since sizeof(char) always 1. However, if you do this for consistency, it is better to use the type of the recipient pointer to determine the size of the element, enter it explicitly instead. And don't say the result malloc

 tempSides = malloc(strlen(inSides) * sizeof *tempSides); 

Of course, when working with null strings, you must remember to allocate extra space for the null termination character. It is impossible to say whether you intend to make tempSides zero-terminated string in this case, so I cannot say if you need it.

0
source share

The correct way to allocate dynamic memory on tempSides shown below:

 char* sides ="5"; char* tempSides; tempSides = (char*)malloc((strlen(sides) + 1) * sizeof(char)); 

char* stores string data similar to char[] . The null (\0) lines are complete. Therefore, to store null characters, an additional one byte must be allocated.

A dynamically allocated block of memory should be freed using free() after completion of use. If not freed, a memory leak will occur.

 free(tempSides); 

One freed memory, null must be assigned so that it is not a dangling pointer.

 tempSides = NULL; 
0
source share

All Articles