Thank you David for your fine explanation. Now I see clearly that is a
compiler's bug, despite the way the test code is written.

Kind regards,

Roberto G. Berner
booleangene...@ciudad.com.ar
4308 3500 tel
4308 3700 fax

----- Original Message -----
From: David Brown <da...@westcontrol.com>
To: <mspgcc-users@lists.sourceforge.net>
Sent: Thursday, January 23, 2003 10:27 AM
Subject: Re: [Mspgcc-users] Struct assignment BUG...


> > Dear Garst,
> >
> > I can see that there are some friends in the list that had found a bug
> from
> > what Oleg has originally posted ( struct declararation and use ). I
would
> > like to respectfully ask this friends what the bug is an which is the
> > interesting code it generates, so I ( and probably someone else ) could
> > learn something new. It is clear that I missed something. Can the
original
> > Oleg's code work fine ? Why ? Thanks in advance.
> >
>
> The array v ( struct x *v[2] ) contains two elements, each of which is a
> pointer to an "x" structure.  I think everyone is clear on that one.
>
> The original code was:
>     void swap(void)
>      {
>          struct x tmp;
>           tmp=*v[0];
>           *v[0]=*v[1];
>           *v[1]=tmp;
>      }
>
> And the suggested change was:
>
>     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;
>      }
>
> In the first case, tmp is declared to be a "struct x".  The structure that
> v[0] points to is copied into tmp.  Then the structure that v[1] points to
> is copied into the space that v[0] points to, and then the tmp structure
is
> copied into the space that v[0] points to.
>
> I think you were trying to suggest that the pointers should be swapped,
not
> the structs they pointed to.  In that case, the code would have been:
>         struct x *tmp;
>         tmp = v[0]; v[0] = v[1]; v[1] = tmp;
> Note that there are no longer dereferencers (the *'s).
> The code you have given will do pretty much the same thing as the original
> code, except that the temporary area is accessed indirectly through a
> pointer tmp, which is not initialised.  In other words, the code will use
a
> random bit of memory somewhere (which may not physically exist) for the
> scratch space, which is not what you want.  The compiler should give you a
> warning on that sort of thing (if not, add the -W and -Wall flags to your
> compiler flags).
>
> mvh.
>
> David
>
>
>
> >
> >
> > > 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.
> > > >
> > > >
> > > >
> > >
> > >
> > > -------------------------------------------------------
> > > This SF.net email is sponsored by: Scholarships for Techies!
> > > Can't afford IT training? All 2003 ictp students receive scholarships.
> > > Get hands-on training in Microsoft, Cisco, Sun, Linux/UNIX, and more.
> > > www.ictp.com/training/sourceforge.asp
> > > _______________________________________________
> > > Mspgcc-users mailing list
> > > Mspgcc-users@lists.sourceforge.net
> > > https://lists.sourceforge.net/lists/listinfo/mspgcc-users
> > >
> > >
> >
> >
> >
> > -------------------------------------------------------
> > This SF.NET email is sponsored by:
> > SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
> > http://www.vasoftware.com
> > _______________________________________________
> > Mspgcc-users mailing list
> > Mspgcc-users@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/mspgcc-users
> >
> >
>
>
>
>
> -------------------------------------------------------
> This SF.NET email is sponsored by:
> SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
> http://www.vasoftware.com
> _______________________________________________
> Mspgcc-users mailing list
> Mspgcc-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/mspgcc-users
>


Reply via email to