473,671 Members | 2,250 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How this code is executed ?

pai
Hi ,

Below is the code .

*************** *******
#include<iostre am>
using namespace std;

class A{

public:
int a;
A(){ cout << "Constructo r" << endl; }
void Ab(){ //a=10; cout << "hello " << a << endl; }

};

int main(){

A *a;
a->Ab();

return 0;

}
--------------------
This answer when givin command ./a.out is as follows
hello 1474660693

how could the "print" method executed while I havnt created an object
and also the vaiable b is showing garbage value while varable have not
been allocatedmemory .

Can any one explain me how is this happeneing.

Thanks
Pai

Feb 13 '07 #1
3 1276

"pai" <gr****@yahoo.c omwrote in message
news:11******** **************@ q2g2000cwa.goog legroups.com...
Hi ,

Below is the code .

*************** *******
#include<iostre am>
using namespace std;

class A{

public:
int a;
A(){ cout << "Constructo r" << endl; }
void Ab(){ //a=10; cout << "hello " << a << endl; }

};

int main(){

A *a;
a->Ab();

return 0;

}
--------------------
This answer when givin command ./a.out is as follows
hello 1474660693

how could the "print" method executed while I havnt created an object
and also the vaiable b is showing garbage value while varable have not
been allocatedmemory .

Can any one explain me how is this happeneing.

Thanks
Pai
You're effectively dereferencing an unitialized pointer, which yields
undefined behaviour, so your code could just about to anything (including
formatting your harddrive)

- Sylvester
Feb 13 '07 #2
In article <11************ **********@q2g2 000cwa.googlegr oups.com>,
"pai" <gr****@yahoo.c omwrote:
Hi ,

Below is the code .

*************** *******
#include<iostre am>
using namespace std;

class A{

public:
int a;
A(){ cout << "Constructo r" << endl; }
void Ab(){ //a=10; cout << "hello " << a << endl; }

};

int main(){

A *a;
a->Ab();

return 0;

}
--------------------
This answer when givin command ./a.out is as follows
hello 1474660693
Actually, because of the comment "//" the above won't even compile, but
we will pretend the "//" isn't there.
how could the "print" method executed while I havnt created an object
and also the vaiable b is showing garbage value while varable have not
been allocatedmemory .

Can any one explain me how is this happeneing.
That is called "undefined behavior" the compiler is allowed to generate
code that does anything, including executing the method and showing
garbage.

What is likely happening under the covers is that upon creating the
pointer, it has some garbage information in it, then when you call Ab(),
the system pretends that the garbage actually points to a real type A
object.

Best thing to do, is to assign NULL to 'a' upon creation.

A* a = NULL; or A* a = 0;

Calling Ab() on it is still undefined, but many compilers will flag that
as a runtime error.
Feb 13 '07 #3
On Feb 13, 9:23 am, "pai" <grp...@yahoo.c omwrote: (lightly edited)
Hi ,

Below is the code .

*************** *******
#include<iostre am>
using namespace std;

class A{

public:
int a;
A(){ cout << "Constructo r" << endl; }
void Ab()
{
//a=10;
cout << "hello " << a << endl;
}

};

int main(){

A *ap;
ap->Ab();

return 0;

}

--------------------
This answer when givin command ./a.out is as follows
hello 1474660693

how could the "print" method executed while I havnt created an object
and also the vaiable b is showing garbage value while varable have not
been allocatedmemory .

Can any one explain me how is this happening.

Thanks
Pai
I assume you just meant to comment out the assignment to the first
[int] 'a' and it got lost in line wrapping - as edited above along
with a renamed second [A *] 'a' as 'ap' to clarify what is being
talked about, I believe what happened was this...

The fuction Ab() was effectively compiled into this:

void C_Ab( A *this ) // Assume this is 'C'
{
// a=10;
cout << "hello " << this->a << endl;
}

The pointer 'ap' was in a register coincidentally initialized to a
random location on the stack, heap, or code space: quite likely, if
the register was used as a pointer before.

C_Ab() was called with this random address:

C_Ab( ap ); // == ap->Ab(); with random ap.

and cout was called with "hello " and the contents of what the 'this'
pointer pointed to, namely 1474660693 (this time). Other times you
probably would get a segfault or worse.

To really mess up, you should have declared Ab() virtual, in which
case 'ap' would have been used to access A's virtual function table
compiled something like this:

struct A_Vtable { (void *)Ab_Entry(A *); };

static A_Vtable = { &C_Ab };

(ap->A_Vtable.Ab_En try)( ap ); // == ap->Ab(); with random ap.

In this case instead of getting an integer from a random location and
printing it, you get a function pointer from nowhere in particular and
call it. At least it's not likely to fail silently!

Best Regards,

David O.

Feb 13 '07 #4

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

Similar topics

38
3515
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser = serial.Serial()
8
2288
by: | last post by:
In global.asa I have some code outside all the Subs. I have some confirmation that it is being executed OnStart. Yet I can's see if it is executed OnEnd. The literature I have says that OnEnd the Sub Application_OnEnd is executed, but is does not say that anything else is executed Actually I even can't understand if global.asa is permited to have any code outside the Subs
2
1616
by: Bramz | last post by:
Hi all, I have a rather strange linker (?) problem. At least, I think it's a linker problem, but I'm not so sure. It's a bit lengthy to describe ... I've implemented a generic factory similar to the one in 'modern c++ design' (page 208). To initialize this factory with the different products, I'm using the same technique on page 204-205 that bassicly looks like: namespace
16
2043
by: graham.reeds | last post by:
I am updating a website that uses a countdown script embedded on the page. When the page is served the var's are set to how long the countdown has left in minutes and seconds, but the rest of the script is left untouched. However I want to take the script out of the page and have it as a seperate file that can be cached, reducing serving costs - the page gets hit a couple of thousand times per day, sometimes as high a 5K, so any...
3
1550
by: moondaddy | last post by:
I'm writing a shopping cart in vb.net and for the first time I'm working with session state on the server. I'm using it to maintain the user's shopping cart for the session. in the browser is a datagrid with a listing of products. If I put a break point in the code behind the code execution will break at that point just as you would expect. When the user clicks on an item in the grid it executes a line of jscript listed below which...
18
2881
by: Simon | last post by:
I was of the impression that code placed after a Try...Catch block was only executed if there was no exception thrown. I've got some VB.net code as part of a Windows form that executes even when an exception is thrown - it behaves as if the code is part of a finally block. Looking through all the documentation and MSDN articles, it seems that none of the examples contain code placed
4
4389
by: Dennis Sjogren | last post by:
Greetings! First, I'm not 100% sure where to post this question. I use VB.NET for this project, but it's really a design question (a question on which method to use when solving this problem). In this medium sized (30 or so forms) application, our users have requested a more visual notification of when the client (this app) is communicating with the server, or for other lenghty processes. Disabling the current form and changing the...
2
1921
by: Water Cooler v2 | last post by:
http://www.w3schools.com/js/js_whereto.asp This link is to a JavaScript tutorial on w3schools. The page says that a script put in the HEAD is executed only when called, whereas one put in the BODY is executed as the page loads (implying invariably, whether called or not). To test, I tried the following two pages:
4
7666
by: GD | last post by:
Hi, I have the following C# code in a page: Response.ContentType = "APPLICATION/OCTET-STREAM"; Response.AppendHeader("Content-Disposition", "Attachment; Filename=\"C:\\Download.txt\"" FileInfo fileToDownload = new FileInfo("C:\\Test.txt"); Response.Flush(); Response.WriteFile(fileToDownload.FullName); Response.End();
1
2040
by: Jeff | last post by:
hi ..Net 2.0 I've created the code below and think I've executed it. After I thought I've executed it I checked in EventLog on the computer (win2k3) and no entry was added. This could mean 2 things: - There are some logical errors in my code - The code wasn't actually executed
0
8476
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
8917
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8821
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
8598
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
8670
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6229
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
4225
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
4407
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.