简体   繁体   English

没有使用malloc分配内存

[英]Didn't allocate memory by using malloc

I am facing a problem on malloc for allocating memory: 我在malloc上遇到分配内存的问题:

ByteArr = (BYTE *)malloc(sizeof(SHORT) * 20); 

I m getting error like 我遇到类似的错误

"CXX0030: Error: expression cannot be evaluated"

But if i am taking 428 or 1024 instead of 20 than its allocating the memory.Can you please tell me where is the problem ...thanks. 但是,如果我比分配内存要占用428或1024而不是20,请问我哪里出了问题...谢谢。

Extending lavino's answer and the fact this problem does not happen when you use the values like 1024 indicates to me that you are trying to read/write from a memory which is outside what you have allocated. 扩展lavino的答案以及使用1024这样的值时不会发生此问题的事实向我表明,您正在尝试从已分配的内存之外进行读/写操作。 Looks like you have allocated 20 shorts and try to read 100th short usinf the ByteArr pointer. 看起来您已经分配了20个短裤,并尝试使用ByteArr指针读取第100个短裤。 This will show the 'expression can not be evaluated' error in the debugger. 这将显示调试器中的“无法评估表达式”错误。

I'm not sure why it's working for other values, but the expression that it can't evaluate might be the missing variable for the ByteArr. 我不确定为什么它适用于其他值,但是无法评估的表达式可能是ByteArr缺少的变量。 You have the type specified, but no variable to assign. 您已指定类型,但没有要分配的变量。

BYTE *myByteArr = (BYTE *)malloc(sizeof(SHORT) * 20);

Updated: 更新:

That's a message in the debugger, telling you that the memory pointed to by the return isn't a valid memory block. 这是调试器中的一条消息,告诉您返回所指向的内存不是有效的内存块。

[Not this] Is the return value ENOMEM ? [不是这个]返回值是ENOMEM吗? If so, for some reason memory isn't being allocated, or the target variable isn't compatible with the return value of the malloc() call. 如果是这样,则由于某种原因未分配内存,或者目标变量与malloc()调用的返回值不兼容。

[Not this] What is the type of ByteArr ? [不是这个] ByteArr是什么类型? It's BYTE* , right? BYTE* ,对不对? And not BYTE[] ? 而不是BYTE[]吗?

[How about this?] At the time of the debugger message, is ByteArr still pointing to the same address that was returned by the malloc() call? [怎么办?]在发出调试器消息时, ByteArr仍指向malloc()调用返回的相同地址? You might be off the end of the array, or completely outside the allocated memory block. 您可能不在阵列的末尾,或者完全不在分配的内存块之外。

I guess the problem is that it should be: 我想问题是它应该是:

Byte* ByteArr = (BYTE *)malloc(sizeof(SHORT) * 20);

instead of: 代替:

Byte ByteArr = (BYTE *)malloc(sizeof(SHORT) * 20);

Now, I am not sure what is ByteArr in your code but from one of your comments to another answer I have sort of picked out that this is the problem. 现在,我不确定您的代码中的ByteArr是什么,但是从您的评论之一到另一个答案,我已经意识到这是问题所在。

Reference this page: 参考此页面:

http://www.codeguru.com/forum/showthread.php?t=430940 http://www.codeguru.com/forum/showthread.php?t=430940

It seems you need allocate more space. 似乎您需要分配更多空间。 Also google is always a good helper. 谷歌总是一个很好的帮手。

What I feel is there might be one more allocation, before this allocation, and there you are over running the allocated memory space. 我的感觉是,在此分配之前可能还会有另外一个分配,而您已经超出了分配的内存空间。 And then again trying to allocate here, where malloc logic may be failing. 然后再次尝试在此处分配,这可能导致malloc逻辑失败。

malloc is not finding next free block because of previous case of over running and hence not able to allocate for mentioned space, and if you give 1024 malloc is finding one block where that much free space is available and is getting allocated. 由于先前的超速运行情况,因此malloc找不到下一个空闲块,因此无法分配提到的空间,并且如果给出1024,则malloc会找到一个有足够可用空间并正在分配的块。

Try to fix that allocation then this problem will be solved. 尝试修复该分配,然后将解决此问题。

I think you are correcting your memory somewhere else in your software. 我认为您正在纠正软件中其他位置的内存。 Try using a tool like Rational PurifyPlus from IBM, or Bounds Checker . 尝试使用IBM的Rational PurifyPlusBounds Checker之类的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM