Reputation: 41
How are variables really stored in memory? I ask this because say you malloc a segment of memory and assign it to a pointer e.g.
int *p = malloc(10 * sizeof(int));
and then run a for loop to assign integers through p - this seems different to declaring an int variable and assigning an integer to it like:
int x = 10;
Because it's a more explicit declaration that you want an int stored in memory, whereas in malloc it's just a chunk of memory you're traversing through pointer arithmetic.
Am I missing something here? Much thanks.
Upvotes: 3
Views: 121
Reputation: 6555
malloc asigns an memoryblock and returns an pointer to it, its life time is as of dynamicaly allocated memory. You can store any objects of its type in it. An int
int x = 10;
is automatic storage and is an lvalue not an pointer. So you dont have to access it by it's adress, as you would have to by an value a pointer is pointing to. You can access and assign it's value by it's identifyer name. And its also cleaned up, when you leave it's scope.
I summarize your questions about from the deleted answer here again for you: malloc() returns a raw chunk of data, which isn't preserved for any type. Even if you assign it to an int lvalue the data chunk isn't of type int, untill you derefference it as thoose. (means you'r using it with data of a special type.) The chunk you are getting is described by the parameter parsed to the function in Bytes. sizeof() represents the size of the type. so you are getting in this case a chunk that has place for 10 integers. but if you havent used it with the int * ptr, you could also asign the address to a pointer of type char, and use the block as memory block for 40 char variables. But the first time you "put something in there" its then preserved for that type.
Upvotes: 0
Reputation: 2857
1.If you know the array size . Use int array[10]
is faster and more safe than int *array = malloc(10*sizeof(int))
. Only if you didn.t know the size before run-time then you need malloc
thing.
2.The declare int x = 10
, x
stored in stack memory. If you declare int *p = malloc(10*sizeof(int));
the p
is stored in stack memory but the memory p
pointer is in heap.
3.When you use int *p = malloc(10*sizeof(int));
, the function alloc a block memory , it only have the right size. In fact you can store type you want in this memory , thoungh not encourage to do this.
4.If you use int x = 10
, the memory will be freed auto , just after the variable out of its scope. If you use malloc
, you should free the memory by yourself,or memory leak!
Upvotes: 0
Reputation: 11577
when you need an array of data, for example when you receive numbers from the user but don't know the length you can't use a fixed number of integers, you need a dynamic way to crate memory for those integers. malloc
and his friends let you do that. among other things:
here is an article on the differences between heap and stack
i'm writing the pros of each way:
Stack
Heap
Upvotes: 1
Reputation: 142625
Well, when you're doing
int x = 10;
Compiler does whatever needs to be done. But when you're using malloc(), you're in charge of maintaining that memory block, you can use it at your wish, this also gives you the burdon of cleaning it up properly.
Upvotes: 0
Reputation: 1429
Variables declared in C like int x = 10
are located on the stack, which is accessible only until the function it's declared in returns (if it's declared outside functions, we call it global, and it's available through the whole runtime of the application).
Memory allocated with malloc
and similar functions are located in the heap, which is accessible either until it's freed explicitly (e.g. calling free(...)
) or the application terminates (which in case of servers might take weeks/months/years).
Both the stack and the heap is part of the memory, the main difference is in the method of allocation. In C, the *
and &
unary operators can blur the line between the two, so for example in case of a declaration like int x = 10
you can get the address like int* y = &x
, and at the same time, you can assign a value like *y = 15
in case of a pointer as well.
Upvotes: 0