分配内存和保存字符串在C [英] Allocate memory and save string in c

查看:175
本文介绍了分配内存和保存字符串在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么下面的code isnt't工作

  INT主(INT ARGC,字符** argv的)
{
     字符*测试=(字符*)malloc的(12 * sizeof的(炭));
     测试=testingonly;
     自由(试验);
}

考虑这件事之后,我的假设是我第一次分配空间,在内存中12个字符,但下一行的分配在栈上创建一个字符数组和该内存地址传递给测试。因此,免费的()尝试这是不允许在堆栈上释放的空间。这是否正确?

那么,什么是正确的做法,以节省堆一个字符串?是一种常见的方式下?

  INT主(INT ARGC,字符** argv的)
{
     字符*测试=(字符*)malloc的(12 * sizeof的(炭));
     的strcpy(考testingonly);
     自由(试验);
}


解决方案

 的char *测试=(字符*)malloc的(12 * sizeof的(炭));        +  -  +  -  +  -  +  -  +  -  +  -  +  -  +  -  +  -  +  -  +  -  +  -  +
测试---> | X | X | X | X | X | X | X | X | X | X | X | X | (未初始化的内存,堆)
        + - + - + - + - + - + - + - + - + - + - + - + - +测试=testingonly;        + - + - + - + - + - + - + - + - + - + - + - + - +
测试+ | X | X | X | X | X | X | X | X | X | X | X | X |
     | + - + - + - + - + - + - + - + - + - + - + - + - +
     | + - + - + - + - + - + - + - + - + - + - + - + - +
     + - > | T | E | S | T | I | N | G |Ø| N | L | Y | 0 |
        + - + - + - + - + - + - + - + - + - + - + - + - +自由(试验); //错误,因为测试不再指向分配的空间。

而不是改变指针测试,需要testingonly到分配的场所使用复制的字符串例如的strcpy 或使用的strdup 。请注意,如的malloc 的strdup 收益 NULL 如果没有足够的功能存储器是可用的,并且因此应该被检查。

 的char *测试=(字符*)malloc的(12 * sizeof的(炭));
的strcpy(考testingonly);        + - + - + - + - + - + - + - + - + - + - + - + - +
测试---> | T | E | S | T | I | N | G |Ø| N | L | Y | 0 |
        + - + - + - + - + - + - + - + - + - + - + - + - +

 的char *测试=的strdup(testingonly);        +  -  +  -  +  -  +  -  +  -  +  -  +  -  +  -  +  -  +  -  +  -  +  -  +
测试---> | T | E | S | T | I | N | G |Ø| N | L | Y | 0 |
        + - + - + - + - + - + - + - + - + - + - + - + - +

I was wondering why the following code isnt't working

int main(int argc, char **argv)
{
     char *test = (char*) malloc(12*sizeof(char));
     test = "testingonly";
     free(test);
}

After thinking about it my assumption was that first i allocate space for 12 chars in memory but the assignment in the next line creates a char array on the stack and the memory address of that is passed to test. So free() tries to release space on the stack which is not allowed. Is that correct?

So what would be the correct approach to save a string on the heap? Is the following a common way?

int main(int argc, char **argv)
{
     char *test = (char*) malloc(12*sizeof(char));
     strcpy(test, "testingonly");
     free(test);
}

解决方案

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|
        +-+-+-+-+-+-+-+-+-+-+-+-+

这篇关于分配内存和保存字符串在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆