2

I know there are several types of memory allocation. Taking C as language example, we have static allocation, stack allocation, heap allocation.

I understand that the static allocation is done by compiler, but the other two takes place on run time. Who is responsible for that, is it the Compiler, Process or the Operating System?

I'm basically asking if the compiler is involved in running a program?

14
  • 3
    Heap allocation is done by specific functions: malloc, realloc, calloc... so the coder is responsible for allocation and for release (free) the memory. Stack it is done at runtime, by assembler code generated by compiler at compile time. Commented Jun 29, 2016 at 10:00
  • 2
    The compiler is not involved in any way when the program is running. Commented Jun 29, 2016 at 10:12
  • @LPs: you are right, post it as an answer !!! Commented Jun 29, 2016 at 10:58
  • 1
    There is neither a heap nor a stack mandated by the C language. They are implementation details and alternative approaches can be used (and are used). Commented Jun 29, 2016 at 11:24
  • 1
    @Destructor thanks, but an answer to this question must be more detailed and must be explained more in depth which takes time I haven't right now. ;) Commented Jun 29, 2016 at 11:28

2 Answers 2

4

OS is responsible for stack and heap memory allocation.

The OS allocate stack memory for every thread and the language runtime calls the OS for allocate heap memory.

Sign up to request clarification or add additional context in comments.

2 Comments

Most C programs run on systems without a full-grown OS. And quite some don't even have a stack for variables.
I would say this depends, really. The full answer should include various scenarios, explaining hardware differences, libc for barebones (like avr-libc) or linux, etc. I agree with @Olaf - it is wrong to assume that all the hardware runs OS, yet the source code might use dynamic allocation. Here, malloc without OS: nongnu.org/avr-libc/user-manual/malloc.html
-1

Compiler is not involved in running a program. Its job is to build the program to a stage from where the linker can take it to create an executable or take to a engine that executes the code in case of a dynamic builder. It only checks for some protocols that have to be followed for a particular language.

Static allocation (storage space) depends on the files that are being linked to and the storage required by the static structures and variables in the code.

Dynamic allocation is taken by the memory management unit where the resources are been allocated to the task that is in the memory and that is where the heap allocation takes place during the runtime.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.