🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

array getting filled with trash values

Started by
44 comments, last by NikiTo 4 years, 4 months ago

Now i need to ask if you forgot to allocate 1 VertexGroups… it's the only reason i can imagine.

Edit: Otherwise, probably commenting out causes too much change so the corruption moves elsewhere. Which would be a case of the issue i mentioned initially. :(

Advertisement

Thanks JoeJ you did help me push the boundary of knowledge a bit further.

I found the bug, starting clean is sometimes the way to go.

My project`s facebook page is “DreamLand Page”

Calin said:

I initiate an array in a function and check the values within that array right after initialisation and everything is fine, down the road after some code is executed, I check the values again they are modified even though I haven`t touched the array (no direct edit of values). What`s causing this? do I need to place the initialisation outside the function so that the array doesn`t get affected by whatever goes wrong in the said function?

First thing I noticed,

  1. You initiated an array in a function — anything that you declare in a function remains in the scope of the function so once you program exits the function, anything that was stored in the array goes out of scope and you'll end up with trash.

I recommend, you initialize the array OUT of the function or pass it to the function as a reference to do some work with it, so yes, you need to place it, outside of the function.

rafaelsantana said:
You initiated an array in a function — anything that you declare in a function remains in the scope of the function so once you program exits the function, anything that was stored in the array goes out of scope and you'll end up with trash.

The array must be a global or member variable. Otherwise it would not compile.


do I need to place the initialisation outside the function

@rafaelsantana the reason behind what I said was the wish to get rid of the problem not by actually fixing the bug but by `covering up` the issue placing the code outside the problem area.

My project`s facebook page is “DreamLand Page”

JoeJ said:

rafaelsantana said:
You initiated an array in a function — anything that you declare in a function remains in the scope of the function so once you program exits the function, anything that was stored in the array goes out of scope and you'll end up with trash.

The array must be a global or member variable. Otherwise it would not compile.

Correct, that was implied when I said “declare it out of the function” that would make it global. If it was a member variable, he could use the class constructor to initialize it instead.

JoeJ said:

rafaelsantana said:
You initiated an array in a function — anything that you declare in a function remains in the scope of the function so once you program exits the function, anything that was stored in the array goes out of scope and you'll end up with trash.

The array must be a global or member variable. Otherwise it would not compile.

If he declares an array with the same name, twice - once inside the function, once outside, will it compile? It works in Javascript without complaining and gives erroneous result ofc.

NikiTo said:
If he declares an array with the same name, twice - once inside the function, once outside, will it compile?

It would compile without warnings because that's often intended, but we would see his second declaration in the function code snippet. And it would not change the outside data to random values.

@JoeJ If it is a hardcore array, it should have garbage values in the beginning. But he said he first checked the content then did something, and then it was bad. So this is not the case.
I think he said he is a C# guy in other of his posts. It takes time to get used to pointers in C++…

@calin In the end of the day, you might need to manually trace all the writes that happen in the code. This is C++.
(another thing a C# user could initially not notice in C++ is that there are data types. You could get garbage if you mess with the type of data somewhere.)

@joel I would not name two arrays in the same way. I consider this to be an error. The compilers should at least issue a warning in my opinion.

NikiTo said:

@joel I would not name two arrays in the same way. I consider this to be an error. The compilers should at least issue a warning in my opinion.

No, it's rarely a source of accident. First, replace ‘array’ with more general ‘variable’ (or type) and then think about this example:

struct Rect
{
int width, length;
Rect (const int width, const int length) : length(length)
{
this→width = width;
}
};

Ofc. you will only use one of the two shown ways to set the member variables, but just pick what you like more.

And why would you want to think of another variable name just to prevent any confusion, if coming up with variable names is so hard? :D

You will agree it is nice we do not have to.

Next, an array is just a pointer, so ofc. we we do not want to make this a special case and any different from the above.

But there is a similar case which is indeed a flaw:

for (int i=0; i<10; i++)
{
SomeFunction(i);
}

for (int i=0; i<20; i++)
{
SomeDifferentFunction(i);
}

Depending on compiler (settings), the second declaration of i can cause a compile time error ‘i already defined’.

It seems there is (or was) no specification what's the scope of the variables declared in the loop.

I don't know. But i have had no more issues with this since many years, so i hope the above is correct now with every compiler and i only exists within the scope inside the loop.

This topic is closed to new replies.

Advertisement