DSPRelated.com
Forums

Structure dereferencing in C

Started by Ashish Gupta June 7, 2008
Hi,
I am writing code for implementing circular buffer in C. I want to have the
same functionality as we get in C55x assembly by using circular addressing
of AR0-AR7. For that i used structure of double linked list. But when i am
derefencing the structure by using a pointer with dereferencing arrow then
it is not dereferencing properly. then i tried derferencing using structure
variable name and then using (.) to point to the element of the structure
variable then it is happening properly.
I want to know that is there any good way of implementing circular buffer .
And can i deference the structure in CCS by using pointers.

Regards,
Ashish
Could you provide some sample code for what you are trying to do and the
ways you have tried?
It's easier to understand and thelp then =)

-Sima

On Sat, Jun 7, 2008 at 12:14 PM, Ashish Gupta
wrote:

> Hi,
> I am writing code for implementing circular buffer in C. I want to have the
> same functionality as we get in C55x assembly by using circular addressing
> of AR0-AR7. For that i used structure of double linked list. But when i am
> derefencing the structure by using a pointer with dereferencing arrow then
> it is not dereferencing properly. then i tried derferencing using structure
> variable name and then using (.) to point to the element of the structure
> variable then it is happening properly.
> I want to know that is there any good way of implementing circular buffer .
> And can i deference the structure in CCS by using pointers.
>
> Regards,
> Ashish
>
>
Hello,
I am attaching the sample code for your reference. I have defined a
structure of double linked list for implementing a circular buffer which
points to both forward and backward. If i dereference the structure using
structure name and (.) then it derefences properly .But if i define a ptr
to the structure and then try to dereference the same by using ptr and then
the -> then it doesn't get the correct result. the same function works in C
but there seems to be problem with the same in CCS. If anybody can guide me
how to implement circular buffer in C in any other way then it is welcome. I
have implemented the circular buffer in assembly by defining AR3 as
circular and defining BK and BSA registers. I want to know that can i link
the same assembly code in C. But if C program uses the same AR3 register
then won;'t it confict with the circular buffer implementation in assembly.

The C code is produced below:
struct cir_buf{
short sample;
short index;
struct cir_buf *next;
struct cir_buf *prev;
};
struct cir_buf start_cir_buf[20],*ptr_2_cir_buf,*temp,*first,*last;
int *t1;
void create_cir_buf(void)
{
int i= 0,j=0;

ptr_2_cir_buf=(struct cir_buf *)&start_cir_buf[0];

start_cir_buf[0].next=&start_cir_buf[1];

for(i = 1;i<19;i++)
{
start_cir_buf[i].index = i;

start_cir_buf[i].next=&start_cir_buf[i+1];
start_cir_buf[i].prev=&start_cir_buf[i-1];
ptr_2_cir_buf = ptr_2_cir_buf->next; //NOT WORKING .. THIS works in C
but not in CCS.
//without this the linked list can't be used. Can u pl guide me how
to use. I have
//seen the the corresponding assembly code in "mixed mode" option. In
that, it is using T1
//to point to the next pointer but T1 is not defined anywhere.
}

start_cir_buf[i].next=&start_cir_buf[0];
start_cir_buf[i].prev=&start_cir_buf[i-1];
start_cir_buf[0].prev=&start_cir_buf[i];

ptr_2_cir_buf=&start_cir_buf[0];

}

Thanks.
Ashish Gupta

On 6/9/08, Sima Baymani wrote:
>
> Could you provide some sample code for what you are trying to do and the
> ways you have tried?
> It's easier to understand and thelp then =)
>
> -Sima
>
> On Sat, Jun 7, 2008 at 12:14 PM, Ashish Gupta
> wrote:
>
>> Hi,
>> I am writing code for implementing circular buffer in C. I want to have
>> the same functionality as we get in C55x assembly by using circular
>> addressing of AR0-AR7. For that i used structure of double linked list. But
>> when i am derefencing the structure by using a pointer with dereferencing
>> arrow then it is not dereferencing properly. then i tried derferencing using
>> structure variable name and then using (.) to point to the element of the
>> structure variable then it is happening properly.
>> I want to know that is there any good way of implementing circular buffer
>> . And can i deference the structure in CCS by using pointers.
>>
>> Regards,
>> Ashish
>
Do you get any message from the CCS-compiler when compiling this code? That
would help a lot in finding what the problem is.
You can turn on warnings in the Build Options menu for the project, if I
recall correctly.
Also, you can analyse the value that your pointer gets. By looking at what
it's value is you can see where it actually points, which might help in
explaining and understanding why your code doesn't work.

Code-wise you could try to dereference the structure pointer before using
it:
ptr_2_cir_buf = (*ptr_2_cir_buf).next;

that imitates the code that works, and is maybe enough to get that part to
work as well.

Hope this helps and good luck!

-Sima

On Thu, Jun 12, 2008 at 1:18 PM, Ashish Gupta
wrote:

> Hello,
> I am attaching the sample code for your reference. I have defined a
> structure of double linked list for implementing a circular buffer which
> points to both forward and backward. If i dereference the structure using
> structure name and (.) then it derefences properly .But if i define a ptr
> to the structure and then try to dereference the same by using ptr and then
> the -> then it doesn't get the correct result. the same function works in C
> but there seems to be problem with the same in CCS. If anybody can guide me
> how to implement circular buffer in C in any other way then it is welcome.
> I
> have implemented the circular buffer in assembly by defining AR3 as
> circular and defining BK and BSA registers. I want to know that can i link
> the same assembly code in C. But if C program uses the same AR3 register
> then won;'t it confict with the circular buffer implementation in assembly.
>
> The C code is produced below:
>
> struct cir_buf{
> short sample;
> short index;
> struct cir_buf *next;
> struct cir_buf *prev;
> };
>
> struct cir_buf start_cir_buf[20],*ptr_2_cir_buf,*temp,*first,*last;
> int *t1;
> void create_cir_buf(void)
> {
> int i= 0,j=0;
>
> ptr_2_cir_buf=(struct cir_buf *)&start_cir_buf[0];
>
> start_cir_buf[0].next=&start_cir_buf[1];
>
> for(i = 1;i<19;i++)
> {
> start_cir_buf[i].index = i;
>
> start_cir_buf[i].next=&start_cir_buf[i+1];
> start_cir_buf[i].prev=&start_cir_buf[i-1];
> ptr_2_cir_buf = ptr_2_cir_buf->next; //NOT WORKING .. THIS works in C
> but not in CCS.
> //without this the linked list can't be used. Can u pl guide me how
> to use. I have
> //seen the the corresponding assembly code in "mixed mode" option. In
> that, it is using T1
> //to point to the next pointer but T1 is not defined anywhere.
> }
>
> start_cir_buf[i].next=&start_cir_buf[0];
> start_cir_buf[i].prev=&start_cir_buf[i-1];
> start_cir_buf[0].prev=&start_cir_buf[i];
>
> ptr_2_cir_buf=&start_cir_buf[0];
>
> }
>
> Thanks.
> Ashish Gupta
>
> On 6/9/08, Sima Baymani > wrote:
> >
> > Could you provide some sample code for what you are trying to do and the
> > ways you have tried?
> > It's easier to understand and thelp then =)
> >
> > -Sima
> >
> > On Sat, Jun 7, 2008 at 12:14 PM, Ashish Gupta
> >
> > wrote:
> >
> >> Hi,
> >> I am writing code for implementing circular buffer in C. I want to have
> >> the same functionality as we get in C55x assembly by using circular
> >> addressing of AR0-AR7. For that i used structure of double linked list.
> But
> >> when i am derefencing the structure by using a pointer with
> dereferencing
> >> arrow then it is not dereferencing properly. then i tried derferencing
> using
> >> structure variable name and then using (.) to point to the element of
> the
> >> structure variable then it is happening properly.
> >> I want to know that is there any good way of implementing circular
> buffer
> >> . And can i deference the structure in CCS by using pointers.
> >>
> >> Regards,
> >> Ashish
> >>
> >
> >
> >
>
Hello Sima,
thanks for the reply . I have tried using ptr_2_cir_buf *(ptr_2_cir_buf).next in the beginning itself. But it doens;t work. It works
only when i use structure name. Is there anyway in which i can use C55x DSP
Circular addressing in assembly and call that function in C. But my concern
is that if i use any auxiliary register as circular buffer in assembly ,say
AR0 then it should not be used by any function in C so that its contents are
not modified. OR it there any other better way of implementing circular
buffer.
can u pl suggest.

Regards,
Ashish
On 6/12/08, Sima Baymani wrote:
>
> Do you get any message from the CCS-compiler when compiling this code?
> That would help a lot in finding what the problem is.
> You can turn on warnings in the Build Options menu for the project, if I
> recall correctly.
> Also, you can analyse the value that your pointer gets. By looking at what
> it's value is you can see where it actually points, which might help in
> explaining and understanding why your code doesn't work.
>
> Code-wise you could try to dereference the structure pointer before using
> it:
> ptr_2_cir_buf = (*ptr_2_cir_buf).next;
>
> that imitates the code that works, and is maybe enough to get that part to
> work as well.
>
> Hope this helps and good luck!
>
> -Sima
>
> On Thu, Jun 12, 2008 at 1:18 PM, Ashish Gupta
> wrote:
>
>> Hello,
>> I am attaching the sample code for your reference. I have defined a
>> structure of double linked list for implementing a circular buffer which
>> points to both forward and backward. If i dereference the structure using
>> structure name and (.) then it derefences properly .But if i define a ptr
>> to the structure and then try to dereference the same by using ptr and
>> then
>> the -> then it doesn't get the correct result. the same function works in
>> C
>> but there seems to be problem with the same in CCS. If anybody can guide
>> me
>> how to implement circular buffer in C in any other way then it is welcome.
>> I
>> have implemented the circular buffer in assembly by defining AR3 as
>> circular and defining BK and BSA registers. I want to know that can i link
>> the same assembly code in C. But if C program uses the same AR3 register
>> then won;'t it confict with the circular buffer implementation in
>> assembly.
>>
>> The C code is produced below:
>>
>> struct cir_buf{
>> short sample;
>> short index;
>> struct cir_buf *next;
>> struct cir_buf *prev;
>> };
>>
>> struct cir_buf start_cir_buf[20],*ptr_2_cir_buf,*temp,*first,*last;
>> int *t1;
>> void create_cir_buf(void)
>> {
>> int i= 0,j=0;
>>
>> ptr_2_cir_buf=(struct cir_buf *)&start_cir_buf[0];
>>
>> start_cir_buf[0].next=&start_cir_buf[1];
>>
>> for(i = 1;i<19;i++)
>> {
>> start_cir_buf[i].index = i;
>>
>> start_cir_buf[i].next=&start_cir_buf[i+1];
>> start_cir_buf[i].prev=&start_cir_buf[i-1];
>> ptr_2_cir_buf = ptr_2_cir_buf->next; //NOT WORKING .. THIS works in C
>> but not in CCS.
>> //without this the linked list can't be used. Can u pl guide me how
>> to use. I have
>> //seen the the corresponding assembly code in "mixed mode" option. In
>> that, it is using T1
>> //to point to the next pointer but T1 is not defined anywhere.
>> }
>>
>> start_cir_buf[i].next=&start_cir_buf[0];
>> start_cir_buf[i].prev=&start_cir_buf[i-1];
>> start_cir_buf[0].prev=&start_cir_buf[i];
>>
>> ptr_2_cir_buf=&start_cir_buf[0];
>>
>> }
>>
>> Thanks.
>> Ashish Gupta
>>
>> On 6/9/08, Sima Baymani > wrote:
>> >
>> > Could you provide some sample code for what you are trying to do and the
>> > ways you have tried?
>> > It's easier to understand and thelp then =)
>> >
>> > -Sima
>> >
>> > On Sat, Jun 7, 2008 at 12:14 PM, Ashish Gupta
>> >
>> > wrote:
>> >
>> >> Hi,
>> >> I am writing code for implementing circular buffer in C. I want to have
>> >> the same functionality as we get in C55x assembly by using circular
>> >> addressing of AR0-AR7. For that i used structure of double linked list.
>> But
>> >> when i am derefencing the structure by using a pointer with
>> dereferencing
>> >> arrow then it is not dereferencing properly. then i tried derferencing
>> using
>> >> structure variable name and then using (.) to point to the element of
>> the
>> >> structure variable then it is happening properly.
>> >> I want to know that is there any good way of implementing circular
>> buffer
>> >> . And can i deference the structure in CCS by using pointers.
>> >>
>> >> Regards,
>> >> Ashish
>> >>
>> >
>> >
>> >
Ashish-

> thanks for the reply . I have tried using ptr_2_cir_buf > *(ptr_2_cir_buf).next in the beginning itself. But it doens;t work. It works
> only when i use structure name. Is there anyway in which i can use C55x DSP
> Circular addressing in assembly and call that function in C. But my concern
> is that if i use any auxiliary register as circular buffer in assembly ,say
> AR0 then it should not be used by any function in C so that its contents are
> not modified. OR it there any other better way of implementing circular
> buffer.
> can u pl suggest.

Yes you could implement the circular buffer in C55x asm lang, and still allow C code
accessibility. But if you do that, then it becomes inefficient to make a function
call just to do something like increment a pointer. The main idea behind circular
addressing capability in DSPs is that if you write highly optimized asm lang code,
for example an FFT function, then circular addressing can save some cycles and make
that code even more optimized. But that assumes the _whole function_ is in asm,
rather than the main function being in C code and just the increment being in asm
language.

If you are writing C code, then it makes more sense to create an increment function,
for example:

void inc_circ(int* buffer, int** ptr) { // pseudo-code, not tested

int offset = (int)(*ptr - buffer);
offset++;
offset &= (N-1); // N is buffer size, must be power-of-2
*ptr = buffer + offset;
}

You could make inc_circ() a C-callable asm function if you want, but you wouldn't
save many MIPS / cycles.

Does that make sense?

-Jeff
> On 6/12/08, Sima Baymani wrote:
> >
> > Do you get any message from the CCS-compiler when compiling this code?
> > That would help a lot in finding what the problem is.
> > You can turn on warnings in the Build Options menu for the project, if I
> > recall correctly.
> > Also, you can analyse the value that your pointer gets. By looking at what
> > it's value is you can see where it actually points, which might help in
> > explaining and understanding why your code doesn't work.
> >
> > Code-wise you could try to dereference the structure pointer before using
> > it:
> > ptr_2_cir_buf = (*ptr_2_cir_buf).next;
> >
> > that imitates the code that works, and is maybe enough to get that part to
> > work as well.
> >
> > Hope this helps and good luck!
> >
> > -Sima
> >
> > On Thu, Jun 12, 2008 at 1:18 PM, Ashish Gupta
> > wrote:
> >
> >> Hello,
> >> I am attaching the sample code for your reference. I have defined a
> >> structure of double linked list for implementing a circular buffer which
> >> points to both forward and backward. If i dereference the structure using
> >> structure name and (.) then it derefences properly .But if i define a ptr
> >> to the structure and then try to dereference the same by using ptr and
> >> then
> >> the -> then it doesn't get the correct result. the same function works in
> >> C
> >> but there seems to be problem with the same in CCS. If anybody can guide
> >> me
> >> how to implement circular buffer in C in any other way then it is welcome.
> >> I
> >> have implemented the circular buffer in assembly by defining AR3 as
> >> circular and defining BK and BSA registers. I want to know that can i link
> >> the same assembly code in C. But if C program uses the same AR3 register
> >> then won;'t it confict with the circular buffer implementation in
> >> assembly.
> >>
> >> The C code is produced below:
> >>
> >> struct cir_buf{
> >> short sample;
> >> short index;
> >> struct cir_buf *next;
> >> struct cir_buf *prev;
> >> };
> >>
> >> struct cir_buf start_cir_buf[20],*ptr_2_cir_buf,*temp,*first,*last;
> >> int *t1;
> >> void create_cir_buf(void)
> >> {
> >> int i= 0,j=0;
> >>
> >> ptr_2_cir_buf=(struct cir_buf *)&start_cir_buf[0];
> >>
> >> start_cir_buf[0].next=&start_cir_buf[1];
> >>
> >> for(i = 1;i<19;i++)
> >> {
> >> start_cir_buf[i].index = i;
> >>
> >> start_cir_buf[i].next=&start_cir_buf[i+1];
> >> start_cir_buf[i].prev=&start_cir_buf[i-1];
> >> ptr_2_cir_buf = ptr_2_cir_buf->next; //NOT WORKING .. THIS works in C
> >> but not in CCS.
> >> //without this the linked list can't be used. Can u pl guide me how
> >> to use. I have
> >> //seen the the corresponding assembly code in "mixed mode" option. In
> >> that, it is using T1
> >> //to point to the next pointer but T1 is not defined anywhere.
> >> }
> >>
> >> start_cir_buf[i].next=&start_cir_buf[0];
> >> start_cir_buf[i].prev=&start_cir_buf[i-1];
> >> start_cir_buf[0].prev=&start_cir_buf[i];
> >>
> >> ptr_2_cir_buf=&start_cir_buf[0];
> >>
> >> }
> >>
> >> Thanks.
> >> Ashish Gupta
> >>
> >> On 6/9/08, Sima Baymani > wrote:
> >> >
> >> > Could you provide some sample code for what you are trying to do and the
> >> > ways you have tried?
> >> > It's easier to understand and thelp then =)
> >> >
> >> > -Sima
> >> >
> >> > On Sat, Jun 7, 2008 at 12:14 PM, Ashish Gupta
> >> >
> >> > wrote:
> >> >
> >> >> Hi,
> >> >> I am writing code for implementing circular buffer in C. I want to have
> >> >> the same functionality as we get in C55x assembly by using circular
> >> >> addressing of AR0-AR7. For that i used structure of double linked list.
> >> But
> >> >> when i am derefencing the structure by using a pointer with
> >> dereferencing
> >> >> arrow then it is not dereferencing properly. then i tried derferencing
> >> using
> >> >> structure variable name and then using (.) to point to the element of
> >> the
> >> >> structure variable then it is happening properly.
> >> >> I want to know that is there any good way of implementing circular
> >> buffer
> >> >> . And can i deference the structure in CCS by using pointers.
> >> >>
> >> >> Regards,
> >> >> Ashish
> >> >>
> >> >
> >> >
> >> >
> >>
> >>
> >
> >
>