Amit Singh Tomar
Amit Singh Tomar

Reputation: 8610

Memory allocation for Structure having an array in C

I have structure containing an array. Something like given below:

struct Node
{
    int a;
    char a1[25];
}obj;

main()
{
    struct Node *p = malloc(sizeof(struct Node));
    p->a=10;
}

I would like to know is two different memory section is allocated for this piece of code one from stack for storing structure node and other from heap for storing node pointed to by pointer p?

Upvotes: 0

Views: 229

Answers (4)

Rafael Dazcal
Rafael Dazcal

Reputation: 355

You should notice that you are only declaring a structure (struct Node).

By just declaring it, no memory is actually reserved for it. You are only informing the compiler such an organization of data exists.

When you call malloc, only than you reserve some memory to do whatever you whish with it.

Now, if you were to declare:

struct Node{
 .....
}myNode;

In that case a space in memory would indeed be allocated for myNode.

To answer your follow up question, it won`t be allocated neither in stack nor heap.

Heap can grow with memory allocation. Stack can grow with local variables. This is a global var that will not change in size so it can be placed at a fixed location in memory which is called data segment.

That`s also where static variables are stored.

To get a bit deeper on the subject, when you`re working with an OS like Windows or Linux, before your program is run it is copied to a location in the RAM memory. Part of it will be called code segment, where the read only code is located, part is the data segment, which is where this variable will be located. The rest of the RAM that your program gets access to will be free for heap and stack.

On an embedded system without an OS, the program is run directly from Flash, so its code segment is programmed in Flash. It`s data segment is allocated for the available RAM (shared with stack) and there simply is no heap (you need some memory managment module for the heap concept to make sense)

Upvotes: 0

Jeyaram
Jeyaram

Reputation: 9474

After Edit:

struct Node
{
    int a;
    char a1[25];
}obj;

Instead it will be allocated in data section I guess. This code is modified little-bit like

#include <stdio.h>
#include <stdlib.h>

struct Node
{
    int a;
    char a1[25];
}obj;

int global_data;

int main()
{
   // struct Node *p = malloc(sizeof(struct Node));
   // p->a=10;

    obj.a = 10;

    return 0;
}

And when we look at assembly code.

 .file   "ada.c"
        .comm   obj,32,32
        .comm   global_data,4,4
        .text
        .globl  main
        .type   main, @function
main:
.LFB0:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp


both global_data and obj allocated in same location. i.e uninitialized data section. There will be no chance to allocate in heap. Because there is no call to malloc().

Upvotes: 1

ouah
ouah

Reputation: 145829

p is an object with automatic storage duration; its storage is in the stack on most compilers.

It points to a structure object with allocated storage duration; the storage of this structure object is in the heap on most compilers.

Upvotes: 3

user142162
user142162

Reputation:

There is no stack allocation for the array here. The array inside of struct Node is stored in a continuous block on the heap. You can see this by printing sizeof(struct Node), which will equal at least:

sizeof(int) + sizeof(char) * 25

Upvotes: 3

Related Questions