C - Malloc or calloc ... and how?

I have a text file where the first number determines the size of the arrays. I know what calloc

or malloc

can reserve memory, but how?

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 the first line of text contains the number 10, with this code the size of the name will be 10? what is the name [10] ???

+2


a source to share


4 answers


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

      

- a slightly better version:

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

      

You can now access alphas1, etc., "array-like":



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

      

alphas1[...]

has type ALPHA


alphas2[...]

has type ALPHA

.

PS: To make your code more robust, remember to check if malloc / calloc failed. To do this, make sure (in your case) check if alphas1

or is different alphas2

from NULL

. If they are NULL

, you will not be able to access them as shown and you must develop a mechanism to recover from this, or simply exit the program (+ error message).

0


a source


Use one of the following:

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

      



They allocate memory for tot

your structures ALPHA

.

+3


a source


There are several issues in your code.

First, you seem to be misleading to declare that the datatype actually has a variable. You declare struct

and then a data type like int

or double

. Before you can assign anything to one, you must have it. You can get it either by defining it in the ( ALPHA a;

) function , or by allocating memory for one with malloc()

or calloc()

.

To use calloc()

, you have two arguments, one is how much you need, and one is the size of any. For, malloc()

you multiply by two. Another difference is that it malloc()

returns memory with what was in it, and calloc()

initializes everything to zero. (This string is zero-length, or whole zeros by the standard. Other values ​​are not guaranteed, but on most modern systems you will get the equivalent of zero.) These functions return a pointer to the allocated memory.

You seem to want tot

int

s, so (using calloc()

) the correct statement is like int * a = calloc(tot, sizeof(int));

or int * a = calloc(tot, sizeof(*a));

. No shading is required in C (required in C ++, but you wouldn't normally use malloc()

either calloc()

in C ++) and the only thing it can do is hide a possible error (leaving #include <stdlib.h>

to be specific).

After that, you can refer to int

as to a[3]

.

Putting the result in the field is ALPHA

doable, but you really need it ALPHA

, so something like

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

      

will work. Therefore you have to call it a.size[3]

eg.

Also, I can't see what it does name

. This is one character not enough for any non-blank line, and I don't know why you got it in the call calloc()

. You may want to name

be a dynamically allocated row, which size

is its size. In this case, you will need to change the lines in the declaration ALPHA

as

int size;
char * name;

      

and the code can be

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

      

Once you have done that, after entering 10 you can refer to a.name[0]

through a.name[9]

and to your ten characters. a.name[10]

will be one by one. Note that you can only put a nine-character string in a ten-character array, since you need to have room for a null terminator ( '\0'

which is the last character of any C style string). If you want to enter the specified number of characters, you must add 1 to a.size

after the user entered the number.

+3


a source


You may just want

ALPHA alphas [tot];

+1


a source







All Articles