简体   繁体   English

C-Malloc或calloc…以及如何?

[英]C - Malloc or calloc…and how?

i have a text file where the first number defines the size of the arrays. 我有一个文本文件,其中第一个数字定义了数组的大小。 I know that calloc or malloc can reserve memory, but how? 我知道callocmalloc可以保留内存,但是如何呢?

this code: 这段代码:

typedef struct alpha {
    int* size;
    char name;
    int  tot;
    char line[60];
} ALPHA;

fgets(line, 60, fp);
tot = atoi(line);
size = (int*)calloc(name, sizeof(int);

Imagine that in the first line of text is the number 10, with this code the size of name will be 10? 想象一下,在文本的第一行中,数字为10,使用此代码,名称的大小将为10? like name[10]??? 喜欢名字[10] ???

Use one of the following: 使用以下之一:

ALPHA* alphas1 = calloc(tot, sizeof(ALPHA));
// or
ALPHA* alphas2 = malloc(tot * sizeof(ALPHA));

Those allocate memory for tot of your ALPHA structures. 那些分配内存tot您的ALPHA结构。

There's several problems with your code. 您的代码有几个问题。

First, you seem to be confusing a declaration of what a data type is with actually having a variable. 首先,您似乎在将数据类型的声明与实际拥有变量混淆了。 You declare a struct , and from then on it's a data type, much like int or double . 您声明一个struct ,从那时起它就是一个数据类型,很像intdouble Before you assign anything to one, you need to have one. 将任何东西分配给一个之前,您需要拥有一个。 You can get one either by defining one in the function ( ALPHA a; ) or by allocating memory for one with malloc() or calloc() . 您可以通过在函数中定义一个( ALPHA a; )或通过使用malloc()calloc()为一个分配内存来获得一个。

To use calloc() , you have two arguments, one being how many whatevers you want, and one being the size of a whatever. 要使用calloc() ,您有两个参数,一个是您想要多少个参数,另一个是任意值的大小。 For malloc() , you multiply the two. 对于malloc() ,请将两者相乘。 The other difference is that malloc() returns memory with whatever used to be in it, while calloc() initializes everything to zero. 另一个区别是malloc()返回的内存是以前的值,而calloc()则将所有内容初始化为零。 (That's zero-length string or integral zeros according to the Standard. Other values are not guaranteed, but with most modern systems you'll get the equivalent of a zero.) These functions return a pointer to the memory allocated. (根据标准,该长度为零长度的字符串或整数零。不保证其他值,但是在大多数现代系统中,您将获得与零相等的值。)这些函数返回指向分配的内存的指针。

You seem to want tot int s, so (using calloc() ), the correct statement is something like int * a = calloc(tot, sizeof(int)); 你似乎想tot int S,SO(使用calloc()正确的说法是一样的东西int * a = calloc(tot, sizeof(int)); , or int * a = calloc(tot, sizeof(*a)); int * a = calloc(tot, sizeof(*a)); . No cast is required in C (it is required in C++, but you usually wouldn't use malloc() or calloc() in C++), and the only thing it can do is cover up a possible mistake (leaving out #include <stdlib.h> to be specific). 在C中不需要calloc()在C ++中是必需的,但是在C ++中通常不会使用malloc()calloc() ),并且它唯一能做的就是掩盖可能的错误( #include <stdlib.h> )。

Once you have that, you can refer to the int s as something like a[3] . 一旦有了,就可以将int称为a[3]

Putting the result in a field of an ALPHA is doable, but you really do need an ALPHA , so something like 将结果放在ALPHA的字段中是可行的,但是您确实确实需要ALPHA ,所以类似

ALPHA a;
a.size = calloc(tot, sizeof(*a));

will work. 将工作。 You would therefore refer to it as a.size[3] , for example. 因此,例如,您将其称为a.size[3]

Also, I don't see what name is doing. 另外,我看不出name在做什么。 It's one character, which is not enough for any non-empty string, and I don't know why you've got it in the calloc() call. 这是一个字符,不足以容纳任何非空字符串,而且我不知道您为什么在calloc()调用中找到它。 You might want name to be a dynamically allocated string, with size being its size. 您可能希望name是动态分配的字符串, size是其大小。 In that case, you'd need to change the lines in the declaration of ALPHA to be 在这种情况下,您需要将ALPHA声明中的行更改为

int size;
char * name;

and the code might be be 和代码可能是

ALPHA a;
fgets(line, 60, fp);
a.size = atoi(line);
a.name = calloc(a.size, sizeof(*a.name));

Once you've done that, after entering 10, you can refer to a.name[0] through a.name[9] , and that's your ten characters. 完成此操作后,输入10后,您可以通过a.name[9]引用a.name[0] a.name[9] ,这就是您的十个字符。 a.name[10] would be one past the end. a.name[10]将是末尾的一个。 Note that you can only put a nine-character string in a ten-character array, since you need to have room for the null terminator (the '\\0' which is the last character of any C-style string). 请注意,您只能在一个10个字符的数组中放置一个9个字符的字符串,因为您需要为null终止符( '\\0' ,这是任何C样式字符串的最后一个字符)留出空间。 If you want to be able to enter the specified number of characters, you'd want to add 1 to a.size after putting the user-entered number in. 如果要输入指定的字符数,则在输入用户输入的数字后,要在a.size添加1。

You might just want to 您可能只想

ALPHA alphas[tot]; ALPHA alphas [tot];

ALPHA *alphas1 = calloc(tot, sizeof(*alphas1));
ALPHA *alphas2 = malloc(tot * sizeof(*alphas2));

is a slightly better version of: 是以下版本的略好版本:

ALPHA* alphas1 = calloc(tot, sizeof(ALPHA));
ALPHA* alphas2 = malloc(tot * sizeof(ALPHA));

Now you can access alphas1 and so on, "array-like": 现在,您可以访问alphas1,以此类推,类似于“数组”:

alphas1[0], alphas1[1], alphas1[2], ..., alphas1[tot] 
alphas2[0], alphas2[1], alphas2[2], ..., alphas2[tot] 

alphas1[...] is of type ALPHA alphas1[...]的类型为ALPHA
alphas2[...] is of type ALPHA . alphas2[...]的类型为ALPHA

PS: To make your code more reliable, don't forget to check if a malloc / calloc failed. PS:为了使您的代码更可靠,请不要忘记检查malloc / calloc是否失败。 To this verify that (in your case) check if alphas1 or alphas2 are different from NULL . 为此,请验证(在您的情况下)检查alphas1alphas2是否不同于NULL In case they are NULL , you won't be able to access them as presented, and you should design a mechanism to recover from this, or simply exit the program (+error message). 如果它们是NULL ,则您将无法按所示访问它们,并且应该设计一种机制以从中恢复,或者简单地退出程序(+错误消息)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM