Machine-translated by Google. Questions are authored in English.
Top

Discussion

Consider the following program, where are i, j and k are stored in memory?

int i;
int main()
{
    int j;
    int *k = (int *) malloc (sizeof(int));
}

 

  • A.i, j and *k are stored in stack segment
  • B.i and j are stored in stack segment. *k is stored on heap
  • C.i is stored in BSS part of data segment, j is stored in stack segment. *k is stored on heap
  • D.j is stored in BSS part of data segment, i is stored in stack segment. *k is stored on heap

Answer: C

Correct answer: i is stored in BSS part of data segment, j is stored in stack segment. *k is stored on heap

Each of the three lives in a different region. i is a global with no initialiser, so it has static storage duration and is zero-initialised - such objects go in the BSS, which the loader clears rather than reading from the file.

j is an automatic variable inside main, so it is on the stack. The object k points to comes from malloc and so lives on the heap; note that k itself, the pointer, is on the stack alongside j.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.