2

Let's say I have these variables and pointers. How do I determine which is in stack or heap?

     #include <stdio.h>
     #define SIZE 5


    int main( void ) {

    int *zLkr;                 
    int *aLkr= NULL;
    void *sLkr= NULL;
    int num, k;
    int a[SIZE] = { 1, 2, 3, 4, 5 };
    zLkr = a;

    }
trincot
  • 317,000
  • 35
  • 244
  • 286
answerSeeker
  • 2,692
  • 4
  • 38
  • 76
  • 1
    Since there is no dynamic allocation, the question is are these variables declared in a function body, or at file/global scope? – jxh Oct 07 '13 at 03:44
  • code's been changed to make better sense – answerSeeker Oct 07 '13 at 03:45
  • Curious, what's your use case? – Cory Nelson Oct 07 '13 at 03:46
  • assume it's in a function integers/addresses are stored in 4 bytes and starting array address is 122500 – answerSeeker Oct 07 '13 at 03:46
  • Then that is your answer, all variables are function scope variables. Nothing is dynamically allocated. – jxh Oct 07 '13 at 03:47
  • Ok so they're all stack? – answerSeeker Oct 07 '13 at 03:48
  • If this code is inside a function (why not show a top line such as `void function(void){` to indicate that clearly?), then they're all stack variables. – Jonathan Leffler Oct 07 '13 at 03:48
  • There's no way to determine if an arbitrary address is part of a function's stack space in C (hell, there's no such thing as "stack space" in C), though you could use [platform-specifics](http://stackoverflow.com/questions/6700966/thread-stack-pointer) to test. – Cory Nelson Oct 07 '13 at 03:48
  • Ok I re-edited it to put it inside a function – answerSeeker Oct 07 '13 at 03:51
  • There is a case which contradicts the "function local variables are placed on the 'stack'" rule of tumb -- in C99, if you use *variable length arrays* (vla:s), the compiler is allowed to allocate them dynamically. – Lindydancer Oct 07 '13 at 03:59
  • @Lindydancer: The variable has automatic duration, which is not "heap" in the sense that the asker means. For instance, when returning the address of the VLA from the function, if the caller tries to use the array, it would be undefined behavior. – jxh Oct 07 '13 at 04:37

2 Answers2

2

All your variables have automatic scope. They come from the "stack", in that the variables are no longer valid once the function returns.

Named function variables can never come from the "heap" in the sense that you mean it. The memory for a named function variable is always tied to the function scope (or the innermost block scope within the function in which the variable is declared).

A variable can be assigned a value obtained by malloc() or similar dynamic allocation function. The variable then points to an object that exists in the "heap". However, the named pointer variable itself is not in the "heap".

Sometimes the "stack" itself is dynamically allocated. Such as for a thread. Then, the memory used to allocate function local variables running within that thread is in the "heap". However, the variables themselves are still automatic, in that they are invalid once the function returns.

jxh
  • 69,070
  • 8
  • 110
  • 193
  • If I had `num = *zLkr[2];` in the function the value would be 3 but would that still be in stack? – answerSeeker Oct 07 '13 at 03:56
  • `num` is on the stack. `*zLkr[2]` is an invalid expression. `zLkr` points to `&a[0]`. `a` is on the "stack". – jxh Oct 07 '13 at 03:58
  • so it should be `num = &zLkr[2];` so then the value would still be 0? and it would still be on the stack? – answerSeeker Oct 07 '13 at 04:05
  • `&zLkr[2]` is a pointer to `int`, and `num` is an `int`. `num` is on the stack. `a` is on the stack (which means, each of `a`'s elements are on the stack). `zLkr` points to `&a[0]`. – jxh Oct 07 '13 at 04:06
  • I see... the array subscript notation doesn't apply to zLkr, it only applies to variable a and they are all on the stack. – answerSeeker Oct 07 '13 at 04:10
  • `zLkr[2]` is equivalent to `*(zLkr + 2)` and is equal to `a[2]`. – jxh Oct 07 '13 at 04:12
  • Ok, so the value will be 3 because a[2] is 3 – answerSeeker Oct 07 '13 at 04:17
1

Local variables are allocated on the stack

int main(void)
{    
    int thisVariableIsOnTheStack;

    return 0;
}

Variables from the heap are allocated via malloc somewhere in memory. That memory can be returned to the heap, and reused by a later malloc call.

int main(void)
{
    char *thisVariableIsOnTheHeap = (char *) malloc(100);

    free (thisVariableIsOnTheHeap);
    return 0;
}

Module variables are neither. They have a constant address in memory in one module.

void f1(void)
{
    /* This function doesn't see thisIsAModule */
}

int thisIsaModule = 3;

void f(void)
{
    thisIsaModule *= 2;
}

int main(void)
{
    return thisIsaModule;
}

Global variables are neither. They have a constant value in memory, but can be referred to across modules.

extern int globalVariable;    /* This is set in some other module. */

int main(void)
{
    return globalVariable;
}
EvilTeach
  • 28,120
  • 21
  • 85
  • 141