'Is the value stored on the stack? C

I'm currently studying C and im exploring how a generic Binary Tree is implemented and I stumbled upon this example.

I have this node struct:

struct node{const char * value; struct node * left; struct node * right;};
typedef struct node node;

And this function that creates a tree:

node * create_tree(const void * value, node * left, node * right) {
    node * root = malloc(sizeof(node));
    if (root) {
        root->value=value;
        root->left=left;
        root->right=right;
    }
    return root;
 }

Then this is used in the main as such:

node * root = create_tree("b", create_tree("a", NULL, NULL), create_tree("c", NULL, NULL));

I understand what the result of this is. What I want to make sure I understand is where in memory "b", "a" and "c" end up. From what I understand these are equivalent to a char pointer (since they are in double quotes) and thus my thinking is that the characters 'b' and '\0' are stored on the stack and are pointed to from the heap. Is that what actually happens?

And another question since the struct definition takes a void pointer does create_tree allocate space on the heap based on the argument type once it is called?

c


Solution 1:[1]

String literals such as "a", "b" and "c" are normally stored in read-only memory. They won't be stored on the stack.

If you want them to be stored on the stack, you will have to allocate an array on the stack and copy the content of the string literal into that array. You are merely copying the pointer to the string literal and storing that pointer in the heap (memory allocated by malloc), not the stack.

You are possibly confusing char arrays with char *. Here is an explanation of the differences:

int main(void)
{
    //This will be stored on the stack, because although it
    //is using a string literal for initialization, the
    //array itself is declared as a local variable.
    char str1[] = "Test";

    //This is identical to the previous declaration, except
    //for the fact that the length of the array is specified
    //explicitly.
    char str2[5] = "Test";

    //In this case, only the pointer will be stored on the
    //stack, but not the string itself, because the pointer
    //is pointing to a string literal, which is probably
    //stored in read-only memory.
    char *str3 = "Test";
}

And another question since the struct definition takes a void pointer does create_tree allocate space on the heap based on the argument type once it is called?

The function create_tree is only aware of the value of the pointer value, but not the type of object that it is pointing to. Therefore, it is unable to allocate space based on the type of the referenced object. Also, it will only allocate space on the heap if you tell it to, for example using the function malloc (which you do call once in that function).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1