Archived

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

the_recon

Why is this ALWAYS happening to me!?

Recommended Posts

I need help. I have this function which uses structures. The thing is, it can use all kinds of structures: struct Foo { int a,b,c,d; }; struct Bar { int e,f,g,h; }; struct myStruct { int I,feel,good; }; the structure has a parameter where i insert a pointer of a structure which i then can use to poke around in a memory "pool" of which the structure creates. UnknownFunction(0,0,(BYTE**)&structFoo); structFoo[0].a = 1211; structFoo[0].b = 2422; structFoo[0].c = 3633; UnknownFunction(0,0,(BYTE**)&structBar); structBar[0].e = 1211; structBar[0].f = 2422; structBar[0].g = 3633; UnknownFunction(0,0,(BYTE**)&myStruct); myStruct[0].I = 1; myStruct[0].feel = 1; myStruct[0].good = 0; Now I *_really_* hate defining pointers which I''m only going to use once, so i''ve made my own function of which stores a structure i provide, in a predefined pointer. I.e: void *pStruct; which I SHOULD be able to use: pStruct.x = dumdedum; But when I do this, the program keeps complaining about "Size unknown!!". Is it even POSSIBLE to have a pointer which can change in type? Or something like that? I.e: MyFunc(Foo); //pStruct = Foo; pStruct[0].a = 0; //instead of Foo[0].a = 0 MyFunc(Bar); //pStruct = Bar; pStruct[0].e = 0; //instead of Bar[0].e = 0 MyFunc(myStruct); //pStruct = myStruct; pStruct[0].Good = 0; //instead of myStruct[0].Good = 0

Share this post


Link to post
Share on other sites
quote:
Original post by the_recon
I need help. I have this function which uses structures.
The thing is, it can use all kinds of structures:
struct Foo
{
int a,b,c,d;
};

struct Bar
{
int e,f,g,h;
};

struct myStruct
{
int I,feel,good;
};

the structure has a parameter where i insert a pointer of a structure which i then can use to poke around in a memory "pool" of which the structure creates.



What do you mean with "the structure has a parameter"?? Structures
doesn''t have parameters. Do you mean member?

quote:

UnknownFunction(0,0,(BYTE**)&structFoo);
structFoo[0].a = 1211;
structFoo[0].b = 2422;
structFoo[0].c = 3633;

UnknownFunction(0,0,(BYTE**)&structBar);
structBar[0].e = 1211;
structBar[0].f = 2422;
structBar[0].g = 3633;

UnknownFunction(0,0,(BYTE**)&myStruct);
myStruct[0].I = 1;
myStruct[0].feel = 1;
myStruct[0].good = 0;

Now I *_really_* hate defining pointers which I''m only going to use once, so i''ve made my own function of which stores a structure i provide, in a predefined pointer.
I.e:
void *pStruct;

which I SHOULD be able to use: pStruct.x = dumdedum;


No. The compiler needs to know the size of the structure so you
can use the brackets, and .x... There''s no way for the compiler to
know which type of structure it is. There can be many different
types of structures which have a "x" member

quote:

But when I do this, the program keeps complaining about "Size unknown!!". Is it even POSSIBLE to have a pointer which can change in type? Or something like that?
I.e:
MyFunc(Foo); //pStruct = Foo;
pStruct[0].a = 0; //instead of Foo[0].a = 0

MyFunc(Bar); //pStruct = Bar;
pStruct[0].e = 0; //instead of Bar[0].e = 0

MyFunc(myStruct); //pStruct = myStruct;
pStruct[0].Good = 0; //instead of myStruct[0].Good = 0



MyFunc(Foo); //pStruct = Foo;
((Foo*)pStruct)[0].a = 0; //instead of Foo[0].a = 0

MyFunc(Bar); //pStruct = Bar;
((Bar*)pStruct)[0].e = 0; //instead of Bar[0].e = 0

MyFunc(myStruct); //pStruct = myStruct;
((myStruct*)pStruct)[0].Good = 0; //instead of myStruct[0].Good = 0


Joakim Asplund
http://megajocke.cjb.net

Share this post


Link to post
Share on other sites
What you are trying to do is quite dangerous and even if it is possible, you shouldn''t do it anyway. Go back to your drawing board and find another way to do whatever it is you''re trying to do.

If you''re intent on playing around with memory, I recommend learning C++. At least then you could do your poking around in a type-safe fashion **''s and C type casts send uneasy shivers down my spine every time I see them and the bad memories come back...

Besides, if --I-- was going to poke around memory (which I might add is bad practise) then I would first of all create an alias:
typedef void* LPVOIDPTR;

Then all I would do is typecast the structure to a pure pointer and read in the data as a sequential stream of bytes.

Just dont bother, k?



-------- E y e .Scream Software --------
----------------------------------------
                                  /-\
    http://www.eyescream.cjb.net | * |
                                  \-/
----------------------------------------

Share this post


Link to post
Share on other sites
struct eins
{
int i;
...
} x;

struct zwei
{
int j;
...
} y;

void * p;

p = &x;
cout << (struct eins *)(p)->i;
p = &y;
cout << (struct zwei *)(p)->j; // These 2 lines are
cout << (struct eins *)(p)->i; // identical.

The last thing will only work when your structures have the same variable type at the same offset. Dont do such things unless youre absolutly sure it will make things easier.
-> x.i == (struct zwei *)(&x)->j;

Maybe you can use a union.

However, as was already pointed out, the compiler has no way to know what structure you mean if you dont tell him (unlike perl).


[edited by - Ketzer on June 10, 2002 5:48:15 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Are you using C or C++? If you are using C++ you might not be able to do that at all(don''t quote me)...

Why don''t you use void *?

Share this post


Link to post
Share on other sites
If you are willing to use C++, this will give you the functionality you want.

  
#include <iostream>

class BaseClass {
public:
BaseClass() {x=0;y=0;}
virtual void setX(int a) {x=a;}
virtual void setY(int b) {y=b;}
virtual int getX() {return x;}
virtual int getY() {return y;}
private:
int x,y;
};

class FooClass:public BaseClass {
public:
FooClass() {x=1;y=1;}
void setX(int a) {x=a;}
void setY(int b) {y=b;}
int getX() {return x;}
int getY() {return y;}
private:
int x,y;
};

class BarClass:public BaseClass {
public:
BarClass() {x=2;y=2;}
void setX(int a) {x=a;}
void setY(int b) {y=b;}
int getX() {return x;}
int getY() {return y;}
private:
int x,y;
};

void UnknownFunction(int a,int b,BaseClass *test) {
test->setX(a);
test->setY(b);
}

int main(int argc, char *argv[]) {
BaseClass *base=new BaseClass();
FooClass *foo=new FooClass();
BarClass *bar=new BarClass();

cout << "base->x=" << base->getX() << endl;
cout << "base->y=" << base->getY() << endl;
cout << "foo->x=" << foo->getX() << endl;
cout << "foo->y=" << foo->getY() << endl;
cout << "bar->x=" << bar->getX() << endl;
cout << "bar->y=" << bar->getY() << endl;

UnknownFunction(3,4,base);
UnknownFunction(5,6,foo);
UnknownFunction(7,8,bar);

cout << "base->x=" << base->getX() << endl;
cout << "base->y=" << base->getY() << endl;
cout << "foo->x=" << foo->getX() << endl;
cout << "foo->y=" << foo->getY() << endl;
cout << "bar->x=" << bar->getX() << endl;
cout << "bar->y=" << bar->getY() << endl;

return 0;
}

Here is another option, depending on what you are looking for:

  
#include <iostream>

class BaseClass {
public:
BaseClass() {x=0;y=0;}
virtual void setX(int a) {x=a;}
virtual void setY(int b) {y=b;}
virtual int getX() {return x;}
virtual int getY() {return y;}
protected:
int x,y;
};

class FooClass:public BaseClass {
public:
FooClass() {x=1;y=1;}
void setX(int a) {x=a;}
void setY(int b) {y=b;}
int getX() {return x;}
int getY() {return y;}
};

class BarClass:public BaseClass {
public:
BarClass() {x=2;y=2;}
void setX(int a) {x=a;}
void setY(int b) {y=b;}
int getX() {return x;}
int getY() {return y;}
};

void UnknownFunction(int a,int b,BaseClass *test) {
test->setX(a);
test->setY(b);
}

int main(int argc, char *argv[]) {
BaseClass *base=new BaseClass();
FooClass *foo=new FooClass();
BarClass *bar=new BarClass();

cout << "base->x=" << base->getX() << endl;
cout << "base->y=" << base->getY() << endl;
cout << "foo->x=" << foo->getX() << endl;
cout << "foo->y=" << foo->getY() << endl;
cout << "bar->x=" << bar->getX() << endl;
cout << "bar->y=" << bar->getY() << endl;

UnknownFunction(3,4,base);
UnknownFunction(5,6,foo);
UnknownFunction(7,8,bar);

cout << "base->x=" << base->getX() << endl;
cout << "base->y=" << base->getY() << endl;
cout << "foo->x=" << foo->getX() << endl;
cout << "foo->y=" << foo->getY() << endl;
cout << "bar->x=" << bar->getX() << endl;
cout << "bar->y=" << bar->getY() << endl;

return 0;
}

ps. Both these samples work, I compiled and ran them.

[EDIT] You can do the same thing with structs, but you cannot directly access the members with inheritance in this fashion, because you have to use the virtual keyword and that only works on methods(I think).
---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home

[edited by - CaptainJester on June 12, 2002 8:49:57 AM]

Share this post


Link to post
Share on other sites