C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Runtime error - _CrtIsValidHeapPointer(pUserData)

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Schizoid Man
Guest





PostPosted: Mon Oct 23, 2006 9:11 am    Post subject: Runtime error - _CrtIsValidHeapPointer(pUserData) Reply with quote



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 _CrtIsValidHeapPointer(pUserData)
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 _CrtIsValidHeapPointer:
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);
};
Back to top
Kouisawang
Guest





PostPosted: Mon Oct 23, 2006 9:11 am    Post subject: Re: Runtime error - _CrtIsValidHeapPointer(pUserData) Reply with quote



Hello,
I can't answer your question sorry, but I have a question about your
code instead.

Schizoid Man wrote:
Quote:
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 _CrtIsValidHeapPointer(pUserData)
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 ^^'

Quote:
(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 _CrtIsValidHeapPointer:
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 Razz ).
Hope might help.


Quote:

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
Back to top
Kouisawang
Guest





PostPosted: Wed Oct 25, 2006 9:10 am    Post subject: Re: Runtime error - _CrtIsValidHeapPointer(pUserData) Reply with quote



Thanks you all of you, appreciate it :^)
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.