Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
122 user(s) are online (92 user(s) are browsing Forums)

Members: 2
Guests: 120

MartinW, flash, more...

Headlines

 
  Register To Post  

(1) 2 3 »
GCC, Dynamic structure creation method ?
Quite a regular
Quite a regular


See User information
Hi,

In fact, I want to create dynamically objects that uses a structure.

Imagine a structure like this one :

typedef struct{
  
BOOL Exist;
  
float XPosYPosZPos;
  
float XRotYRotZRot;
  
float FOV;
  
BOOL Backdrop;
  
float RangeNearRangeFar;
  
float LeftTopRightBottom;
 }
ObjectType;


I want to have a function in my program that allocate a small memory for an object that'll use this structure and, the function must return the pointer to the freshly created object structure.

I'm not enough skilled in C to imagine how to do this.
Any one got an idea about this ?

Thank you.

Kindest Regards,
Freddix


Edited by freddix on 2009/2/25 19:21:59
All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, Dynamic structure creation method ?
Not too shy to talk
Not too shy to talk


See User information
@freddix

ObjectType *AllocateObjectvoid )
{
  
ObjectType newObj NULL;

  
newObj IExec->AllocVecTagssizeofObjectType ), TAG_DONE );
  if( !
newObj ) return NULL;

  
// Do any required initialisation

  
return newObj;
}

Go to top
Re: GCC, Dynamic structure creation method ?
Quite a regular
Quite a regular


See User information
@xeron
Thank you for this small function.

I imagine that if I want to delete this object from memory, I'll have to do something like this :

*FreeObjectNewObjectType ){
  if ( 
NewObjectType <> ){
    
IExec->FreeVecNewObjectType );
   }
 }


Am I wrong ?

All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, Dynamic structure creation method ?
Just can't stay away
Just can't stay away


See User information
@freddix

In C it's "!=" instead of "<>".
void FreeObjectObjectType *obj ){
  if ( 
obj != NULL ){
    
IExec->FreeVecobj );
  }
}


Actually as IExec->FreeVec() allows passing in a NULL pointer (in this case it will simply do nothing) you can just write:

void FreeObjectObjectType *obj ){
  
IExec->FreeVecobj );
}

Go to top
Re: GCC, Dynamic structure creation method ?
Just popping in
Just popping in


See User information
@salass00

Quote:

salass00 wrote:

Actually as IExec->FreeVec() allows passing in a NULL pointer (in this case it will simply do nothing) you can just write:


Does it? You learn something new everyday... I've always checked if the memory pointed is NULL or not before IExec->FreeVec.

Go to top
Re: GCC, Dynamic structure creation method ?
Quite a regular
Quite a regular


See User information
@salass00
It's what I supposed .... but wasn't sure ...
Thank you.

If I'm not wrong, all this should be correct :

typedef struct{
  
BOOL Exist;
  
int Type;
  
BOOL HiddenCullingTransparencyWireFrameFogAmbientLightsFiltering;
  
float XPosYPosYPos;
  
float XRotYRotZRot;
  
float XScaleYScaleZScale;
  
float RGBRedRGBGreenRGBBlue;
 }
Object3DType;

static 
Object3DType *Allocate3DObjectvoid );
static 
void Free3DObjectObject3DType *OldObject );

Object3DType *Allocate3DObjectvoid ){
  
Object3DType *NewObject NULL;
  
NewObject IExec -> AllocVecTagssizeofObject3DType ), TAG_DONE );
  if ( !
NewObject ){
    return 
NULL;
   }
  return 
NewObject;
 }

void Free3DObjectObject3DType *OldObject ){
  if ( 
obj != NULL ){
    
IExec -> FreeVecOldObject );
   }
 }


Right ?


Edited by freddix on 2009/2/25 23:51:35
All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, Dynamic structure creation method ?
Just popping in
Just popping in


See User information
@freddix

There should be an asterisk in the declaration of NewObject in the Allocate3DObject() function:

Object3DType *NewObject = NULL;

It's a pointer.

Go to top
Re: GCC, Dynamic structure creation method ?
Quite a regular
Quite a regular


See User information
@Gazelle
I've modified. is it correct now ?

All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, Dynamic structure creation method ?
Not too shy to talk
Not too shy to talk


See User information
@freddix

Yes. A good tip is to initialise the structure to sensible defaults as much as possible in your constructor. Can save weird bugs and headaches later.

Go to top
Re: GCC, Dynamic structure creation method ?
Quite a regular
Quite a regular


See User information
@xeron
Do you think that handling the list per ranges can be good ?

For example :
Default is defined with 512 entries,
If more than 512, directly re-allocate 1024 entries,
if more than 1024, directly re-allocate 1536 entries,
if more than 1536, directly re-allocate to 2048 entries,
etc ...

Good idea ?

All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, Dynamic structure creation method ?
Home away from home
Home away from home


See User information
@freddix

typedef struct{

  
void *last;
  
void *next;

  
BOOL Exist;
  
int Type;
  
BOOL HiddenCullingTransparencyWireFrameFogAmbientLightsFiltering;
  
float XPosYPosYPos;
  
float XRotYRotZRot;
  
float XScaleYScaleZScale;
  
float RGBRedRGBGreenRGBBlue;
 }
Object3DType;


If you add a next and last pointer you can add objects, so it becomes a list, note that there exist a node and list structure in exec already you can use, this enable you use the exec functions for this.

If you are making a multi threaded game or program, you should be thinking about some kind of lock for etch of objects, so that one thread does not modify the same object at the same time, it called writing a thread safe programs.

(NutsAboutAmiga)

Basilisk II for AmigaOS4
AmigaInputAnywhere
Excalibur
and other tools and apps.
Go to top
Re: GCC, Dynamic structure creation method ?
Quite a regular
Quite a regular


See User information
@LiveForIt
Thank you.

A friend just sent me an e-mail explaining linked-list too.
now I understand what I must do to handle objects dynamically.

Thank you all.

Kindest Regards,
Freddix

All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, Dynamic structure creation method ?
Just can't stay away
Just can't stay away


See User information
@Slash

Quote:

Does it? You learn something new everyday... I've always checked if the memory pointed is NULL or not before IExec->FreeVec.


It does. It's even documented in the autodocs.

Same applies to many other functions, like DisposeObject(), DeleteMsgPort(), FreeSysObject(), Close(), DropInterface(), CloseLibrary(), ...

Go to top
Re: GCC, Dynamic structure creation method ?
Quite a regular
Quite a regular


See User information
@all :
Thank you for all these precious informations.
When I'll get something to show, I'll post a sample ;)

All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, Dynamic structure creation method ?
Quite a regular
Quite a regular


See User information
I encounter another problem with structures.
I've made this with the book help :

struct Basic3DStructType{
  
int *LastCreatedObject;
 };
struct Basic3DStructType Basic3D;

struct Object3DType{
  
int *PreviousObject, *NextObject;
 };

void BASIC3D_Destructorvoid ){
  
struct ObjectType *NewObject Basic3D.LastCreatedObject;
   
struct Object3DType *NextToDelete NewObject.PreviousObject;
 }


I've removed other lines to just show what is wrong ...
I get an error on this line :
Quote:
struct Object3DType *NextToDelete = NewObject.PreviousObject;


BAsic3D.c:64: error: request for member 'PreviousObject' in something not a structure or union

Why ?

I tried to change struc to these :
Quote:
struct Object3DType{
int PreviousObject, NextObject;
};

Same Result ...

Any info ?

All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, Dynamic structure creation method ?
Just popping in
Just popping in


See User information
@freddix
Quote:
void BASIC3D_Destructorvoid ){
  
struct ObjectType *NewObject Basic3D.LastCreatedObject;
  
struct Object3DType *NextToDelete NewObject.PreviousObject;
 }

BAsic3D.c:64: error: request for member 'PreviousObject' in something not a structure or union

Why ?


That's because NewObject is a pointer to a structure and not a structure.

To access elements of a structure through a pointer you need the "->" operator.

struct Object3DType *NextToDelete NewObject->PreviousObject;


Would be correct but will cause a DSI if NewObject is NULL.

This should work:
struct Object3DType
{
  
struct Object3DType *PreviousObject;
  
struct Object3DType *NextObject;
  
/* rest of the objectdata */
};

struct Basic3DStructType
{
  
struct Object3DType *LastCreatedObject;
};
struct Basic3DStructType Basic3D;

void BASIC3D_Destructorvoid )
{
  
struct Object3DType *NewObject Basic3D.LastCreatedObject;

  if (
NewObject != NULL)
  {
    
struct Object3DType *NextToDelete NewObject->PreviousObject;
    
/* your code */
  
}
}

Go to top
Re: GCC, Dynamic structure creation method ?
Quite a regular
Quite a regular


See User information
@Gazelle
ok.

Now I understand how the pointer in struc works.

Thank you Gazelle.


When compiling, I also get some errors on things I'read here for example :

definition :
typedef struct Object3DType{
 ...
 };


Object3DType *Allocate3DObjectint ObjectID ){
  ...
 }

Basic3D.c:48: error: expeted '=', ',', ';', 'asm' or '__attribute__' before '*' token

and

void Free3DObjectObject3DType *OldObject ){
 ...
 }

Basic3D.c:71: error: expected ')' before '*' token

Why ? I use exactly what was adviced to me here in this post ...

Kindest Regards,
Freddix


Edited by freddix on 2009/3/1 10:55:00
All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, Dynamic structure creation method ?
Quite a regular
Quite a regular


See User information
@freddix
no one know ?

All we have to decide is what to do with the time that is given to us.
Go to top
Re: GCC, Dynamic structure creation method ?
Just popping in
Just popping in


See User information
@freddix


I think this is wrong:

typedef struct Object3DType{
 ...
 };


and should be:

typedef struct {
 ...
 } 
Object3DType;

Go to top
Re: GCC, Dynamic structure creation method ?
Quite a regular
Quite a regular


See User information
@Shadow
When I make the changes you said, I get several other error ...

Like :
Basic3D.c:56: error: request for member 'PreviousObject' in something not a structure or union

and previous object is a component of the struct defined ...

All we have to decide is what to do with the time that is given to us.
Go to top

  Register To Post
(1) 2 3 »

 




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




Powered by XOOPS 2.0 © 2001-2023 The XOOPS Project