c++ strangest problem

Started by
7 comments, last by ji yan 20 years, 3 months ago
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
Advertisement
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...
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
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.
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...
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
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;} 

Is this a Debug build under MSVC++? Small wonder.
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?

thanks for response in advance


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.
quote:Original post by Anonymous Poster
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?

thanks for response in advance


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.


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
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.

This topic is closed to new replies.

Advertisement