473,796 Members | 2,625 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 #1
13 8791
Hello,
I can't answer your question sorry, but I have a question about your
code instead.

Schizoid Man wrote:
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++)
Is the line below really legal without overloading "+"? If so, what
does it mean? sorry for a noob question ^^'
(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
PS. in my opinion, this may be the error of your compiler coz
allocating memory should be a compiler's job. A programmer cannot
explicitly allocate memory himself ( or there is such a function? not
sure :P ).
Hope might help.

>
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) ;
};
-O.Kittipot

Oct 23 '06 #2
Schizoid Man wrote:

[snip]
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?
Maybe you should take your C++ Primer down from your bookshelf and do
some serious re-reading. C or C++ arrays are _always_ accessed starting
from index zero up the size of the array minus one. Your second loop
will try to read memory that lies outside the array, and luckily this is
caught by the run-time checking facility of your compiler.

Regards,
Stuart
Oct 23 '06 #3
Kouisawang wrote:
Hello,
I can't answer your question sorry, but I have a question about your
code instead.

Schizoid Man wrote:
>>
for (int i = 1; i <= numItems; i++)

Is the line below really legal without overloading "+"? If so, what
does it mean? sorry for a noob question ^^'

>> (itemInventory + i)->printItem();
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;
}
>
PS. in my opinion, this may be the error of your compiler coz
allocating memory should be a compiler's job. A programmer cannot
explicitly allocate memory himself ( or there is such a function? not
sure :P ).
Hope might help.
Are you by any chance a Java programmer? Google for "operator new", or
buy a text book about C++. Any will do.

Stuart

Oct 23 '06 #4

Stuart Redmann wrote:
Schizoid Man wrote:

[snip]
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?

Maybe you should take your C++ Primer down from your bookshelf and do
some serious re-reading. C or C++ arrays are _always_ accessed starting
from index zero up the size of the array minus one. Your second loop
will try to read memory that lies outside the array, and luckily this is
caught by the run-time checking facility of your compiler.
Of course!!! If I initialize i = 1, then the method call should be:
(itemInventory + i - 1)->printItem();

Thanks for the help.

Oct 23 '06 #5

Stuart Redmann wrote:
Kouisawang wrote:
Hello,
I can't answer your question sorry, but I have a question about your
code instead.

Schizoid Man wrote:
>
for (int i = 1; i <= numItems; i++)
Is the line below really legal without overloading "+"? If so, what
does it mean? sorry for a noob question ^^'

> (itemInventory + i)->printItem();
-----------------------------------------------------------------------------------------------------------------
So you mean all pointers already have overload "+" operation even
though
that pointer that doesn't belong to the predefined or primary
class/type?
Sorry again about noob question, I know just little about c++ and wanna
study more.
-----------------------------------------------------------------------------------------------------------------
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;
}

PS. in my opinion, this may be the error of your compiler coz
allocating memory should be a compiler's job. A programmer cannot
explicitly allocate memory himself ( or there is such a function? not
sure :P ).
Hope might help.

Are you by any chance a Java programmer? Google for "operator new", or
buy a text book about C++. Any will do.

Stuart
Oct 23 '06 #6

Stuart Redmann wrote:
Schizoid Man wrote:

[snip]
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?

Maybe you should take your C++ Primer down from your bookshelf and do
some serious re-reading. C or C++ arrays are _always_ accessed starting
from index zero up the size of the array minus one. Your second loop
will try to read memory that lies outside the array, and luckily this is
caught by the run-time checking facility of your compiler.

Regards,
Stuart
You are right that the array is out of bound. However, most compiler I
use don't show this type of error, so I didn't check carefully about
this point. Usually you can manipulate the out of bound array but what
you get is just the garbage that have been used by the previous
program.

for example.

int a[10] = {0};

for(int i=0; i<10; i++){
cout << "This is a[" << i+1 << "]:" << a[i+1] << endl;
}

you have accessed the a[10], which is not a valid member of our array
but this program won't crash; it will show some garbage for a[10]
instead.
And what do you mean by

"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.

This may be happen only for my compilers, gcc, Dev-C++, and VisualC++
2005(express).
What is your compiler, I want to try if it can catch the out of bound
array? I may have to migrate to it. :^)

Thanks in advance.

Oct 23 '06 #7

Kouisawang wrote:
You are right that the array is out of bound. However, most compiler I
use don't show this type of error, so I didn't check carefully about
this point. Usually you can manipulate the out of bound array but what
you get is just the garbage that have been used by the previous
program.
Ususally, perhaps. But not necessarily always.
for example.

int a[10] = {0};

for(int i=0; i<10; i++){
cout << "This is a[" << i+1 << "]:" << a[i+1] << endl;
}

you have accessed the a[10], which is not a valid member of our array
correct
but this program won't crash;
It might well crash. Nothing about the code says that a crah is
mpossible.
it will show some garbage for a[10] instead.
It might do that too. a is an array of 10 ints, indexed by values 0 to
9. An attempt to access a[10] invokes undefined behavior. That means
that *absolutely anything* is possible. Of the allowed infinite number
of different outcomes, two possibilities are:

A crash
The output shows some garbage value

Anything else you can think of is also possible.

Gavin Deane

Oct 24 '06 #8

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;
}
-----------------------------------------------------------------------------------------------------------------
So you mean all pointers already have overload "+" operation even
though
that pointer that doesn't belong to the predefined or primary
class/type?
Sorry again about noob question, I know just little about c++ and wanna
study more.
-----------------------------------------------------------------------------------------------------------------
<rearranged above>

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

Oct 24 '06 #9

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;
}
-----------------------------------------------------------------------------------------------------------------
So you mean all pointers already have overload "+" operation even
though
that pointer that doesn't belong to the predefined or primary
class/type?
Sorry again about noob question, I know just little about c++ and wanna
study more.
-----------------------------------------------------------------------------------------------------------------

<rearranged above>

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

then what if

// myClass is the class that I have created
myClass *i; //will there be the overload function of "+" for this
object?

Thank you for your help :^)

Oct 24 '06 #10

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
1111
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
3476
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
5539
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
1579
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
1692
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
1869
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
9685
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
10239
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...
0
10019
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
9057
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
7555
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
6796
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
5447
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
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3736
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.