Where in memory are variables stored in c, Heap or Stack?

int main(void)
{
    int a=10;
    char b[10]="HELLO";
    const int x=10;
    return 0;
}

"a" will be stored in the stack only (data segment not possible) with its value(10)

"b" will be stored as a pointer (because the array is a pointer to the first element) in the stack and "HELLO" will be stored in heap ( like if we are using malloc).

"x" can be stored in data, stack, or text depending on compiler.

Please correct me if I am wrong.


"a" will be stored in the stack only (data segment not possible) with its value(10)

Correct.

"b" will be stored as a pointer (because the array is a pointer to the first element) in the stack and "HELLO" will be stored in heap ( like if we are using malloc).

Incorrect.

Think of that line as:

char b[10];
strcpy(b, "HELLO");

b is an array, not a pointer. Stack memory is used for the array.

"x" can be stored in data, stack, or text depending on compiler.

Correct.

链接地址: http://www.djcxy.com/p/82372.html

上一篇: JMP对堆栈和帧指针做了什么?

下一篇: 内存中的变量存储在c,Heap或Stack中的变量中?