Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
77 user(s) are online (40 user(s) are browsing Forums)

Members: 0
Guests: 77

more...

Headlines

 
  Register To Post  

Strange behaviour : OpenGL + C + Pointers
Quite a regular
Quite a regular


See User information
Hi,

This post follow the last one I've made concerning pointers.
Just to tell that I encounter a strange behaviour. Something that appear not logic for me.

To define an object, I created a structure called : Object3DType:
struct Object3DStruct{
  
int ...;
  
float ...;
  ...
 };
typedef struct Object3DStruct Object3DType;

I've also defined a type for a pointer to this kind of structures :
typedef Object3DType *Object3DPointer;


Up to here, everything should be ok.

I create a blank memory for storing a list of 256 objects pointers :
Object3DPointer *ObjectList;
ObjectList IExec->AllocMem512 * (int*), MEMF_CLEAR );

ObjectList is then the pointer to the freshly created memory location to store 512 objects pointers.
( pointer to an array of 512 Object3DPointer )

Now, when I create a new 3D Object, I do this :
Object3DPointer NewObject;
NewObject IExec -> AllocMemsizeofObject3DType ), MEMF_CLEAR );

and to store this pointer in the list, I do this :
AddObjectToListObjectIDNewObject);

Here is the AddObjectToList function:
void AddObjectToListint ObjectIDObject3DPointer ObjectPTR ){
  if ( 
ObjectID >> ){
    if (
Basic3D.ObjectListAmount ObjectID ){
      
ResizeObjectListObjectID );
     }
    
ObjectListObjectID ] = ObjectPTR;
    
Basic.ObjectAmount++;
   }
 }

The first thing that is wrong for me is that I should logically do this :
*ObjectListObjectID ] = ObjectPTR;

To write in the memory location at offset ( ObjectID * 4 ) but if I do this, I get an error telling that types are incompatibles ...

The second thing is now when I want to get the pointer of the object. I do this :
Object3DPointer MyObject;
MyObject GetObjectPTRObjectID );

I should then, following the AddObjectToList quotes, do this :
Object3DPointer GetObjectPTRint ObjectID )[
  
Object3DPointer NewObjectPTR NULL;
  if ( 
ObjectID ){
    if ( 
ObjectID <= Basic3D.ObjectListAmount ){
      
NewObjectPTR ObjectListObjectID ];
     }
   }
  return 
NewObjectPTR;
 }

If I do all of these, my program crash... strange.
if I simply change 1 line with :
NewObjectPTR = &ObjectListObjectID ];

The program run fine ...
Why ???

Can someone light me about these odd things ?
I'm sure I'm doing something wrong with pointer of pointer array but I don't find any solution.

Thank you

Regards,
Freddix/AmiDARK.

All we have to decide is what to do with the time that is given to us.
Go to top
Re: Strange behaviour : OpenGL + C + Pointers
Just popping in
Just popping in


See User information
@freddix

if ( ObjectID >> 0 ){

should be >= 0 I think.

Go to top
Re: Strange behaviour : OpenGL + C + Pointers
Quite a regular
Quite a regular


See User information
@freddix

ObjectListObjectID ]
is the same as writing
*(ObjectList + (ObjectID*sizeof(Object3DPointer))

If you prefer we can take an example with an array of char :
char charArray;
to modify character at position <n> you would either write
*(charArray+n*sizeof(char)) = 'a';
either
charArray[n]='a';
If you can understand this, just replace char by Object3DPointer, charArray by ObjectList and <n> by ObjectID.

Once this is clear, you'll understand the warning about line
*ObjectListObjectID ]=NewObjectPTR;
(i.e. you are trying to put an Object3DPointer variable into something having type *Object3DPointer in other word into an Object3D. Then if you remove the '*' in front of this line the second line should not need the '&'

Back to a quiet home... At last
Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2023 The XOOPS Project