2

If you have a struct in C like this:

struct mystruct{
        int i;
        char c;
};

and you do:

mystruct m;
m.i = 5;
m.c = 'i';

do you create the struct on the heap or on the stack ?

2
  • 2
    Stack. Stuff in a function is stack, stuff dynamically created is heap. This should be covered in a good basic C tutorial. Commented May 2, 2014 at 23:12
  • 3
    Technically, it wouldn't do anything since it needs to be struct mystruct m;. Commented May 2, 2014 at 23:18

2 Answers 2

6

In the stack:

void func()
{
    mystruct m;
    ...
}

// The address of 'm' is within the stack memory space

In the heap:

void func()
{
    mystruct* m = malloc(sizeof(mystruct));
    ...
}

// The value of 'm' is an address within the heap memory space

In the data-section:

mystruct m;

static mystruct m;

void func()
{
    static mystruct m;
    ...
}

// The address of 'm' is within the data-section memory space

In the code-section:

const mystruct m;

const static mystruct m;

void func()
{
    const mystruct m;
    ...
}

void func()
{
    const static mystruct m;
    ...
}

// The address of 'm' is within the code-section memory space

UPDATE:

Although not directly related to your question, please note that the above rule for const is not entirely accurate, as this keyword has in fact two purposes:

  1. Allocate a variable in the (read-only) code-section of the program.
  2. Prevent you (the programmer) from writing erroneous code, such as changing the value of a variable that you initially intended to keep constant throughout the execution of your program.

But feature #1 is really up to the compiler in use, which may place it elsewhere, depending on your project configuration. For example, sometimes you might want to declare a constant variable just for the sake of feature #2, while feature #1 is not feasible due to insufficient memory space in the code-section.

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

14 Comments

const won't generally affect where the variables live. Take something like void func(char * p) { const char * cp = p; }, where cp will be - and would have to be - created on the stack like any other local variable.
@Paul Griffiths: Sorry to disappoint you, but although variable cp will point to an address on the stack, it will be placed in the code-section (i.e., the address of cp will be an address in the code-section).
@Arun it still needs a sizeof, even if you don't like the sizeof(mystruct) style.
@barak: Since functions are allowed to call themselves, this won't be true. That function might recursively call itself 20 times, in which case you'd need 21 separate copies of cp, and the compiler cannot possibly know how many times it will be called.
@user3386109: Thank you for the correction; was not my idea to remove the sizeof, and I fixed it as soon as I noticed it was edited by someone else.
|
2

The stack. You have to use malloc/calloc to create a heap variable.

1 Comment

What about a global variable?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.