473,748 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ba ndwidth_alloc: %x\n", param);
free(param);
}
Sep 5 '08 #1
11 1398
On Sep 5, 9:50 am, Pranav <pranav...@gmai l.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_ba ndwidth_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...@gmai l.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_ba ndwidth_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_ba ndwidth_alloc: %p\n", (void *) param);
free(param);
}
Sep 5 '08 #5
On Sep 5, 12:45 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
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.i nvalidwrote:
<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...@gmai l.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

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

Similar topics

26
2937
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 excuse to write- and learn- some C++). To cut a long story short, I wrote a fair chunk of it, but realised that it's... not very good. Okay, it's my first "proper" C++ program, so that's no big deal, but I don't want to waste more time working...
2
1950
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 asked this time to compile !!..</b></i>
12
3076
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
2626
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
2109
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 that was a sad end to my interview. Here is the program: #include <stdio.h> int main() { char *c ="abc";
3
1400
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
1465
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; p=ch; printf("Character is %c\n", *p);
12
1498
by: raghukumar | last post by:
# include <iostream> class A { public: A() : i(1) {} int i; } ; class B: public A { public :
2
2190
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)); printf("sizeof=%d\n", sizeof(a)); printf("%d %d", strlen(a),sizeof a);
0
8983
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9310
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8235
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6792
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6072
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4592
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.