Possible duplicate:When should malloc be used in C, and when not?
Hi, I am new to C and found the malloc function. When should I use it? In my work, some say that you should use malloc in this case, but others say that you do not need to use it in this case. So my question is: When should I use malloc? This may be a dumb question for you, but for a programmer who is not familiar with C, this is confusing!
When should malloc be used in C, and when not?
malloc() " ". , , -.
, ,
int my_table[10]; // Allocates a table of ten ints.
, , , ints ,
int *my_table; // During execution you somehow find out the number and store to the "count" variable my_table = (int*) malloc(sizeof(int)*count); // Then you would use the table and after you don't need it anymore you say free(my_table);
one Main use is when you are working on a list of items and the size of the list is unknown to you.