Assigning a malloc'd variable value to a new variable, is the new variable also on heap memory?

StackOverflow https://stackoverflow.com/questions/22953093

  •  30-06-2023
  •  | 
  •  

Question

I am confused about whether the following new variable "num2" is also on the heap memory, because I assigned "num1" value to it and num1 was assigned to heap memory.

int main()
{

    int *num1 = malloc(sizeof(int)); /* assign num1 to heap memory */
    *num1 = 7;

    int num2 = *num1;               /* assign num1's value to num2, is num2 also on heap?*/


    return 0;
}
Was it helpful?

Solution

Both variables num1 and num2 are automatic variables which means they are allocated on the stack automatically. The variables have function scope. Once the function they are defined in returns, they are deallocated from the stack, i.e., they cease to exist and trying to access them is undefined behaviour.

A variable which is defined inside a function (including function parameters) and which is not static is an automatic variable. It is automatically created on the stack when the function is called and is destroyed when the function returns. Therefore, it is local to the function, i.e., it has function scope.

A variable which is defined outside of any function is called a global variable. Static variables and global variables have static storage duration which means they are neither allocated on the stack nor on the heap but in text segment (if they are const) or data segment of the program's memory layout or even into its own memory segment on most modern machines. The important thing to note is that the memory is allocated once when the program begins execution and is never freed because the lifetime of global and static variables extends across the entire run of the program. Also, global and static variables are default initialized to zero if not explicitly initialized. Read this - The initialization of static variable in C.

You cannot create a variable on the heap, i.e., an indentifier cannot be bound to a memory location on the heap. You can allocate memory on the heap and store a pointer to it in a variable but the variable itself is not on the heap. If the variable is local and non-static, it's on the stack else it's in the text segment or data segment. Read this for more details - Memory Allocation in C Programs

OTHER TIPS

Whenever you use malloc memory is allocated in heap (if memory is available).

In your case num1 is pointer to memory in heap when you malloc it. (num1 pointer is present in the heap only.)

int num2 = *num1;

when you use the above statement what you're saying is take the value of whatever num1 is pointing to and assign it num2.

and int num2 is signifying that you want the memory to be created in stack for the current stack frame.

To allocate the memory without malloc you can try global or static. which will allocate the memory for it at compilation time in Data segment. but heap you can't directly assign as per my knowledge

for example taking global variable.

int num2;

int main()
{
  // do something
} 

memcpy() only copies value from one location to another for which you need already initialized memory which can either be in heap or stack.

Q Is the new variable on the heap?

A No. Details can be seen at What and where are the stack and heap?

You are confused with the pointer and memory. When you use malloc you get heap memory of size you request, i.e. a collection of bytes with address range say startAddr and endAddr. Now you have allocated heap memory but how will you access it, since, it is contiguous you just need to know start address (startAddr) and size of memory block (which you already know as you yourself allocated it).

This startAddr is returned by malloc after successful allocation of memory. which you can save in a single pointer or you can copy that address (Not the whole allocated memory) to other pointers.

Now difference between stack and heap memory:

Whenever you declare any variable (pointer is also a variable which stores addresses) which is not global or register or static, it always goes to stack. Heap is only accessible for dynamic allocation and can be accessed by alloc family of functions only.

To conclude:

num1 and num2 both pointers are on stack and hence accessible only inside current function block.

While the address num1 (and num2) is pointing to is in heap memory which is available to whole program, i.e. any other function (or thread in multithreaded program) having this address can access that allocated heap memory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top