I was trying out simple memory allocation on stack and heap in C.
int x = 2;
int *y = malloc(sizeof(int));
When I view the address of x on stack and the heap address contained in y, I see the following
x stack address : 0xbfe92bb4
heap address in y : 0x 9c4b008
Are these addresses in different format ( as I don't see the same number hex characters in both of them ) ?
There are three main types of allocations in the usual program model:
The two last types are the ones that allow some kind of dynamic allocation (implemented and used in very different ways), and they are usually either allocated from different areas (which, especially where virtual memory is supported can mean very different addresses) or from different ends of the same area (where allocation of both kinds of memory progresses towards the middle).
Your case is just different memory areas (the heap address should probably read 0x09c4b008 but the 0 gets lost in the output).
The variables are stored in different memory-segments.
Data The data area contains global and static variables used by the program that are initialized. This segment can be further classified into initialized read-only area and initialized read-write area. For instance the string defined by char s[] = "hello world" in C and a C statement like int debug=1 outside the "main" would be stored in initialized read-write area. And a C statement like const char* string = "hello world" makes the string literal "hello world" to be stored in initialized read-only area and the character pointer variable string in initialized read-write area. Ex: static int i = 10 will be stored in data segment and global int i = 10 will be stored in data segment
Heap The heap area begins at the end of the BSS segment and grows to larger addresses from there. The Heap area is managed by malloc, realloc, and free, which may use the brk and sbrk system calls to adjust its size (note that the use of brk/sbrk and a single "heap area" is not required to fulfill the contract of malloc/realloc/free; they may also be implemented using mmap to reserve potentially non-contiguous regions of virtual memory into the process' virtual address space). The Heap area is shared by all shared libraries and dynamically loaded modules in a process.
Continue your investigations at wikipedia
The malloc is on the heap, which is a different memory area than the stack.
The different format is not a difference in format but simply just a fact of where in the process space the memory exist, which is partly down to a decision of the compiler and to the OS when allocating the heap space for malloc.
See also this question which have much more detail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With