Allocate memory and save string in c

空扰寡人 提交于 2019-11-30 04:44:33
Anders
char *test = (char*) malloc(12*sizeof(char));

        +-+-+-+-+-+-+-+-+-+-+-+-+
test--->|x|x|x|x|x|x|x|x|x|x|x|x|   (uninitialized memory, heap)
        +-+-+-+-+-+-+-+-+-+-+-+-+

test = "testingonly";

        +-+-+-+-+-+-+-+-+-+-+-+-+
test +  |x|x|x|x|x|x|x|x|x|x|x|x|
     |  +-+-+-+-+-+-+-+-+-+-+-+-+
     |  +-+-+-+-+-+-+-+-+-+-+-+-+
     +->|t|e|s|t|i|n|g|o|n|l|y|0|  
        +-+-+-+-+-+-+-+-+-+-+-+-+

free(test); // error, because test is no longer pointing to allocated space.

Instead of changing the pointer test, you need to copy the string "testingonly" into the allocated place using e.g. strcpy or use strdup. Note that functions like malloc and strdup return NULL if insufficient memory is available, and thus should be checked.

char *test = (char*) malloc(12*sizeof(char));
strcpy(test, "testingonly");

        +-+-+-+-+-+-+-+-+-+-+-+-+
test--->|t|e|s|t|i|n|g|o|n|l|y|0|
        +-+-+-+-+-+-+-+-+-+-+-+-+

or

char *test = strdup("testingonly");

        +-+-+-+-+-+-+-+-+-+-+-+-+
test--->|t|e|s|t|i|n|g|o|n|l|y|0|
        +-+-+-+-+-+-+-+-+-+-+-+-+

You answered your question already. Essentially , strcpy is the appropriate way of copying string.

The first version doesn't create a string on the stack, but you're correct that you're not allowed to free it after the assignment. String literals are usually stored in constant/read-only sections of memory. The assignment doesn't copy anything, just makes test point to that area of memory. You cannot free it. You also cannot modify that string.

Your second piece of code is correct and usual. You might want to also look into strdup if your implementation has that.

Well you are correct. Now lets examine first piece of code.

char *test = (char*) malloc(12*sizeof(char));

Above code is no issues.

test = "testingonly";

Here you modified the pointer test leading to memory leak. And when you try to free you are not freeing actual allocated pointer but one "testingonly" literal pointing to. Literal points to constant memory which cannot be overridden in usual scenarios.

Now about second piece of code, this will work fine as you explicitly copied data from where literal is residing to heap where your test is pointing.

To your second point yes strcpy is a usual way. Other ways are 'memcpy' if you are copying raw bytes.

NOTE: Literals are not stored on stack. But you cannot modify location where literals are stored.

the code

#include <stdio.h>
int main(int argc, char **argv)
{
     char *test = (char*) malloc(12*sizeof(char));
     strcpy(test, "testingonly");
     printf("string is: %s\n",test);
     free(test);
     return 0;
}

will work

Kavita Jain

This is for allocating the memory:

char *string;
string = (char *) malloc(15);

This is for saving the data:

strcpy(str, "kavitajain");
printf("String = %s,  Address = %u\n", str, str);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!