This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Function returns structure problem in ARM926

Parents
  • Note: This was originally posted on 6th July 2009 at http://forums.arm.com

    reddyr,

    There is no point in making "struct B b;" static i.e. "static struct B b;", as it gets immediately overwritten by the memcpy()s in f3(). Also, given that f2() takes a pointer, I don't see any advantage to the thing it points at being "local" or not. Assuming you were going to remove the memcpy()s from f3() so the value of "b" really was persistent, then better code might be:

    #include <stdio.h>
    #include <string.h>

    struct A
    {
      int a[10];
      int b[10];
      int c[10];
    };

    void f2(struct A *q);
    void f3(void);

    int main(void)
    {
      f3();
      return 0;
    }

    void f3(void)
    {
      int d[9]={1,1,1,1,1,1,1,1,1};
      int i;
      static struct A b;
      memcpy(b.a,d,sizeof d);
      memcpy(b.b,d,sizeof d);
      memcpy(b.c,d,sizeof d);

      printf("Values before function f2()\n\n\n");
      for(i=0;i<9;i++)
    {
       printf("Values In a[i] %d,Values In b[i] %d,Values In c[i] %d\n\n",
       b.a[i],b.b[i],b.c[i]);
    }

      f2(&b);
      printf("Values 1 after function f2()\n\n\n");
      for(i=0;i<9;i++)
    {
       printf("Values In a[i] %d,Values In b[i] %d,Values In c[i] %d\n\n",
       b.a[i],b.b[i],b.c[i]);
    }

      f2(&b);
      printf("Values 1 after function f2()\n\n\n");
      for(i=0;i<9;i++)
    {
       printf("Values In a[i] %d,Values In b[i] %d,Values In c[i] %d\n\n",
       b.a[i],b.b[i],b.c[i]);
    }

    }

    void f2(struct A *q)
    {
      int i;
      for(i=0;i<9;i++)
    {
       q->a[i]=q->a[i]+1;
       q->b[i]=q->b[i]+1;
       q->c[i]=q->c[i]+1;
    }
    }


    hth
    s.
Reply
  • Note: This was originally posted on 6th July 2009 at http://forums.arm.com

    reddyr,

    There is no point in making "struct B b;" static i.e. "static struct B b;", as it gets immediately overwritten by the memcpy()s in f3(). Also, given that f2() takes a pointer, I don't see any advantage to the thing it points at being "local" or not. Assuming you were going to remove the memcpy()s from f3() so the value of "b" really was persistent, then better code might be:

    #include <stdio.h>
    #include <string.h>

    struct A
    {
      int a[10];
      int b[10];
      int c[10];
    };

    void f2(struct A *q);
    void f3(void);

    int main(void)
    {
      f3();
      return 0;
    }

    void f3(void)
    {
      int d[9]={1,1,1,1,1,1,1,1,1};
      int i;
      static struct A b;
      memcpy(b.a,d,sizeof d);
      memcpy(b.b,d,sizeof d);
      memcpy(b.c,d,sizeof d);

      printf("Values before function f2()\n\n\n");
      for(i=0;i<9;i++)
    {
       printf("Values In a[i] %d,Values In b[i] %d,Values In c[i] %d\n\n",
       b.a[i],b.b[i],b.c[i]);
    }

      f2(&b);
      printf("Values 1 after function f2()\n\n\n");
      for(i=0;i<9;i++)
    {
       printf("Values In a[i] %d,Values In b[i] %d,Values In c[i] %d\n\n",
       b.a[i],b.b[i],b.c[i]);
    }

      f2(&b);
      printf("Values 1 after function f2()\n\n\n");
      for(i=0;i<9;i++)
    {
       printf("Values In a[i] %d,Values In b[i] %d,Values In c[i] %d\n\n",
       b.a[i],b.b[i],b.c[i]);
    }

    }

    void f2(struct A *q)
    {
      int i;
      for(i=0;i<9;i++)
    {
       q->a[i]=q->a[i]+1;
       q->b[i]=q->b[i]+1;
       q->c[i]=q->c[i]+1;
    }
    }


    hth
    s.
Children
No data