#### Archived

This topic is now archived and is closed to further replies.

# c++ strangest problem

This topic is 5243 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

void BuildTree(Building* node) { double aa=0; Building *build = first_building; node->nextNodes = &build; node->nextNodes[1] = build; // line 4 } Please take a look at above code, one of my functions, after the program finishes executing line 4, the value of aa is automatically changed to 4460928. There shouldn''t be any connection between line 4 and the value of aa. Does anyone have any idea on why this happens? thanks for response in advance

##### Share on other sites
Show us line 4... That would help, my only thought is that it is declared in line 4, and it got assigned that funky garbage because your constructor didnt assign it anything...

##### Share on other sites
I''ll take a stab at this:

''aa'' and ''build'' are both stack variables declared within your function, and we''ll assume that the compiler places ''aa'' just after ''build'' in (stack) memory.

In line 3, you assign the address of your stack variable ''build'' to node->nextNodes, so now node->nextNodes points to the stack. Based on our above assumptions about what the compiler did, node->nextNodes[0] now points to ''build'' and node->nextNodes[1] points to ''aa''.

So, when you assign the value of the pointer stored in ''build'' to node->nextNodes[1], you are actually setting the value of ''aa''. That''s why it changes.

##### Share on other sites
If nextNodes is the last member of class Building, and nextNodes has been declared as...

Building** nextNodes;

... then not only do you have no idea how to write in C++, but you''ve created a really cool error.

Okay, first. You are assigning &build... that is, the address of a pointer to a Building... to node->nextNodes. Okay. We''ll go with that.

You are then assigning build... that is, the address of a Building... to (node->nextNodes + 1). Which in the call stack, happens to be the low-address half of the 64-bit floating-point aa.

Now, as it stands, that code fragment makes no sense, even performs things you never wanted it to. So I suggest you scrap it and start again.

Remember that the first element of an array is element 0, and that it is usually pointless for a building''s "next node" to be itself...

##### Share on other sites
Since your function is called BuildTree , I''m going to take a wild stab in the dark and assume that your ''nextNodes'' member is deliberately a Building**, or rather a Building*[] of size 2 - so that nextNodes[0] and nextNodes[1] represent the two "children" of the current node.

I have no idea where your ''first_building'' comes from, or why you''re assigning it to a temporary. But it looks like you want to have both children of the input node end up pointing at it (??? is your first_building a "null object"?).

As the other posters pointed out, ''&build'' at this point is somewhere in the call stack. Apparently the double ''aa'' you put in there ends up in the right position to pick up ''element 1'' of the new nextNodes ''array''. (I assume you originally tried it without the aa, and the code crashed outright. Yes?).

Anyway, try simply:
void BuildTree(Building* node){	node->nextNodes[0] = first_building;	node->nextNodes[1] = first_building;}

##### Share on other sites
Is this a Debug build under MSVC++? Small wonder.

##### Share on other sites
quote:
Original post by ji yan
void BuildTree(Building* node)
{
double aa=0;
Building *build = first_building;
node->nextNodes = &build;
node->nextNodes[1] = build; // line 4
}

Please take a look at above code, one of my functions, after the program finishes executing line 4, the value of aa is automatically changed to 4460928. There shouldn''t be any connection between line 4 and the value of aa. Does anyone have any idea on why this happens?

Dude, you''re assigning the address of *build (a local variable) to node->nextNodes... then when you use the [11] subscript, you are addressing another part of the stack (since the stack goes up you''re pointing at aa). You''re writing over your local stack.

##### Share on other sites
quote:
Original post by Anonymous Poster
quote:
Original post by ji yan
void BuildTree(Building* node)
{
double aa=0;
Building *build = first_building;
node-&gt;nextNodes = &amp;build;
node-&gt;nextNodes[1] = build; // line 4
}

Please take a look at above code, one of my functions, after the program finishes executing line 4, the value of aa is automatically changed to 4460928. There shouldn''t be any connection between line 4 and the value of aa. Does anyone have any idea on why this happens?

Dude, you''re assigning the address of *build (a local variable) to node-&gt;nextNodes... then when you use the [11] subscript, you are addressing another part of the stack (since the stack goes up you''re pointing at aa). You''re writing over your local stack.

btw, i can''t remember my login password, but if you want me to email you clarifications, I''m at:
matt@cubewarfare.com
http://salus.cubewarefare.com

##### Share on other sites
You are declaring aa inside the function, therefore aa is local to that function. As soon as the function exits, all local variables are popped off the stack. You could declare it as static.

• 23
• 10
• 19
• 15
• 14