Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Please Explain This behaviour...,

Reply
Thread Tools

Please Explain This behaviour...,

 
 
Pranav
Guest
Posts: n/a
 
      09-05-2008
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);
}
 
Reply With Quote
 
 
 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      09-05-2008
On Sep 5, 9:50 am, Pranav <(E-Mail Removed)> 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);
>
> }


 
Reply With Quote
 
 
 
 
Pranav
Guest
Posts: n/a
 
      09-05-2008
On Sep 5, 11:59 am, (E-Mail Removed) wrote:
> On Sep 5, 9:50 am, Pranav <(E-Mail Removed)> 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?
 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      09-05-2008
On Sep 5, 10:03 am, Pranav <(E-Mail Removed)> 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.
 
Reply With Quote
 
Pranav
Guest
Posts: n/a
 
      09-05-2008
On Sep 5, 12:04 pm, (E-Mail Removed) wrote:
> On Sep 5, 10:03 am, Pranav <(E-Mail Removed)> 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?
 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      09-05-2008
On Sep 5, 10:09 am, Pranav <(E-Mail Removed)> wrote:
> On Sep 5, 12:04 pm, (E-Mail Removed) wrote:
>
> > On Sep 5, 10:03 am, Pranav <(E-Mail Removed)> 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.
 
Reply With Quote
 
Pranav
Guest
Posts: n/a
 
      09-05-2008
On Sep 5, 12:17 pm, (E-Mail Removed) wrote:
> On Sep 5, 10:09 am, Pranav <(E-Mail Removed)> wrote:
>
> > On Sep 5, 12:04 pm, (E-Mail Removed) wrote:

>
> > > On Sep 5, 10:03 am, Pranav <(E-Mail Removed)> 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.,
 
Reply With Quote
 
viza
Guest
Posts: n/a
 
      09-05-2008
On Fri, 05 Sep 2008 00:22:53 -0700, Pranav wrote:

> On Sep 5, 12:17 pm, (E-Mail Removed) wrote:
>> On Sep 5, 10:09 am, Pranav <(E-Mail Removed)> wrote:
>> > On Sep 5, 12:04 pm, (E-Mail Removed) wrote:
>> > > On Sep 5, 10:03 am, Pranav <(E-Mail Removed)> 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 */
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-05-2008
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);
}
 
Reply With Quote
 
Pranav
Guest
Posts: n/a
 
      09-05-2008
On Sep 5, 12:45 pm, Richard Heathfield <(E-Mail Removed)> 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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please explain this "Why's" example please Kaye Ng Ruby 8 06-08-2010 09:13 AM
70-292: can someone please verify / explain the answer to this question Tim Moor MCSE 7 12-18-2005 03:30 AM
Explain this a little better please. fwells11@hotmail.com Cisco 3 07-31-2005 06:49 AM
Someone please explain to me ... =?Utf-8?B?bXJzLmdyYW50?= Wireless Networking 6 11-25-2004 10:08 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments