array a[(foo)()] (help)

Discussion in 'C Programming' started by noname_is_me, Sep 16, 2004.

  1. noname_is_me

    noname_is_me Guest

    int (*foo)(void);
    int bar(void)
    {
    unsigned int a = 10;
    foo = &bar;
    return a;
    }


    int main(void)
    {
    unsigned int a[(foo)()];
    printf("%d\n",sizeof a);
    return 0;
    }
    gcc does not give any compile time error. But the program crashes. why?
    Array 'a' can not
    be decleared with size 10 * 4 size? Please help.
     
    noname_is_me, Sep 16, 2004
    #1
    1. Advertising

  2. noname_is_me

    Alex Vinokur Guest

    "noname_is_me" <> wrote in message news:...
    > int (*foo)(void);
    > int bar(void)
    > {
    > unsigned int a = 10;
    > foo = &bar;
    > return a;
    > }
    >
    >
    > int main(void)
    > {

    bar();
    {
    > unsigned int a[(foo)()];
    > printf("%d\n",sizeof a);

    }
    > return 0;
    > }
    > gcc does not give any compile time error. But the program crashes. why?
    > Array 'a' can not
    > be decleared with size 10 * 4 size? Please help.
    >


    --
    Alex Vinokur
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Sep 16, 2004
    #2
    1. Advertising

  3. noname_is_me

    -berlin.de Guest

    noname_is_me <> wrote:
    > int (*foo)(void);
    > int bar(void)
    > {
    > unsigned int a = 10;
    > foo = &bar;


    There's no need for the address operator here.

    > return a;
    > }



    > int main(void)
    > {
    > unsigned int a[(foo)()];


    You don't need the parentheses around 'foo'.

    > printf("%d\n",sizeof a);
    > return 0;
    > }
    > gcc does not give any compile time error. But the program crashes. why?


    If you run gcc in c90-compliant mode it's going to warn you about
    variable-size array you're using, which are only allowed in C99. And
    another problem is that in the calculation of the size you use the
    uninitialized pointer 'foo' as if it would point to a function,
    which it doesn't ('foo' would only gets set in function bar(), but
    that is never called).
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Sep 16, 2004
    #3
  4. noname_is_me

    Amit Guest

    noname_is_me wrote:

    > int (*foo)(void);
    > int bar(void)
    > {
    > unsigned int a = 10;
    > foo = &bar;
    > return a;
    > }
    >
    >
    > int main(void)
    > {

    bar(); /* added */
    > unsigned int a[(foo)()];
    > printf("%d\n",sizeof a);
    > return 0;
    > }
    > gcc does not give any compile time error. But the program crashes. why?
    > Array 'a' can not
    > be decleared with size 10 * 4 size? Please help.
    >


    I am a beginner in C so i am not sure if i am right or wrong.
    When the main() is called,at that time foo doesnt have the address of
    bar(),so obviously it will try to go to some garbage value contained in
    foo and try to execute it and thus results in a crash.
    As for gcc,execute bar function before your declaration.

    As ANSI C doesn't allow "variable-size array".Moreover the solution here
    is also not ANSI C complaint.It gives error: ISO C89 forbids mixed
    declarations and code.
     
    Amit, Sep 16, 2004
    #4
  5. noname_is_me

    Richard Bos Guest

    noname_is_me <> wrote:

    > int (*foo)(void);
    > int bar(void)
    > {
    > unsigned int a = 10;
    > foo = &bar;
    > return a;
    > }
    >
    > int main(void)
    > {
    > unsigned int a[(foo)()];
    > printf("%d\n",sizeof a);
    > return 0;
    > }
    > gcc does not give any compile time error. But the program crashes. why?
    > Array 'a' can not be decleared with size 10 * 4 size?


    Array a has nothing to do with it. You're calling an uninitialised
    function pointer; foo is never given a value.

    Richard
     
    Richard Bos, Sep 16, 2004
    #5
  6. noname_is_me

    Richard Bos Guest

    Amit <> wrote:

    > noname_is_me wrote:
    >
    > > int main(void)
    > > {

    > bar(); /* added */
    > > unsigned int a[(foo)()];
    > > printf("%d\n",sizeof a);
    > > return 0;


    > When the main() is called,at that time foo doesnt have the address of
    > bar(),so obviously it will try to go to some garbage value contained in
    > foo and try to execute it and thus results in a crash.


    This is right.

    > As ANSI C doesn't allow "variable-size array".Moreover the solution here
    > is also not ANSI C complaint.It gives error: ISO C89 forbids mixed
    > declarations and code.


    This is wrong. ANSI C, these days, is ISO C99; at least, I'd be very
    surprised if ANSI had not ratified C99. ANSI C89 and ISO C90 did not
    allow the things you mention, that's true.

    Richard
     
    Richard Bos, Sep 16, 2004
    #6
  7. noname_is_me

    noname_is_me Guest

    Alex Vinokur wrote:

    >"noname_is_me" <> wrote in message news:...
    >
    >
    >>int (*foo)(void);
    >>int bar(void)
    >>{
    >> unsigned int a = 10;
    >> foo = &bar;
    >> return a;
    >>}
    >>
    >>
    >>int main(void)
    >>{
    >>
    >>

    > bar();
    > {
    >

    I must call the function bar() since it initializes the function pointer
    foo. Am I correct?

    >
    >
    >> unsigned int a[(foo)()];
    >> printf("%d\n",sizeof a);
    >>
    >>

    > }
    >
    >
    >> return 0;
    >>}
    >>gcc does not give any compile time error. But the program crashes. why?
    >>Array 'a' can not
    >>be decleared with size 10 * 4 size? Please help.
    >>
    >>
    >>

    >
    >
    >
     
    noname_is_me, Sep 16, 2004
    #7
  8. noname_is_me

    Alex Vinokur Guest

    "noname_is_me" <> wrote in message news:...
    >
    >
    > Alex Vinokur wrote:
    >
    > >"noname_is_me" <> wrote in message news:...
    > >
    > >
    > >>int (*foo)(void);
    > >>int bar(void)
    > >>{
    > >> unsigned int a = 10;
    > >> foo = &bar;
    > >> return a;
    > >>}
    > >>
    > >>
    > >>int main(void)
    > >>{
    > >>
    > >>

    > > bar();
    > > {
    > >

    > I must call the function bar() since it initializes the function pointer
    > foo. Am I correct?


    Yes, you are correct.

    >
    > >
    > >
    > >> unsigned int a[(foo)()];
    > >> printf("%d\n",sizeof a);
    > >>
    > >>

    > > }
    > >
    > >
    > >> return 0;
    > >>}
    > >>gcc does not give any compile time error. But the program crashes. why?
    > >>Array 'a' can not
    > >>be decleared with size 10 * 4 size? Please help.
    > >>


    --
    Alex Vinokur
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Sep 16, 2004
    #8
  9. noname_is_me

    noname_is_me Guest

    Thanks for all the quick responses
     
    noname_is_me, Sep 16, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Vinokur
    Replies:
    7
    Views:
    456
    Jerry Coffin
    Sep 21, 2003
  2. Alex Vinokur
    Replies:
    4
    Views:
    491
    Ron Natalie
    Sep 24, 2003
  3. Replies:
    3
    Views:
    404
    John Roth
    Jul 29, 2005
  4. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    417
    JaredGrubb
    Sep 24, 2008
  5. Replies:
    4
    Views:
    170
    Thomas 'PointedEars' Lahn
    Dec 23, 2007
Loading...

Share This Page