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

corrupted virtual function table?

Hi,

I see a problem with the virtual functions when I new an array of
elements of the derived class type and get them using the base class
type.

In the program given below

type = msg->getMsgFormatType();

will crash the second time it goes through the loop. First time it
works fine. Second time it has problem. when I debug the code I
observed the following

first time:
msg
__vfptr === DerivedMsg::'vftable'
second time:
msg
__vfptr === ERROR expression


Code:
=========

enum FormatType
{
common,
derived
};

class CommonMsg
{

public:
virtual FormatType getMsgFormatType();

};
FormatType CommonMsg::getMsgFormatType()
{
return common;
}

class DerivedMsg:public CommonMsg
{
public:
unsigned char msgBuffer[55];
FormatType getMsgFormatType();
};

FormatType DerivedMsg::getMsgFormatType()
{
return derived;
}

class CommonMsgQ
{
protected:
CommonMsg *msg;

public:
virtual CommonMsg* getElement(int);

};
CommonMsg* CommonMsgQ::getElement(int index)
{
if (index < 5)
return (CommonMsg*) (&msg[index]);
else
return (CommonMsg*)0;
}

class DerivedMsgQ : public CommonMsgQ
{
public:
DerivedMsgQ (int);
CommonMsg* getElement(int);
};

DerivedMsgQ::DerivedMsgQ(int size)
{

msg = (CommonMsg *) new DerivedMsg[5];

}

CommonMsg* DerivedMsgQ::getElement(int index)
{
if (index < 5)
return (CommonMsg*) (&msg[index]);
else
return (CommonMsg*)0;
}

int main()
{

DerivedMsgQ dMsg(3);
CommonMsg* msg = (CommonMsg*)0;

for (int i =0; i<3; ++i)
{
msg = (CommonMsg*) 0;
msg = dMsg.getElement(i);
if (msg != (CommonMsg*) 0)
type = msg->getMsgFormatType();

}
return 0;
}

Nov 30 '06 #1
2 2793
The following change fixed the problem. Why should I type cast msg to
DerivedMsg*, does it already know that it is of type DerivedMsg* not
CommonMsg*?

Why does it work for the first element not for the subsequent ones?

Change that worked:
-----------------------------
CommonMsg* DerivedMsgQ::getElement(int index)
{
if (index < 5)
return (CommonMsg*) (&((DerivedMsg*)msg)[index]);
else
return (CommonMsg*)0;
}

Nov 30 '06 #2
NewToCPP wrote:
The following change fixed the problem. Why should I type cast msg to
DerivedMsg*, does it already know that it is of type DerivedMsg* not
CommonMsg*?

Why does it work for the first element not for the subsequent ones?

Change that worked:
-----------------------------
CommonMsg* DerivedMsgQ::getElement(int index)
{
if (index < 5)
return (CommonMsg*) (&((DerivedMsg*)msg)[index]);
else
return (CommonMsg*)0;
}
An array of objects of derived class should never be treated as
an array of objects of base type. You can only do it with individual
objects. As soon as you try indexing using an "incorrect" pointer,
the compiler tries to calculate the offset in the array using the
incorrect size, and you end up with a pointer to nowhere.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 30 '06 #3

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

Similar topics

4
by: DaKoadMunky | last post by:
I was recently looking at some assembly code generated by my compiler. When examining the assembly code associated with constructors I noticed that the dynamic-dispatch mechanism was enabled...
7
by: Frank-René Schäfer | last post by:
Case: -- class X has occupies tiny amount of memory: sizeof(X) is only a little greater than sizeof(void*). -- X instantiates thousands of objects and memory does matter. -- The class has...
3
by: Anunay | last post by:
Hello all, I have a doubt regarding the concept of virtual functions. When does the virtual function table get created? At compile time or at runtime? Please provide any good references where...
9
by: ypjofficial | last post by:
Hello All, I am defining a class with one virtual function and storing its first 4 bytes ie. the address of the virtual function table to a file.I am again rereading the file in the same program...
11
by: Chris Thomasson | last post by:
Consider an an object that that can has 7 or 8 functions. If you create an abstract base class for the "interface" of the object, well, that means 7 or 8 pure virtual functions right? Well, IMHO,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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,...
0
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...

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.