473,805 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Runtime error - _CrtIsValidHeap Pointer(pUserDa ta)

Hi,

I have defined a very simple class as follows. I initialize the
CInventory object as: CInventory *itemInveotry;

The program just performs two functions - it reads the total number of
items and values of itemNumber, quantity and cost per item from the
user, and it prints these values back.

While printing these values, I get a _CrtIsValidHeap Pointer(pUserDa ta)
exception if I use a statement like:

for (int i = 1; i <= numItems; i++)
(itemInventory + i)->printItem();

However, if I modify the for statement to the one below, the program
works perfectly.

for (int i = 0; i <= numItems - 1; i++)
(itemInventory + i)->printItem();

Can anyone tell me why the same program works fine for one for loop, but
not for the other (equivalent?) for loop?

When the application does fail I get the following error right before
the _CrtIsValidHeap Pointer:
HEAP CORRUPTION DETECTED: after Normal block (#143) CRT Detected
application wrote to memory after end of heap buffer

Thanks in advance,
Schiz

class CInventory {

private:
int itemNumber; // holder the item's item number
int quantity; // in-stock item quantity
double cost; // storage cost per item
double totalCost; // total inventory cost

public:
// set the input info for each item
void setItem(int, int, double);
// print the item info
void printItem(void) ;
};
Oct 23 '06
13 8792

Kouisawang wrote:
Gavin Deane wrote:
Kouisawang wrote:
Stuart Redmann wrote:
Operator+ is already overloaded for pointers. Depending on the actual
type that is pointed to, operator+ for pointers will behave like this:

T* operator+ (T* BaseAddress, int Offset)
{
return (T*) int (BaseAddress) + sizeof (T) * Offset;
}
What do you mean by "predefined or primary class/type"? Pointers are as
much built in and fundamental as types like int, double and bool.
Whenever you use the + operator to add an integer value to a pointer,
the behaviour is as described by the code above. And, because interger
types and pointer types are buil in to the language, there is no way
that you as the programmer can change the way the addition of an
integer to a pointer behaves, regarldess of whether that pointer type
happens to point to a built in type (e.g. int, double, bools etc.) or
points to some user defined (e.g. class) type. That behaviour is a
fundamental part of the language.

Gavin Deane

My predefind class/type is your fundamental type. So I mean if we use a
pointer that is not of any fundamental type, will there already be its
overload function for us?
For example,

Fundamental type

int *i; // there will be the overload function of "+" for this object,
right
There is no actual overloaded operator+ function for pointers. If you
add an int to the pointer i, all there is is behaviour mandated by the
language. The code posted by Stuart Redmann neatly describes that
mandated behaviour. But no function containing that (or equivalent)
code actually exists to be called. In the same way that

int a = 1;
int b = 2;
int c = a + b;

does not call any actual operator+ function for a pair of ints. The
behaviour is built in to the language and does not need to be defined
in a function somewhere.
then what if

// myClass is the class that I have created
myClass *i; //will there be the overload function of "+" for this
object?
No difference whatsoever. There is no actual overloaded operator+
function for pointers. If you add an int to the pointer i, exactly the
same behaviour is mandated by the language so Stuart Redmann's code is
an equally useful description of that behaviour. Note that his
description is based on a type pointer-to-T and makes no assumption
either way about whether T happens to be a built in type (e.g int) or a
user defined type (e.g myclass). That makes his description a sensible
one because as far as the language is concerned, with this question,
whether the type pointed to happens to be built in or user defined is
completely irrelevant.

Gavin Deane

Oct 24 '06 #11
Kouisawang wrote:
"C or C++ arrays are _always_ accessed starting from index zero up the
size of the array minus one."?

Can't we start accessing form any other positions in an array?
>>From the code above, I don't think there will be a problem if we change
from "for(int i=0;.....)"
to "for(int i=2, .....)". so not sure what do you mean by that.
I meant that if you want to access the first element of the array, you
have to use index zero. If you want to access the last element, you have
to use the index that equals the number of array elements minus one.
Note that C++ doesn't offer the possibility to define the index range
for an array, as this is allowed under Ada95 for example: The following
declaration is an Ada95 array declaration where the index starts from 50
and ends at 100 (so the array contains 51 Integers):
type Table is array(50 .. 100) of Integer;

(Unfortunately, ) such facilities are not availale under C++ (that means
you have to use some templates to achive the same behaviour).

Regards,
Stuart
Oct 24 '06 #12
Kouisawang wrote:
My predefind class/type is your fundamental type. So I mean if we use a
pointer that is not of any fundamental type, will there already be its
overload function for us?
A pointer is a fundamental type, even if the things pointed are not.

--
Salu2
Oct 24 '06 #13
Thanks you all of you, appreciate it :^)

Oct 25 '06 #14

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

Similar topics

1
2166
by: Morten Lind | last post by:
Hello I'm having a problem with some DLL, that I wrote, and a test application using it, which I also wrote. In the DLL some class A is defined and exported. I have no problem using it in the test application. Also in the DLL there is a class B which has a method of the kind void B::getSomeAs(vector<A>& aContainer)
0
1112
by: scott_wu | last post by:
Greeting: I use dotnet2003(DB:Mysql data provider: MySQLDriverCS1 socket: easysocket) after compile release and run on win2000 I got dbgheap.c 1132 expression : _crtisvalidheappointer (pUserData) And my application is dead My another application memory will increase very quickly Could anyone guide me where I could start to trace what cause this Any suggestion is appreciated Scott_Wu
0
1062
by: Al Havrilla | last post by:
i'm getting the following error from my DEBUG build of a program that ran fine under vc 6. --------------------------------- ntdll.dll!77f75a58() ntdll.dll!77f9cb5e() ntdll.dll!77f82c95() KERNEL32.DLL!77e75b6c() KERNEL32.DLL!77e6c75a()
12
3477
by: Markus Ewald | last post by:
I'm just experimenting around with the VisualC++ 2005 Express Edition Beta and found some strange behavior for which I can't seem to find a workaround. What I've done is set up two static library projects for Lua 5.0.2 and LuaBind beta-6, compiling both with no CLR support and configured to use the Multithreaded DLL runtime. Then I created a new console application which makes use of these two static libraries (of course, also set to MT...
0
5542
by: jiing | last post by:
When I execute my program (it's multithread and has COM and dll), there is an error message as follows: Program D:\dongle_1\TestAP\bin\Debug\TestAP.exe File: dbgheap.c Line: 1132 Expression: _CrtIsValidHeapPointer(pUserData) For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts. ==================================================
0
1619
by: jiing | last post by:
When I execute my program (it's multithread and has COM and dll), there is an error message as follows: Program D:\dongle_1\TestAP\bin\Debug\TestAP.exe File: dbgheap.c Line: 1132 Expression: _CrtIsValidHeapPointer(pUserData) For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts. ==================================================
6
1580
by: nscbabu | last post by:
Hi, We are migrating a product from vc6 to vs.net 2003, when we run the app it crashes pointing to a c++ class destructor, we didnot make any code changes during the migration. Can anybody help me giving some pointers on what could be the possible reason? Thanks in advance, --sarath.
1
1693
by: Pascalus | last post by:
Hi there! I have a problem with the delete operator to destroy a (char*). It used to work since today. I don't know what I may have changed in the project and/or the solution (probably in the properties or what) to cause this problem... I have an app written in cpp.NET. In one function, I call an external function from a win32 DLL of my own. Here is the body: #include "myDll.h" void myCppNetFunction(...) { char* str = NULL;
0
1870
by: =?Utf-8?B?REx1ZWNr?= | last post by:
I am getting a debug assertion error that reads: Debug Assertion Failed! program E:\program files\internet explorer\iexplore,exe File: dbgheap.c Line: 1252 Expression: _CrtIsValidHeapPointer(pUserData)
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10105
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...
0
9185
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
7646
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
6876
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
5542
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
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
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.