I'm looking at exams in C from past years, and I'm came across a question I didn't fully understand. They've supplied a simple piece of code, and asked about the location of different variables in the memory. The options were heap stack and unknows.
I know that automatic variables a initialized in the stack, and that dynamically allocated variables are in the heap.
So, first question - what is unknown?
For the second question here's the code and the table with the right answer:
#include <string.h>
#include <stdlib.h>
typedef struct
{
char *_str;
int _l;
} MyString;
MyString* NewMyString (char *str)
{
MyString *t = (MyString *) malloc (sizeof (MyString)); //LINE 12
t->_l = strlen (str);
t->_str = (char*) malloc (t->_l + 1);
strcpy (t->_str, str);
return t;
}
int main()
{
MyString ** arr = (MyString**) malloc (sizeof (MyString*)*10); //LINE 21
int i;
for (i=0;i<10;i++)
{
arr[i] = NewMyString ("item"); //LINE 25
}
// do something
// free allocated memory
return 0;
}
And here's the table with the results I didn't understand
Variable Line number Memory location
arr 21 stack
*arr 21 heap
**arr 21 unknows
arr[5] 25 heap
arr[3]->_str[2] 25 heap
*(t->_str) 12 unknows
I think I understand arr and *arr, but why is **arr unknown?
The rest I couldn't answer at all, so any help would be great.
I know its long so thanks in advance...