简体   繁体   English

C ++:两个动态数组导致内存分配错误

[英]C++: Two dynamic arrays causes memory allocation error

Here is my code: 这是我的代码:

int main()
{
    int length = 10;
    int* H = new int(length);
    for(int i=0;i<length;i++)
    {
         H[i] = 0;
    }
    for(int i=0;i<length;i++)
        cout << i << ": " << "\t" << H[i] << "\n";

    double* dos = new double(length);
    for(int i=0;i<length;i++)
    {
        dos[i] = 1.0;
    }
    for(int i=0;i<length;i++)
         cout << i << ": " << dos[i] << "\t" << H[i] << "\n";
    return 0;
}

I'm trying to create an array of doubles that all equal 1.0 and an array of ints that all equal 0. When I run this code it correctly outputs the dos array, but then I get this error 我试图创建一个全为1.0的double数组和一个全为0的ints数组。当我运行此代码时,它正确地输出了dos数组,但随后出现此错误

lattice3d: malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || grid3d:malloc.c:2451:sYSMALLOc:声明`(old_top ==((((mbinptr)((((char *)&((av)-> bins [(((1)-1)* 2]))) (struct malloc_chunk,fd))))&& old_size == 0)|| ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed. (((unsigned long)(old_size)> =(unsigned long)(([[(__ builtin_offsetof(struct malloc_chunk,fd_nextsize))+((2 *(sizeof(size_t)))-1))&〜((2 *(sizeof (size_t)))-1)))&&(((old_top)-> size&0x1)&&((unsigned long)old_end&pagemask)== 0)'失败。 Aborted (core dumped) 中止(核心已弃用)

If I try to initialize the two arrays at the same time, I don't get a memory error, but the arrays end up with weird values within them (ex: the H array looks like [0,0,0,0,0,0,0,0,0,1247506] or something to that effect). 如果我尝试同时初始化两个数组,则不会收到内存错误,但是数组最终会在其中包含怪异的值(例如:H数组看起来像[0,0,0,0,0 ,0,0,0,0,1247506]或类似的意思)。 Moving where I set all the values of H and dos, changes which values are incorrect. 移动到我设置H和dos的所有值的位置,更改哪些值不正确。

You are allocating an int not an array: 您正在分配一个int而不是一个数组:

int* H = new int(length);

should be: 应该:

int* H = new int[length];

same with your double case: 与您的double情况相同:

double* dos = new double(length);

should be: 应该:

double* dos = new double[length];

what you are doing if it were allocated on the stack is int H(10); 如果将其分配到堆栈上,您正在做的是int H(10); which is like int H = 10; 就像int H = 10; , and the same for your double case; ,同样适用于双重情况​​; double dos(10) is like double dos = 10 . double dos(10)就像double dos = 10

You also are leaking the arrays/values you are creating, and need to call delete[] at the end of your program (note this would be after you do the corrections above): 您还会泄漏正在创建的数组/值,并且需要在程序末尾调用delete[] (请注意,这是在执行上述更正之后):

delete[] H;
delete[] dos;

Since this is tagged C++ and you are using new , it would be worth noting that in modern C++ that is usually best to avoid using new when possible. 由于此标记为C ++并且您正在使用new ,因此值得注意的是,在现代C ++中,通常最好避免使用new Here, it would be better to use std::vector than a dynamically allocated array. 在这里,使用std::vector优于动态分配的数组。 There is pretty much always a better option to use than new that usually involves using something from the standard library. 总是有比new的更好的选择,后者通常涉及使用标准库中的某些东西。

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

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