I have some doubts about C allocations.
#include <stdlib.h>
typedef struct MyStruct {
char CharsInMyStruct[50];
} MyStruct;
int main(void) {
struct MyStruct * s;
s = malloc(100 * sizeof *s);
}
I was told that the allocation is "smart", because the space is not "really" (physically) allocated until I use the memory. Is the memory physically allocated once I use the first element of MyStruct or it's allocated when I use each element?
I thought that if I need to dynamically read a file and copy each row in a dynamic struct, I could just allocate for 100 elements, then realloc for the correct dimension after reading the file, so I don't have to read two times to previously know how much to allocate or to realloc for each row. Is that possible / allowed? Is it a good solution?
Edit: sorry, I was too concerned about theory and I forgot to add a decent code. Maybe the question is not clear enough, but I don't need to know how to code what I'm thinking about; I just need to know how the memory is allocated.