0
int* x = malloc(10 * sizeof(int));
int y[10];
x[10] = 0;
y[0]=1;

what is the difference when you allocate memory with this for this two variants. using *x seems to allocate 10 integers using malloc however y[10] seems to be doing the same work.

enter image description here

0

It's more about where the variables are created and stored. Y is on the stack, x is in heap memory. All this will be covered, but I don't remember what week.

| improve this answer | |
0

In the first one using *x and malloc() function, you are not actually creating a memory you are just getting a pointer which will point to a location and with reference to that you can store values using x[10](i.e. 10steps forward from x ).

Whereas is "int y[10];" y gets a memory of 10 * 2bytes in continuous allocations so that none can interfere with that space allocated to it. If 20bytes of continuous space is not available in memory then this array won't be created whereas in the later one it will.

Rest both works well it's just useful in those days where the internal memory of the systems was in MB's and the variables stored using "int y[10];" stays in the system for long since there is no garbage collector(which deallocates the allocated variable in memory space) in C.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .