使用calloc()设置char数组,还可以“释放"代码.完成后的数组 [英] Using calloc() to set up char array, also "freeing" array when done

查看:204
本文介绍了使用calloc()设置char数组,还可以“释放"代码.完成后的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个字符串数组(使用Linux在C语言中).该数组将容纳11个字符串(静态长度).我最初将数组设置为:

  char答案[10] [100]; 

但是在我的代码中,我有一部分调用了fgets(input,sizeof(input),stdin).当调用此fgets()部分时,我的Answers数组的最后一个元素被input的值覆盖(关于Answers在堆栈中的位置吗?).因此,现在我试图锁定"我用于Answers数组的内存.我会用

  char答案= calloc(11 * sizeof(char)); 

OR

通过循环运行它-

  char答案[10];for(c = 0; c< = 10; c ++){答案[c] = calloc(1 * sizeof(char);} 

此外,我需要在完成后使用atexit()释放已分配的内存...这样做的最佳方法是什么,因为我无法在atexit()内部传递参数?

atexit(免费);

  void Free(){免费(答案);} 

提前谢谢!

解决方案

好吧,我想很多误解.那里的大部分代码都不正确.您要求输入11个字符串,所以 char Answers [10] [100]; 不正确,您应该键入 char Answers [11] [100]; ,这就是原因为什么跳过输入.关于错误...首先- calloc()具有两个参数,没有一个参数,例如 malloc(),如以下签名所示:

  void * calloc(size_t nmemb,size_t size); 

void * 返回到已分配的内存区域,第一个参数是您要分配的元素数,第二个参数是每个元素的大小.其次,如上所述,它返回一个POINTER,一个无效的POINTER,因此您无法正确执行这段代码:

  char答案[10];for(c = 0; c< = 10; c ++){答案[c] = calloc(11 * sizeof(char));} 

出什么问题了?首先,参数,如前所述.但是第二个事实是您制作了一个char数组,而不是所需的CHAR POINTERS.应该是这样的:

  char *答案[11];//您要求的11个元素for(c = 0; c< = 10; c ++){答案[c] =(char *)calloc(1,MY_STR_LENGTH);} 

如示例所示,当MY_STR_LENGTH为100的常数时.我们使用了对void指针的强制转换,更正了 calloc 的使用和char指针的声明.现在,此代码是正确的-另外,为什么还要使用 calloc ?通常不需要在字符串中这样做.顺便说一句,无论如何它不需要一行功能.声明此内容的第二种方法是这样的:

  char **答案= calloc(11,sizeof(char *));for(i = 0; i< 11; i ++){答案[i] = calloc(1,sizeof(char)* MY_STRING_LENGTH);//基本上是MY_STRING_LENGTH,因为sizeof(char)几乎总是一个,但仅出于代码的可读性而存在.} 

就是这样,希望您能理解.在此处阅读有关C语言中内存分配的更多信息:

关于 malloc() 关于 calloc() 关于 realloc()

I'm trying to set up an array of strings (in C, using Linux). The array will hold 11 strings (static length). I initially had the array set up as:

char Answers[10][100];

but in my code I have a portion that calls fgets(input,sizeof(input),stdin). When this fgets() portion is called, the final element of my Answers array was being overwritten with the value of input (something about Answers' location on the stack?). So now I'm trying to "lock-in" the memory I use for my Answers array. Would I use

char Answers=calloc(11*sizeof(char));

OR

Run it through a loop--

char Answers[10];
for(c=0;c<=10;c++)
{
    Answers[c]=calloc(1*sizeof(char);
}

Also, I'll need to use atexit() to free the allocated memory when I'm done... What would be the best way to do this since I can't pass arguments inside atexit()?

atexit(Free);

void Free()
{
    free(Answers);
}

Thanks in advance!

解决方案

Okay, lots of misunderstandings, I guess. Most pieces of code there are incorrect. You asked for 11 strings so char Answers[10][100]; is incorrect and you should type char Answers[11][100]; instead, that was the reason why it skipped input. About the mistakes... First - calloc() has TWO parameters and not one, like malloc(), as in the following signature:

void *calloc(size_t nmemb, size_t size);

returns a void* to the area of memory allocated, first parameter is the number of elements that you'd like to allocate and second is the size of each element. Second, as typed above, it returns a POINTER, a void one, so you can't perform this piece of code correctly:

char Answers[10];
for(c=0;c<=10;c++)
{
    Answers[c] = calloc(11*sizeof(char));
}

What's the problem ? FIRST, parameters, as said. But second is the fact you made an array of chars and not CHAR POINTERS as needed. Should be this way:

char* Answers[11]; //As you requested 11 elements
for(c=0;c<=10;c++)
{
    Answers[c] = (char*) calloc(1, MY_STR_LENGTH);
}

When MY_STR_LENGTH is a constant of 100, as shown in your example. We used casting upon the void pointer, we corrected the use of calloc and the declaration of the char pointers. Now this code is correct - also, why do you use calloc? Usually there's no need to do so in a string. By the way, no need for a function when it's one line anyway. The second way to declare this is this way:

char **Answers = calloc(11, sizeof(char*));
for(i = 0 ; i < 11 ; i++)
{
   Answers[i] = calloc(1, sizeof(char)*MY_STRING_LENGTH); //Basically it is MY_STRING_LENGTH as sizeof(char) is almost always one, but it's there only for the readability of the code.
}

This is it, hope you understand. Read more about memory allocation in C here:

About malloc() About calloc() About realloc()

这篇关于使用calloc()设置char数组,还可以“释放"代码.完成后的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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