This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Defining a structure and allocating memory for the structure ?

Hello Experts,

I have a structure shown below

typedef struct

{

   XDAS_Int16 count;

   XDAS_Int Value;

   void *ptr;

} Sample;

and i am this declaring the structure as a member in the <module>_<vendor>_obj this structure

 

typedef struct <module>_<vendor>_obj

{

        IALG_Obj alg;

       Sample *st_ptr;

}<module>_<vendor>_obj;

after declaring the structure as a member in the <module>_<vendor>_Obj then i am assigning the memory in the <module>_<vendor>_alloc() function like 

<module>_<vendor>_alloc(const IALG *algParams, IALG_Fxns **pf, IALG_MemRec memTab[])

{

   memTab[0] = sizeof(<module>_<vendor>_obj);

   memTab[1] = sizeof(Sample *);

}

 

after defining the memory i am assigning the memory in the initObj() function

<module>_<vendor>_initObj()

{

    <module>_<vendor>_Obj *obj = (<module>_<vendor>_Obj *)handle;

    obj->st_ptr = memTab[1].base;

}

 

 

My Question :

1. What the procedure i followed is correct ? 

2. can i declare memory for a structure pointer which i have shown above ?

 

Please help me

 

Thank you,

Raju

 

 

 

 

  • Hi Raju,

    The code snippet you sketched above has an issue with the allocation of the memory for the Sample structure. In your IALG::algAlloc function, the 'size' of memTab[1] structure should be the size of the Sample structure. You are incorrectly setting that to the size of a pointer-to-Sample-structure.

     If you changed that to something like:

    <module>_<vendor>_alloc(const IALG *algParams, IALG_Fxns **pf, IALG_MemRec memTab[])
    {
       . . .
       memTab[0].size = sizeof(<module>_<vendor>_obj);
       . . .
       memTab[1].size = sizeof(Sample) * numSamples; /* numSamples is defined by your algo, this is the number of Samples */

    }

    But even this does not allocate memory for the 'ptr' array that you reference within the Sample struct. I am not sure what you are trying to do, but if you need to allocate an array for the 'ptr', that should also be requested by algAlloc(). For example:

    <module>_<vendor>_alloc(const IALG *algParams, IALG_Fxns **pf, IALG_MemRec memTab[])
    {
       . . .
       memTab[0].size = sizeof(<module>_<vendor>_obj);
       . . .
       memTab[1].size = sizeof(Sample);
       memTab[2].size = sizeof(<whatever-size-of-ptr-array should be>);

    }

    Using this allocation logic, inside your algInit() code, you can initialize the 'ptr' field to the memory allocated via memTab[2]. E.g. :

       obj->st_ptr         = memTab[1].base;
       obj->st_ptr->ptr = memTab[2].base;

    Hope this helps,

    Murat

  • Thank you.

     

    i Have one more doubt, can we declare a self referential structure ?

     

     

    Raju.

  • Raju,

    Not exactly sure what your question is, but in general yes. You can declare self-referential structures.

    Murat

  • Hi Murat,

     

    I am having the structure 

    typedef struct <module>_<vendor>_Obj

    {

         IALG_Obj alg;

         /* declared some variables in this */

        coder codec;

    }

    i am declaring another structure 

    struct coder

    {

      /* declared some variables */

        struct coder *left;

    };

    typedef struct coder coder;

     

    Problem:

    after declaring the structures i am getting the error  

    "error : identifier coder is undefined".

     

    Thank you.

    Raju

     

     

     

     

  • Your declarations above should be fine, however you need to declare your typedef struct __Obj declaration *after* the struct coder and typdef coder declarations. In standard C the order of declarations matter. Best regards, Murat