is this allocated in the heap or stack?

Given this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct Example{
    int number;
    string word;
};
struct Node{
    Example ex;
    Node* next;
};

int main(){
    Example someObject;
    someObject.number=10;
    someObject.word="hello";
    Node* head;
    head=new Node;
    head->ex=someObject;
    head->next=nullpointer;
    return 0;
}


someObject is a struct variable created in the stack, while head points to an object created in the heap. So, is it correct to say that head->ex=someObject; copies all someObject members into head->ex and now I have a copy (someObject) in the stack and another copy (head->ex) in the heap, so that if I change the values in someObject it will not affect head->ex value?
Last edited on
Is this allocated in the heap or stack?
head = new Node;
It is allocated in the heap.

Edit :
So that if I change the values in someObject it will not affect head->ex value?

Yes.
Last edited on
Sorry, but by "yes" you mean my analysis is correct or that it will indeed affect the head->ex value stored in the heap?
Topic archived. No new replies allowed.