Will this work?
 
 struct x{                /* struct of type x defined */
 int a,b,c,d;
 };
 
 struct x *v[2];    /* array of 2 pointers to structures of type x -
somewhere initialized, OK ?*/
 
 /* swap pointers, this means after call to swap(), v[0] holds the
pointer to v[1]   and viceversa */
 void swap(void)    {
 struct x *tmp;    /*  this declaraction should be struct x *tmp , and
not the variable tmp of type struct x !!! */
  *tmp = *v[0];         
  *v[0]= *v[1];
  *v[1]= *tmp;
 }

Garst
> 
Luke Hoffmann wrote:
> 
> Sorry, I'll repost this with HTML off ;-)
> 
> Hi,
> 
> I think Oleg has identified a real problem.
> Lets leave the question of "is this the best way to do it" aside and realise 
> that "this is the best way to reproduce the problem"!
> 
> The swap() function is not swapping pointers it is swapping the contents of 
> structures.
> This is a valid operation and it produces some interesting code!
> Clearly the code attached is wrong.
> 
> Regards
> Luke
> 
> 
> -----Original Message-----
> From: Roberto G. Berner [mailto:booleangene...@ciudad.com.ar]
> Sent: Thursday, 23 January 2003 4:36 AM
> To: mspgcc-users@lists.sourceforge.net
> Subject: RE: [Mspgcc-users] Struct assignment BUG...
> 
> Dear Oleg:
> 
> Accept my apologies, but I think you should check this. If I am wrong, please 
> excuse me.
> 
> 
> struct x{                /* struct of type x defined */
> int a,b,c,d;
> };
> 
> struct x *v[2];    /* array of 2 pointers to structures of type x - somewhere 
> initialized, OK ?*/
> 
> /* swap pointers, this means after call to swap(), v[0] holds the pointer to 
> v[1] and viceversa */
> void swap(void)    {
> struct x tmp;    /*  this declaraction should be struct x *tmp , and not the 
> variable tmp of type struct x !!! */
>  tmp=*v[0];        /* tmp should be a pointer in order to hold pointers 
> properly */
>  *v[0]=*v[1];
>  *v[1]=tmp;
> }
> 
> Kind regards,
> 
> Roberto G. Berner
> booleangene...@ciudad.com.ar
> ven...@increible.homelinux.com
> 4308 3500 tel
> 4308 3700 fax
> 15 5122 6095 cel
> ----- Original Message -----
> From: Oleg Skydan
> To: mspgcc-users@lists.sourceforge.net
> Sent: Wednesday, January 22, 2003 4:27 PM
> Subject: Re: [Mspgcc-users] Struct assignment BUG...
> 
> Dear Roberto,
> 
> The swap function exchanges the contents of the two structs,
> pointed by elements of the array v, so there a significant BUG.
> 
> I have omitted the initialization section for simplicity (I have found this 
> BUG in
> a real program, which is very large).
> 
> Anyway the stack pointer should point to the same location at the function
> exit and entry (unless it is explicitly changed, using asm statement for 
> example).
> 
> Thanks,
> Oleg.
> ----- Original Message -----
> From: Roberto G. Berner
> To: mspgcc-users@lists.sourceforge.net
> Sent: Wednesday, January 22, 2003 6:25 PM
> Subject: RE: [Mspgcc-users] Struct assignment BUG...
> 
> Dear Oleg:
> 
> I think that there is no bug. Should I suggest trying ...
> 
> struct x *tmp;
> 
> Kind regards,
> 
> Roberto G. Berner
> booleangene...@ciudad.com.ar
> ven...@increible.homelinux.com
> 4308 3500 tel
> 4308 3700 fax
> 15 5122 6095 cel
> ----- Original Message -----
> From: Oleg Skydan
> To: MSP430 GCC
> Sent: Wednesday, January 22, 2003 12:12 PM
> Subject: [Mspgcc-users] Struct assignment BUG...
> 
> Hello, All !
> 
> I found a significant BUG when using struct assignment.
> 
> Here is the sample:
> 
> struct x{
> int a,b,c,d;
> };
> 
> struct x *v[2];
> 
> void swap(void)
> {
> struct x tmp;
>  tmp=*v[0];
>  *v[0]=*v[1];
>  *v[1]=tmp;
> }
> 
> And here is GCC's listing (with -O option):
> 
> /***********************
>  * Function `swap'
>  ***********************/
> swap:
> /* prologue: frame size = 8 */
> .L__FrameSize_swap=0x8
> .L__FrameOffset_swap=0x8
>  sub #8, r1 ; 8, fpn 0
> /* prologue end (size=1) */
>  mov &v, r15
>  mov @r15+, @r1
>  mov @r15+, 2(r1)
>  mov @r15+, 4(r1)
>  mov @r15+, 6(r1)
>  mov &v+2, r14
>  mov @r14+, 0(r15)
>  mov @r14+, 2(r15)
>  mov @r14+, 4(r15)
>  mov @r14+, 6(r15)
>  mov &v+2, r15
>  mov @r1+, 0(r15)       ;We should not change the stack pointer here !
>  mov @r1+, 2(r15)
>  mov @r1+, 4(r15)
>  mov @r1+, 6(r15)
> /* epilogue: frame size=8 */
>  add #8, r1                    ;After this command the stack pointer points 
> to the illegal location !
>  ret
> /* epilogue end (size=2) */
> /* function swap size 33 (30) */
> 
> I use win32 version (from 10.12.2002).
> Thanks, Oleg.
> 
> 
>

Reply via email to