c++
Does anyone know any good resources to help with passing pointers around from different structures and classes involving multiple different header-files? I'm getting some data corruption issues(or at least I was until I decided "Frak it, I'll just put the tile map display offsets in with the globals!")
I lost the code(I stupidly backspaced instead of just commenting it out), so I cant show it to you right now.
It's mostly just an issue of 'you're doing it wrong.' Passing pointers around won't cause problems on its own, so long as you have the right stuff included. With issues involving data corruption, it's due to either forgetting how variable scope works or forgetting how pointers work, and usually without realizing you overlooked that particular aspect of how things work.
Scope issues commonly result from creating an object in a function or other class without using manual memory allocation. For example:
int main()
{
int* a = foo();
}
int* foo()
{
int aNum = 5;
int* value = &aNum;
return value;
}
The above code will result in a pointing to memory which is de-allocated when the function foo() ends, due to function scope. foo() ends, aNum's memory is deallocated, and the returned pointer points to deallocated memory. To fix this, either return a copy (usually sub-optimal) or use dynamic memory allocation. Just be sure to use a delete for every new you use, or you end up with memory leaks.
Issues coming from pointers themselves usually involves something you happened to overlook while planning your code. Things like:
int main()
{
int* a = new int[5];
for(int i=0; i<5; i++)
a[i] = 1;
ChangeInt(a);
}
void ChangeInt(int* a)
{
delete[] a;
a = new int[5];
for(int i=0; i<5; i++)
a[i] = 2;
}
The end result of the above code will be all the values within array a in main deleted, followed by the creation of a new set of values which won't be pointed at by the array a in main, thus creating a memory leak on top of not setting the values. As a pointer is essentially an unsigned integer of some sort, it can help to think of the pointer itself being data; albeit data which is being used for a special purpose. In this case, changing it to the following would work (assuming delete[] is called on a in main at some point before it exits):
int main()
{
int* a = new int[5];
for(int i=0; i<5; i++)
a[i] = 1;
ChangeInt(&a);
}
void ChangeInt(int** a)
{
delete[] *a;
a* = new int[5];
for(int i=0; i<5; i++)
(*a)[i] = 2;
}
Those 2 examples are some of the more common problems involving so-called data corruption with pointers. If you aren't using arrays in such a way, your problem is almost certain scope related, in which case you need to go through and figure out when and why your data is being deallocated; and it's usually because of treating scoped data like it will always exist. Pointers are nice because they let you send a small handful of bytes telling the program where data is stored; but they can be not-so-nice when you accidentally de-allocated the memory the data is in, either with delete or from leaving scope.
When dealing with classes/structs (in C++ they are exactly the same thing; struct is just public by default whereas class is private by default), pointers behave the same way, with the addition being class data members having the scope of that instance of the class.
struct a
{
a(){aNum=5};
int aNum;
};
int main()
{
a* instance = new a();
int* ptr = instance->aNum;
delete instance;
//ptr is now pointing to de-allocated memory
}
Also keep in mind that de-allocated memory may not change immediately. When it is de-allocated, the value will stay the same for a while, then at some random point in the future will change.