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.

static declaration of structure object and pointer in C

Hi all,

One more post I am posting about the passing of a static structure pointer . The code below explains that I am declaring a structure object and a pointer static and initialising the pointer to the object in the ISR. I am then passing the pointer to another function called in the ISR. The function definition is shown below also

/* the structure declaration in a header file included in both the source files*/

struct stl{

float a[10];

float b[10];

};

interrupt void dmax_isr( void )

{static stl fd;

static stl *pt;

compute(pt);

}

void compute(struct stl *a)

{

/* the changes are made here to the elements of the structure using this local pointer *a , */

}

Is this a correct and safe way to pass static pointers? I am unable to find memory leaks but executing this above code with higher input blocks indicates memory leaks.

Please help.

ANushree

  • Hi,

    I suppose You mean:

      static stl* pt=&fd;  //pointer initialization, just missed in the "cut & paste"?

    I think in this way it is safe, even if you can easly drop the pointer off by writing:

      compute(&fd);  //safer: you don't have to initialize the pointer

    Sorry but I don't understand what means "higher input block".

  • hi,

    sorry i forgot to put 

    pt = &fd;

    higher input block means the size of array inside the structure is increased to around 800.

    And my structure is double word aligned thus just sending the object would not be correct i think.

  • anushree mahapatra said:

    higher input block means the size of array inside the structure is increased to around 800.

    Well, this should change nothing in the passing of the reference to fd. Mybe the problem is not in the argument passing itself but  some elsewhere (inside compute?). How do you detect the memory leak and what do you exactly means for "memory leacks"?

    Usually, memory leacks means an error the management of the dynamic memory, such as a misssed deallocation of a dynamicaly allocated memory area, but in your case the memory is static.

    And my structure is double word aligned thus just sending the object would not be correct i think.

    If by "sending the object" you refert to compute(&fd), note that it is exactly the same as compute(pt). In both cases you pass the address of fd

  • Memory leaks also occur when memory is static allocated. When pointers point to wrong memory or something similar to that. I am deducing its memory leak because beyond a certain size of input arrays in the structure, the audio becomes corrupted. How else shall be possible that the output shall be wrong only beyond a certain size of array.

    I am also using DSPLIB which reuqires pointers to be of restrict type.

    thanks. Any help shall be appreciated. 

  • As i have already calculated the CPU load with the current program and its very less around max. 15% and mips used is also considerably low. I have also checked RAM occupied and it shows me still a lot of area to accomodate an increase of 200 values in the input array of the structure. Thus, my suspect has gone to memory leak or corruption.

    anushree

  • Well, regardless of the exact meaning you give to "memory lea" (wikipedia, for instanc, focus on dynamic the memory consumption), returning ot the argument of this thread, IMHO the way you pass the static pointer is, in the laguage point of view, corect.

    The DSPLIB requirements on pointer alignament regards the address of the structure, not the pointer that hold the addres of fd. You can force it: with "#pragma DATA_ALIGN(fd, 8)"

  • I think better not go into DSPLIB details..my variables are local and they cant be aligned in local using the pragma.it works only for global variables.

    Thanks for the reply anyways. and thanks for the advice.

    Regards,

    Anushree

  • anushree mahapatra said:
    variables are local

    Am I to understand you are allocating an 800-byte local structure?  This is fairly large for a local variable; you may be overflowing the stack.  Try increasing the stack size by about 800 bytes, or make this variable a global variable.

  • Hi,

    Mybe You can use STRUCT_ALIGN instead, or force the alig with a trick:

        struct { double force_align; struct stl data } aligned_fd;  .... compute(&aligned_fd.data);

    The field force_align should force the compiler to align the whole struct to double.

  • hi archaeologist,

    Actually it is much more than 800 bytes of local data. One structure is actually having around 12000 bytes of local data currently. Yes, even i am assuming its causing a stack overflow. I shall make it static declared and then check.

    Thanks.

    Anushreee