2

I'm trying to understand where memory is allocated in c programs.

void func1(char e, int f, int g)
{
    int b = 4;
    char * s = "hello world";
    char * temp = (char *) malloc(15); 

}

As I understand it, there are three automatic variables allocated on the stack b,s, and temp. where the contents of b (4) is stored on the stack, and the pointers s and temp are stored on the stack, and the data for temp is stored in the heap, but where is the data for s stored? because the b,s, and temp will be gone when we leave the func1 call but the memory for the contents has been allocated permanently. My other questions is the stack pointer always shifted down by 4 like when pushing the functions arguments on, even in the case of a char which is one byte? would the stack look like this, even though e is only one byte?

30:// other stuff
26: g
22: f
18: e

http://www.firmcodes.com/wp-content/uploads/2014/08/memory.png isnt this the layout for a c program?

  • 5
    Just anywhere your tool-chain is putting it. It depends on the architecture, the compiler, the linker, what's not.. For GCC it will likely to go to the `.rodata` section, but where would it be physically.. see above. – Eugene Sh. Aug 25 '16 at 19:55
  • 1
    Please read why you should not cast the return value of [`malloc()`](http://stackoverflow.com/a/605858/1983495). And I think you are right about the stack but also note that there are no `char`s there are only pointers and it's either 4 or 8 for pointers. The *string literal* which is the correct term for the `"hello world"` string in your code should be in the [Data segment](https://en.wikipedia.org/wiki/Data_segment). It would be readonly. – Iharob Al Asimi Aug 25 '16 at 19:58
  • 1
    Try compile it into assembly to see what would happen. e.g on linux / gcc / intel cpu, use: `gcc -S -masm=intel program_name.c` – Eric Aug 25 '16 at 19:59
  • Good catch @iharob, I will include that in my answer, if you don't mind. – gsamaras Aug 25 '16 at 19:59
  • There is no stack in the C language. That word does not even appear once in the standard. Same for a heap. None of those concepts is mandated by C nor used for every implementation. Actually most modern implementations do not store all automatic variables on the/a stack. And `s` is a normal automatic variable. Why do you think it's value is stored anywhere else than for the others? – too honest for this site Aug 25 '16 at 20:04
  • @EricWang: That should work for AMD-CPUs as well ... you likely mean x86 – too honest for this site Aug 25 '16 at 20:07
  • @Olaf Yes, actually `-masm` specify the assembly syntax, not cpu. – Eric Aug 25 '16 at 20:10
  • @EricWang: I was aware of that. I refered to "e.g on linux / gcc / **intel** cpu" – too honest for this site Aug 25 '16 at 20:11
  • @olaf what do you mean that there is no stack in the c language? – user3743168 Aug 25 '16 at 22:25
  • @user3743168: I mean that the C language standard does not require an implementation to use a stack. And there are actually implementations which do not use a variable-stack. Not using a call stack is much less common, but for very small devices/code the compiler/linker might also use static call/return here. – too honest for this site Aug 25 '16 at 22:31
  • @user3743168: Automatic objects have lifetimes that are arranged in a "stack-like" last-in first-out manner. That doesn't imply a "stack" in the sense of a contiguous region of memory managed via a stack pointer register. Most implementations do use such an arrangement, but not all do. (And as an optimization, variables that might ordinarily be allocated on the "stack" can be allocated in registers, as long as the code doesn't need their addresses.) – Keith Thompson Aug 25 '16 at 22:58

2 Answers2

1

It depends on your platform and it is implementation-defined. In general it goes to the read-only memory, if available on your system. Read more here.

As you noted, when the function terminated the automatic variables will be gone, causing a memory leak, but only for the case where you allocated memory dynamically!

That means that you have to let func1() communicate temp with its caller (e.g. main()), so that you can later free() it. Or if you don't need it after the function has done its job, then free() it just before exiting the function.


By the way, as iharob said: Do I cast the result of malloc? NO!


As for your other question, which should be a new question, read this and this, they might help.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

In standard C terminology there are four possible storage durations:

  • automatic (b)
  • static ("Hello world")
  • dynamic (space allocated by malloc(15))
  • thread

The other stuff you ask about in your question are properties of particular compilers and platforms. Some setups have no stack and no heap, no data segment or .data or .bss section, and so on.

Further reading:

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365