473,395 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Please Explain This behaviour...,

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);
}
Sep 5 '08 #1
11 1364
On Sep 5, 9:50 am, Pranav <pranav...@gmail.comwrote:
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 '08 #2
On Sep 5, 11:59 am, vipps...@gmail.com wrote:
On Sep 5, 9:50 am, Pranav <pranav...@gmail.comwrote:
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?
Sep 5 '08 #3
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.hyou 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
Sep 5 '08 #4
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);
}
Sep 5 '08 #5
On Sep 5, 12:45 pm, Richard Heathfield <r...@see.sig.invalidwrote:
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.hyou 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)
Sep 5 '08 #6
Pranav said:
On Sep 5, 12:45 pm, Richard Heathfield <r...@see.sig.invalidwrote:
<snip>
>>
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.

I didn't get..., Why it would compile in the first place if this is
the problem?
It isn't *the* problem, merely *a* problem. We often find that our code has
more faults in it than we would like to imagine.

Firstly, there are several ways in which a program can be wrong, but very
few circumstances in which a compiler *must* refuse to translate the
program. If the program contains any syntax errors or constraint
violations, the compiler (or interpreter, of course - I use the term
"compiler" in a very broad sense here) is required to issue at least one
diagnostic message, but it is still allowed to translate the program. In
any case, as far as I can recall, your program did not violate any
constraints or syntax rules. But that doesn't mean it's a correct program!
And also it is not possible to overload the functions in
C,
Well, it would be more correct to say that to attempt to do so results in
an incorrect program.
In same header file declaration, And from where did it get its
declaration?
The compiler *didn't* get a declaration! That's why it was forced to make
one up, according to rules which don't cope well with the situation that
your code introduced. In the absence of a function declaration for a
function that your program calls, the compiler (as I explained earlier) is
obliged to assume that the function in question returns int, even if we
know that it doesn't really - and that is the problem here.
and according to C standard malloc return a generic
pointer to the requested size of bytes memory.
Right (if the call succeeds) - but your failure to declare malloc (by
failing to include <stdlib.h>) means that the compiler doesn't actually
know this.
(I am using DevC++ compiler for testing)
It doesn't matter - C is C. (I assume, perhaps wrongly, that you are
invoking the compiler in C mode, not C++ mode.)

--
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
Sep 5 '08 #7
static abc param;

Why The Above line also did not generate exception?
Sep 5 '08 #8

Sorry I got the BUG..., It is specifically related to compiler..,
Sep 5 '08 #9
On Sep 5, 7:50 am, Pranav <pranav...@gmail.comwrote:
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.
Sep 5 '08 #10
Pranav <pr*******@gmail.comwrote:
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
Sep 8 '08 #11
Richard Bos wrote:
Pranav <pr*******@gmail.comwrote:
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
Sep 8 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Michael Strorm | last post by:
Hi! I posted a message a while back asking for project suggestions, and decided to go with the idea of creating an adventure game (although it was never intended to be a 'proper' game, rather an...
2
by: Abhish | last post by:
HI All ,:) I am a VC++ programmer,and some time My <b>Acumen</b> ask Microsofts VC++ (Visual Studio VC++ 6.0 )complier to complile my <b>senseless programs </b> ! :) <i><b> See what I have...
12
by: Sanjeev | last post by:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3). Please explain why ? //////////////////////////////////////////////// #include<stdio.h> #include<string.h> void main() {
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
22
by: Jaspreet | last post by:
I was recently asked this question in an interview. Unfortunately I was not able to answer it and the interviewer made a decision on my C strengths (or weekness) based on this single question and...
3
by: Aarti | last post by:
Hi, Can some one please explain why the output of this program is 15 #include <iostream> using namespace std; class A {
3
by: sathishc58 | last post by:
Hi All, Here is the code which generates Segmentation Fault. Can anyone explain why the third printf fails and the first printf works? main() { char ch={"Hello"}; char *p; ...
12
by: raghukumar | last post by:
# include <iostream> class A { public: A() : i(1) {} int i; } ; class B: public A { public :
2
by: sathishc58 | last post by:
Hi All Please explain why strlen returns() "16" as output here and explain the o/p for sizeof() as well main() { char a={'a','b','c'}; printf("strlen=%d\n", strlen(a));...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.