Extracting objects to array
-
How do I extract a gob of class information to an array? For example say I have a class:
class Box
{
int Right;
int Left;
int Bottom;
};Let's say it's 96bytes in Size. There is another function that returns the class but it is now 480bytes in size. From that and doing calculations there is 5 objects of that size that was returned. How do I assign that information into an array? Or, what is the best way to extract each piece of the class? Thanks HomeNuke ---- "Nuke'd Your Home, Yet?" Run your own PostNuke based web server from home http://www.homenuke.com
-
How do I extract a gob of class information to an array? For example say I have a class:
class Box
{
int Right;
int Left;
int Bottom;
};Let's say it's 96bytes in Size. There is another function that returns the class but it is now 480bytes in size. From that and doing calculations there is 5 objects of that size that was returned. How do I assign that information into an array? Or, what is the best way to extract each piece of the class? Thanks HomeNuke ---- "Nuke'd Your Home, Yet?" Run your own PostNuke based web server from home http://www.homenuke.com
Tip: always name your classes C*
class CBox
{
int Right;
int Left;
int Bottom;
};// assuming SomeFunc is the function that returns the pointer
// to a blob of memory with those classes in it. And it sets
// nSize to be the size of the memory allocated.
int nSize;
CBox *p_BoxArray = (CBox *) SomeFunc(&nSize);int NumberOfObjectsInArray = nSize / sizeof(CBox);
for (int i=0; i < NumberOfObjectsInArray ; i++)
{
cout << p_BoxArray[i]->Right << "\n";
}// remember to free the memory when you're done with it.
why does this work? well, you can use a pointer as if it were an array (there isn't really any difference between them, in fact.). Logically speaking, incrememnting a pointer does not make it point to that memory address + 1 byte, it makes it point to that memory address + (the size of whatever object it points to). Sorry to dissapoint you all with my lack of a witty or poignant signature.
-
Tip: always name your classes C*
class CBox
{
int Right;
int Left;
int Bottom;
};// assuming SomeFunc is the function that returns the pointer
// to a blob of memory with those classes in it. And it sets
// nSize to be the size of the memory allocated.
int nSize;
CBox *p_BoxArray = (CBox *) SomeFunc(&nSize);int NumberOfObjectsInArray = nSize / sizeof(CBox);
for (int i=0; i < NumberOfObjectsInArray ; i++)
{
cout << p_BoxArray[i]->Right << "\n";
}// remember to free the memory when you're done with it.
why does this work? well, you can use a pointer as if it were an array (there isn't really any difference between them, in fact.). Logically speaking, incrememnting a pointer does not make it point to that memory address + 1 byte, it makes it point to that memory address + (the size of whatever object it points to). Sorry to dissapoint you all with my lack of a witty or poignant signature.
Jon, Thanks for your answer! It is exactly what I needed to point me in the right direction. Well, your answer practically kicked me in the right direction because it was so detailed. Thanks Again. Also did some further thought wouldn't this returned memory also be defined as an array of the object? Shouldn't we be able to access the returned information as an array? Or, is an array defined internally as something different or different flags are set in the memory space that an array occupies? Just questions... HomeNuke ---- "Nuke'd Your Home, Yet?" Run your own PostNuke based web server from home http://www.homenuke.com