473,326 Members | 2,175 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,326 software developers and data experts.

How this code is executed ?

pai
Hi ,

Below is the code .

**********************
#include<iostream>
using namespace std;

class A{

public:
int a;
A(){ cout << "Constructor" << 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 1266

"pai" <gr****@yahoo.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
Hi ,

Below is the code .

**********************
#include<iostream>
using namespace std;

class A{

public:
int a;
A(){ cout << "Constructor" << 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**********************@q2g2000cwa.googlegroups. com>,
"pai" <gr****@yahoo.comwrote:
Hi ,

Below is the code .

**********************
#include<iostream>
using namespace std;

class A{

public:
int a;
A(){ cout << "Constructor" << 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.comwrote: (lightly edited)
Hi ,

Below is the code .

**********************
#include<iostream>
using namespace std;

class A{

public:
int a;
A(){ cout << "Constructor" << 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_Entry)( 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
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 =...
8
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...
2
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...
16
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...
3
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...
18
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...
4
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). ...
2
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...
4
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...
1
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.