简体   繁体   English

重新分配时出错但不是malloc?

[英]Error in realloc but not malloc?

I am having to work with someone else's code, so I am not entirely in control over what I can change but I am wondering why I am getting this error and would appreciate any suggestions. 我必须使用其他人的代码,因此我无法完全控制我可以更改的内容,但我想知道为什么会收到此错误并希望提出任何建议。 This code is in the part that I can't change but I need to make sure the part I wrote (StringRef class) works. 这段代码是我无法更改的部分,但是我需要确保编写的部分(StringRef类)有效。

The error it gives me is 'Heap block at X modified at Y past requested size of 28'. 它给我的错误是“ X的堆块在Y处超出请求的28大小而修改”。 If I change the existing code which is using realloc to malloc it kicks the can down the road a bit and loads a few more values into the array. 如果我更改了使用realloc进行malloc的现有代码,则会使罐头工作陷入困境,并将更多的值加载到数组中。 Here are the lines in question. 这是有问题的行。 I can't include all the code as it is too extensive. 我不能包含所有代码,因为它太广泛了。 Is this enough info to diagnose what I am doing wrong? 这足够信息来诊断我在做什么吗?

struct StringList
{
    StringRef *elements;
    unsigned int count;
};

.....

// Append the given StringRef to the list.
bool StringListAppend(StringList& self, StringRef p_string)
{

    StringRef *t_new_elements;
    t_new_elements = (StringRef *)realloc(self.elements, (self.count + 1) * sizeof(StringRef *));
    if (t_new_elements == NULL)
        return false;

    self.elements = t_new_elements;
    std::cout<<self.count<<"\n";
    // Initialize and assign the new element.
    StringInitialize(self.elements[self.count]);
    if (!StringAssign(self.elements[self.count], p_string))
        return false;

    // We've successfully added the element, so bump the count.
    self.count += 1;

    return true;
}

vs

StringRef *t_new_elements;
t_new_elements = (StringRef *)malloc((self.count + 1) * sizeof(StringRef *));

for the line with realloc averts the problem a little further. 对于带有realloc的行,该问题进一步避免了。

Lets say you wish to allocate some memory. 假设您希望分配一些内存。 You should use malloc (stores memory on heap uninitialized) or calloc(stores initializes all elements to 0). 您应该使用malloc(将内存未初始化存储在堆上)或calloc(将所有元素初始化为0)。 Explained more Here! 在这里解释更多!

Realloc extends the length of your allocated memory. Realloc扩展了分配的内存的长度。 So you need to allocate some before you can extend it. 因此,您需要先分配一些,然后才能扩展它。 (dont neeed to, but it is good coding practice to do so) Realloc (不neeed,但它是良好的编码习惯这样做) 的realloc

I would suggest looking up more on memory allocation because abusing it can greatly reduce efficiency and must be treated with precaution, like: you should always free memory that you have allocated at the end of your program! 我建议您更多地查看内存分配,因为滥用它会大大降低效率,并且必须采取预防措施,例如:您应该始终释放在程序结束时分配的内存!

sizeof(StringRef *) should be sizeof(StringRef) sizeof(StringRef *)应该是sizeof(StringRef)

You could avoid this error by using this idiom: 您可以通过使用以下惯用法来避免此错误:

ptr = realloc(old_ptr, n_elements * sizeof *ptr);

when the number of bytes to allocate is determined based on the type of the variable that holds the returned pointer; 当根据保存返回的指针的变量的类型确定要分配的字节数时; it doesn't require you to repeat anything and thereby introduce a discrepancy. 它不需要您重复任何操作,从而不会产生差异。

As to why changing realloc to malloc slightly moves the point at which the program crashes... undefined behaviour is undefined :) 至于为什么将realloc更改为malloc稍微移动程序崩溃的点...未定义的行为是未定义的:)

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

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