 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Schizoid Man Guest
|
Posted: Mon Oct 23, 2006 9:11 am Post subject: Runtime error - _CrtIsValidHeapPointer(pUserData) |
|
|
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
|
Posted: Mon Oct 23, 2006 9:11 am Post subject: Re: Runtime error - _CrtIsValidHeapPointer(pUserData) |
|
|
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 ).
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
|
Posted: Wed Oct 25, 2006 9:10 am Post subject: Re: Runtime error - _CrtIsValidHeapPointer(pUserData) |
|
|
Thanks you all of you, appreciate it :^) |
|
| Back to top |
|
 |
|
|
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
|
|