开发者

Allocating memory to char* C language

I开发者_JAVA技巧s it the correct way of allocating memory to a char*.

char* sides ="5";

char* tempSides;

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


Almost. Strings are NUL terminated, so you probably want to allocate an extra byte to store the NUL byte. That is, even though sides is 1 character long, it really is 2 bytes: {5,'\0'}.

So it would be:

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

and if you wanna copy it in:

strcpy(tempSides, sides);


Note that:

  1. Strings are zero-terminated (\0), and strlen() doesn't count it;
  2. By definition, sizeof(char) is 1 (byte), so it's not required;
  3. If you use a C (not C++) compiler, there's no need to cast it to char *;

So that would be:

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

Still, if you want to duplicate the contents of inSides, you can use strdup, e.g.:

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


As has been pointed out, you missed allocating space for the terminating NUL chararacter. But I also wanted to point out a couple of other things that can make your code more concise.

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

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

Another thing is that this looks like you are doing to duplicate the string. There is a built in function that does that for you:

tempSides = strdup(inSides);

This handles getting the length, allocating the correct number of bytes and copying the data.


There's a problem with that. tempSides will point to an uninitialized block of memory of size 1. If you intend to copy the sides string into tempSides, then you will need to allocate a size one byte longer, in order to hold the zero terminator for the string. The value returned by strlen() does not include the zero terminator at the end of the string.


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

In addition, you generally should not cast the return from malloc. It can cover up a bug where you've forgotten to #include the correct header. Multiplying by sizeof(char) is also pointless, since the standards (both C and C++) define sizeof(char) to always be 1.

Finally, every call to malloc should include a test of the result. I'd wrap the whole thing up into a function:

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


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

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

Of course, when working with zero-terminated strings you have to remember to allocate extra space for the terminating zero character. There's no way to say whether it is your intent to make tempSides a zero-terminated string in this case, so I can't say whether you need it.


The correct way of allocation dynamic memory to tempSides is as shown below:

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

char* stores a string data, similar to char[]. Strings are null (\0) terminated. So extra one byte should be allocated for null character storage.

Dynamically allocated memory block must be freed using free() after it's use is over. If not freed, memory leak would happen.

free(tempSides);

One the memory is freed, NULL must be assigned to prevent it from being a dangling pointer.

tempSides = NULL;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜