2

Does only malloc() allocate memory on the heap or when we use pointer and normal variables put some value then it will be stored in some memory location on heap.

To be clear, below are cases:

//First case:
int x = 10;

//Second case:
int* x;
*x = 10;

//Third case:
int* x = malloc(sizeof(int));
*x = 10;

Could you please confirm my below understanding:

  • In the first case, I am clear that memory will be allocated on stack.
  • In the second case, I think there will be problem because x is having some random memory location and when it is tried to de-reference then there will be issue. But still, will be on stack or heap?
  • In the third case, memory will be on heap.

Now, in the second case we have seen that there will be an issue because of random access of memory, but how the things would be different in below 2 cases:

//4th case
char* str1;
str1 = "asdasd";

//5th case
char* str2 = "asdasd";

Could you please answer:

  • In above 2 cases, where the memory will be allocated?
  • If we had done, 5th case using int* as int* x = 10; then x would have ended up as holding memory location 10 but when we do same thing using char* then string is successfully stored. Why?

Could you please provide all above answers.

I am really confused with in C, in what all scenarios (like using malloc or just by using pointer) memory will be allocated in heap and stack. And more confusion comes when things work differently with char* and int*

1 Answer 1

3

In the first case, I am clear that memory will be allocated on stack.

not necessarily. it depends on where the variable is defined. if it's a global variable for example, the memory will be allocated on a read/write data segment. but yes, in case it's local, the memory will be allocated on the stack.

In the second case, I think there will be problem because x is having some random memory location and when it is tried to de-reference then there will be issue. But still, will be on stack or heap?

well, clearly x doesn't point to anything at the moment you're are to assign it. such operation has an undefined behavior (very bad) and may certainly cause problems.

however, in case you're dealing with pointer, you need to differentiate between two memory locations:

  1. the location where the pointer stores the address.
  2. the location that the pointer points to (begins at the stored address).

in case of

int *p;

there is memory allocated for p to store an address and where this location is totally depends on where p is defined just like the case above.

when you do

p = malloc(sizeof(int));

and assuming everything goes well, this allocates memory for p to point to (specifically of size sizeof(int)). this memory is gonna be on the heap.

how the things would be different in below 2 cases

see How can char* contain a collection of characters instead of a memory address?

3
  • Thank you your inputs. Some quick questions - (1.) In first case, int x = 10; lets say it is global variable then you said read/write data segment, so this is different from heap? And is it required to free is memory using free or not required, the way developer never frees the stack memory? Commented Jul 25, 2015 at 18:02
  • (2.) In the link you provided you said that in case of char *s = "hello"; address where "hello" is stored will be set in s. But when I do printf("*s %s\n", *s); there will be segmentation fault. I think what is stored in s is "hello" and when s is dereferenced then since it is not a valid address, so segmentation fault error. Commented Jul 25, 2015 at 18:02
  • Now, after your inputs I understand that in case of global variable memory is allocated in read/write segment of memory. In case of local variables memory will be allocated in stack but whether I use int* or char* in all cases memory will be on stack? I am not sure about this part. In the last, we are left with the heap, and my understanding on this is that only way to get some memory on the heap is using malloc, right? Commented Jul 25, 2015 at 18:05

You must log in to answer this question.

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