Please Explain This behaviour...,

Discussion in 'C Programming' started by Pranav, Sep 5, 2008.

  1. Pranav

    Pranav Guest

    The Code is compiling without Error/Bug/Exception..,
    What are the possibilities for this behaviour?
    //***************************************************************
    #include<stdio.h>

    typedef struct abc_ *abc;

    static abc param;

    abc fun(void)
    {
    float *a = NULL;
    a = (float *)malloc(sizeof(float));
    if (a) {
    return ((abc)(a));
    }
    return NULL;
    }

    int main()
    {
    param = fun();
    printf("test_bandwidth_alloc: %x\n", param);
    free(param);
    }
    Pranav, Sep 5, 2008
    #1
    1. Advertising

  2. Pranav

    Guest

    On Sep 5, 9:50 am, Pranav <> wrote:
    > The Code is compiling without Error/Bug/Exception..,
    > What are the possibilities for this behaviour?
    > //***************************************************************
    > #include<stdio.h>
    >
    > typedef struct abc_ *abc;
    >
    > static abc param;
    >
    > abc fun(void)
    > {
    > float *a = NULL;
    > a = (float *)malloc(sizeof(float));
    > if (a) {
    > return ((abc)(a));


    a has type (float *). abc is alias for (struct abc_ *).
    These two types can have different representation and size.

    > }
    > return NULL;
    > }
    >
    > int main()
    > {
    > param = fun();
    > printf("test_bandwidth_alloc: %x\n", param);


    Here you pass a (struct abc_ *) where printf expects unsigned int, and
    you invoke undefined behavior.

    > free(param);
    >
    > }
    , Sep 5, 2008
    #2
    1. Advertising

  3. Pranav

    Pranav Guest

    On Sep 5, 11:59 am, wrote:
    > On Sep 5, 9:50 am, Pranav <> wrote:
    >
    > > The Code is compiling without Error/Bug/Exception..,
    > > What are the possibilities for this behaviour?
    > > //***************************************************************
    > > #include<stdio.h>

    >
    > > typedef struct abc_ *abc;

    >
    > > static abc param;

    >
    > > abc fun(void)
    > > {
    > > float *a = NULL;
    > > a = (float *)malloc(sizeof(float));
    > > if (a) {
    > > return ((abc)(a));

    >
    > a has type (float *). abc is alias for (struct abc_ *).
    > These two types can have different representation and size.
    >
    > > }
    > > return NULL;
    > > }

    >
    > > int main()
    > > {
    > > param = fun();
    > > printf("test_bandwidth_alloc: %x\n", param);

    >
    > Here you pass a (struct abc_ *) where printf expects unsigned int, and
    > you invoke undefined behavior.
    >
    > > free(param);

    >
    > > }


    But there is no structure(abc) defined in the whole code then what it
    is aliasing?
    Pranav, Sep 5, 2008
    #3
  4. Pranav

    Guest

    On Sep 5, 10:03 am, Pranav <> wrote:
    <snip>

    > But there is no structure(abc) defined in the whole code then what it
    > is aliasing?


    All pointers to struct have the same size and representation. The
    compiler doesn't need to know what struct abc_ is in order to have a
    pointer to it; The compiler just needs to know it's a struct.
    , Sep 5, 2008
    #4
  5. Pranav

    Pranav Guest

    On Sep 5, 12:04 pm, wrote:
    > On Sep 5, 10:03 am, Pranav <> wrote:
    > <snip>
    >
    > > But there is no structure(abc) defined in the whole code then what it
    > > is aliasing?

    >
    > All pointers to struct have the same size and representation. The
    > compiler doesn't need to know what struct abc_ is in order to have a
    > pointer to it; The compiler just needs to know it's a struct.


    But memory pointed to by the pointer must be known at least If I do
    perform any pointer arithmetic then by what size it gets incremented
    and point to next memory?
    Pranav, Sep 5, 2008
    #5
  6. Pranav

    Guest

    On Sep 5, 10:09 am, Pranav <> wrote:
    > On Sep 5, 12:04 pm, wrote:
    >
    > > On Sep 5, 10:03 am, Pranav <> wrote:
    > > <snip>

    >
    > > > But there is no structure(abc) defined in the whole code then what it
    > > > is aliasing?

    >
    > > All pointers to struct have the same size and representation. The
    > > compiler doesn't need to know what struct abc_ is in order to have a
    > > pointer to it; The compiler just needs to know it's a struct.

    >
    > But memory pointed to by the pointer must be known at least If I do
    > perform any pointer arithmetic then by what size it gets incremented
    > and point to next memory?


    You can't. It's a pointer to incomplete type.
    You also can't perform arithmetic operations on pointers such as:

    int (*foo)[];

    et cetera.
    , Sep 5, 2008
    #6
  7. Pranav

    Pranav Guest

    On Sep 5, 12:17 pm, wrote:
    > On Sep 5, 10:09 am, Pranav <> wrote:
    >
    > > On Sep 5, 12:04 pm, wrote:

    >
    > > > On Sep 5, 10:03 am, Pranav <> wrote:
    > > > <snip>

    >
    > > > > But there is no structure(abc) defined in the whole code then what it
    > > > > is aliasing?

    >
    > > > All pointers to struct have the same size and representation. The
    > > > compiler doesn't need to know what struct abc_ is in order to have a
    > > > pointer to it; The compiler just needs to know it's a struct.

    >
    > > But memory pointed to by the pointer must be known at least If I do
    > > perform any pointer arithmetic then by what size it gets incremented
    > > and point to next memory?

    >
    > You can't. It's a pointer to incomplete type.
    > You also can't perform arithmetic operations on pointers such as:
    >
    > int (*foo)[];
    >
    > et cetera.


    We can increment the pointers and decrement and subtract.., And I said
    pointer arithmetic not arithmetic operations on pointers.,
    Pranav, Sep 5, 2008
    #7
  8. Pranav

    viza Guest

    On Fri, 05 Sep 2008 00:22:53 -0700, Pranav wrote:

    > On Sep 5, 12:17 pm, wrote:
    >> On Sep 5, 10:09 am, Pranav <> wrote:
    >> > On Sep 5, 12:04 pm, wrote:
    >> > > On Sep 5, 10:03 am, Pranav <> wrote: <snip>

    >>
    >> > > > But there is no structure(abc) defined in the whole code then
    >> > > > what it is aliasing?

    >>
    >> > > All pointers to struct have the same size and representation. The
    >> > > compiler doesn't need to know what struct abc_ is in order to have
    >> > > a pointer to it; The compiler just needs to know it's a struct.

    >>
    >> > But memory pointed to by the pointer must be known at least If I do
    >> > perform any pointer arithmetic then by what size it gets incremented
    >> > and point to next memory?

    >>
    >> You can't. It's a pointer to incomplete type. You also can't perform
    >> arithmetic operations on pointers such as:
    >>
    >> int (*foo)[];
    >>
    >> et cetera.

    >
    > We can increment the pointers and decrement and subtract.., And I said
    > pointer arithmetic not arithmetic operations on pointers.,


    No, you can't - not without defining struct abc_

    typedef struct foo *my_type;

    my_type pointer= WHATEVER; /* is allowed */
    pointer+= 4; /* is NOT allowed */

    struct bar {
    int a;
    char b;
    };

    typedef struct bar *other_type;

    other_type other_pointer= WHATEVER; /* is allowed */
    other_pointer+= 4; /* is allowed */
    viza, Sep 5, 2008
    #8
  9. Pranav wrote:
    > The Code is compiling without Error/Bug/Exception..,
    > What are the possibilities for this behaviour?


    What behavior were you expecting? Why?
    I have cleaned up your code a little. Note that the return statement in
    fun() is not illegal, just stupid.

    #include<stdio.h>
    #include <stdlib.h> /* mha: added, otherwise malloc is
    assumed to return an int, which it
    does not do. */

    typedef struct abc_ *abc;

    static abc param;

    abc fun(void)
    {
    float *a = NULL;
    a = malloc(sizeof *a); /* mha: removed stupid cast and fixed
    poor style in the argment */
    if (a)
    return (abc) a; /* mha: note that this is very stupid */
    return NULL;
    }

    int main()
    {
    param = fun();
    /* mha: fixed printf specifier _and_ argument below */
    printf("test_bandwidth_alloc: %p\n", (void *) param);
    free(param);
    }
    Martin Ambuhl, Sep 5, 2008
    #9
  10. Pranav

    Pranav Guest

    On Sep 5, 12:45 pm, Richard Heathfield <> wrote:
    > Pranav said:
    >
    > > The Code is compiling without Error/Bug/Exception..,
    > > What are the possibilities for this behaviour?
    > > //***************************************************************
    > > #include<stdio.h>

    >
    > > typedef struct abc_ *abc;

    >
    > > static abc param;

    >
    > > abc fun(void)
    > > {
    > > float *a = NULL;
    > > a = (float *)malloc(sizeof(float));

    >
    > Because you failed to #include <stdlib.h> you have failed to provide a
    > declaration for malloc.
    >
    > Because there is no declaration for malloc, the compiler is obliged to
    > assume that malloc returns int, even though we all know it really returns
    > void *.
    >
    > Implementations can legitimately generate code that can retrieve an int
    > return value from a different register than a pointer return value. So,
    > for example, malloc could place its pointer in a given register, but the
    > calling code - generated on the we-know-it's-false but obligatory
    > assumption that malloc returns int - could fetch the value from a
    > different register, with hilarious results. (That's just one scenario of
    > how things can go wrong here - there are others.)
    >
    > Because of this danger, the compiler is obliged to issue a diagnostic
    > message when you screw up types this badly - but if you explicitly insist
    > (via the cast) that the compiler should just do it, the obligation to warn
    > you is removed.
    >
    > From this point on, the behaviour of the code is undefined, and anything
    > can happen. And that may well be why you are getting the behaviour you
    > observe. Or, of course, it may not. That's what is so exciting about
    > undefined behaviour.
    >
    > --
    > Richard Heathfield <http://www.cpax.org.uk>
    > Email: -http://www. +rjh@
    > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    > "Usenet is a strange place" - dmr 29 July 1999


    I didn't get..., Why it would compile in the first place if this is
    the problem? And also it is not possible to overload the functions in
    C, In same header file declaration, And from where did it get its
    declaration? and according to C standard malloc return a generic
    pointer to the requested size of bytes memory.
    (I am using DevC++ compiler for testing)
    Pranav, Sep 5, 2008
    #10
  11. Pranav

    Pranav Guest

    static abc param;

    Why The Above line also did not generate exception?
    Pranav, Sep 5, 2008
    #11
  12. Pranav

    Pranav Guest

    Sorry I got the BUG..., It is specifically related to compiler..,
    Pranav, Sep 5, 2008
    #12
  13. Pranav

    John Bode Guest

    On Sep 5, 7:50 am, Pranav <> wrote:
    > static abc param;
    >
    > Why The Above line also did not generate exception?


    That line is equivalent to

    static struct abc_ *param;

    As several people have tried to explain, it's perfectly legal to
    declare a pointer to an incomplete struct type. All pointers to
    structs have the same size, regardless of the size of the struct type
    itself, so you can create a pointer to a struct before you know the
    struct definition.
    John Bode, Sep 5, 2008
    #13
  14. Pranav

    Richard Bos Guest

    Pranav <> wrote:

    > Sorry I got the BUG..., It is specifically related to compiler..,


    Given the "quality" of the code you've posted here, I would not blame
    any compiler at this point, if I were you.

    Richard
    Richard Bos, Sep 8, 2008
    #14
  15. Pranav

    Default User Guest

    Richard Bos wrote:

    > Pranav <> wrote:
    >
    > > Sorry I got the BUG..., It is specifically related to compiler..,

    >
    > Given the "quality" of the code you've posted here, I would not blame
    > any compiler at this point, if I were you.


    Even compilers get frustrated?




    Brian
    Default User, Sep 8, 2008
    #15
    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. priyom
    Replies:
    5
    Views:
    411
    priyom
    Nov 25, 2006
  2. Replies:
    4
    Views:
    353
    Dave Thompson
    Mar 27, 2006
  3. Train Bwister
    Replies:
    1
    Views:
    268
    Diez B. Roggisch
    Apr 30, 2009
  4. Tim Chase
    Replies:
    3
    Views:
    287
    John Posner
    Apr 30, 2009
  5. Jeremy Henty
    Replies:
    7
    Views:
    105
    Jeremy Henty
    Jun 5, 2006
Loading...

Share This Page