运行时错误 - _CrtIsValidHeapPointer(pUserData) [英] Runtime error - _CrtIsValidHeapPointer(pUserData)

查看:86
本文介绍了运行时错误 - _CrtIsValidHeapPointer(pUserData)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我已经定义了一个非常简单的类,如下所示。我将

CInventory对象初始化为:CInventory * itemInveotry;


程序只执行两个函数 - 它读取总数
$ b来自

用户的$ b项目和itemNumber的数量,每件商品的数量和成本,并且它会打印这些值。


打印这些值时,我得到一个_CrtIsValidHeapPointer(pUserData)
如果我使用如下语句
异常:(b i = 1; i< = numItems; i ++)

(itemInventory + i) - > printItem();


但是,如果我将for语句修改为下面的那个,程序

完美无缺。


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

(itemInventory + i) - > printItem();


任何人都可以告诉我为什么同一个程序适用于一个for循环,但

不适用于另一个(相当于?) for loop?


当应用程序失败时,我会在
$ b之前收到以下错误$ b _CrtIsValidHeapPointer:

HEAP CORRUPTION DETECTED:正常阻止后(#143)检测到CRT

应用程序在堆缓冲结束后写入内存

提前致谢,

Schiz


class CInventory {


private:

int itemNumber; //持有物品的物品编号

int quantity; //库存商品数量

双倍成本; //每件商品的存储成本

double totalCost; //总库存成本


public:

//设置每个项目的输入信息

void setItem(int,int ,double);

//打印商品信息

void printItem(void);

};

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);
};

推荐答案

您好,

我无法回答您的问题抱歉,但我对您的

代码有疑问。


Schizoid Man写道:
Hello,
I can''t answer your question sorry, but I have a question about your
code instead.

Schizoid Man wrote:




我定义了一个非常简单的类如下。我将

CInventory对象初始化为:CInventory * itemInveotry;


程序只执行两个函数 - 它读取总数
$ b来自

用户的$ b项目和itemNumber的数量,每件商品的数量和成本,并且它会打印这些值。


打印这些值时,我得到一个_CrtIsValidHeapPointer(pUserData)

异常如果我使用如下语句:


for(int i = 1; i< = numItems; i ++)
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++)



以下行是否合法而没有超载+?如果是这样,那是什么意思?b $ b是什么意思?对不起一个菜鸟问题^^''

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


(itemInventory + i) - > printItem();


但是,如果我将for语句修改为下面的那个,那么程序

就完美了。


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

(itemInventory + i) - > printItem();


任何人都可以告诉我为什么同一个程序适用于一个for循环,但是

不是为了其他(相当于?)for循环?


当应用程序失败时我会在
$之前收到以下错误b $ b _CrtIsValidHeapPointer:

HEAP CORRUPTION DETECTED:正常块后(#143)检测到CRT

应用程序在堆缓冲区结束后写入内存
(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。在我看来,这可能是你的编译器错误的原因

分配内存应该是编译器的工作。程序员不能自己明确分配内存(或者有这样的功能吗?不是

肯定:P)。

希望可能有所帮助。

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.


>

提前致谢,

Schiz


class CInventory {


private:

int itemNumber; //持有物品的物品编号

int quantity; //库存商品数量

双倍成本; //每件商品的存储成本

double totalCost; //总库存成本


public:

//设置每个项目的输入信息

void setItem(int,int ,double);

//打印商品信息

void printItem(void);

};
>
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

-O.Kittipot


Schizoid Man写道:

[snip]
Schizoid Man wrote:

[snip]

打印这些值时,如果我使用语句,我会得到_CrtIsValidHeapPointer(pUserData)

异常喜欢:


for(int i = 1; i< = numItems; i ++)

(itemInventory + i) - > printItem();


但是,如果我将for语句修改为下面的那个,那么程序

就能完美运行。


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

(itemInventory + i) - > printItem();


任何人都可以告诉我为什么同一个程序适用于一个for循环,但

不适用于其他(等效?)for循环?
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?



也许你应该从你的书架上拿下你的C ++ Primer并且做一些严肃的重读。 C或C ++数组_always_从索引零开始从
开始访问,数组大小减去1。你的第二个循环

将尝试读取数组外部的内存,幸运的是这是由编译器的运行时检查工具捕获的



问候,

Stuart

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


Kouisawang写道:
Kouisawang wrote:

你好,

我不能回答你的问题抱歉,但我对你的

代码有疑问。


Schizoid Man写道:
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 ++)
>>
for (int i = 1; i <= numItems; i++)



以下行是否合法而没有超载+?如果是这样,那是什么意思?b $ b是什么意思?对不起一个菜鸟问题^^''


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


>> (itemInventory + i) - > printItem();
>> (itemInventory + i)->printItem();



运算符+已经超载了指针。根据指向的实际

类型,operator + for pointers的行为如下:


T * operator +(T * BaseAddress,int Offset)

{

return(T *)int(BaseAddress)+ sizeof(T)* Offset;

}

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。在我看来,这可能是你的编译器错误的原因

分配内存应该是编译器的工作。一个程序员不能自己明确地分配内存(或者有这样的功能吗?不是

肯定:P)。

希望可能有所帮助。
>
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.



你是不是一个Java程序员?谷歌为运营商新,或

购买一本关于C ++的教科书。任何人都会这样做。


Stuart

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

Stuart


这篇关于运行时错误 - _CrtIsValidHeapPointer(pUserData)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆