C++错误消息free():下一个大小无效(快速)

C++ error message free(): invalid next size (fast)

本文关键字:无效 快速 下一个 free 错误 C++ 消息      更新时间:2023-10-16

我第一次使用动态数组。我喜欢使用它们的原因是我想创建大小可变的数组。

在运行程序时,我在函数"assign_noise_levels…"之后收到以下错误消息。因此,该程序的最后一个输出是"完成噪音级别!"。

*** Error in `./test_linear_instance': free(): invalid next size (fast): 0x0000000000e78010 ***
======= Backtrace: =========
/lib64/libc.so.6[0x3803075acf]
/lib64/libc.so.6[0x380307cdf8]
./test_linear_instance[0x40150c]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x3803021d65]
./test_linear_instance[0x400db9]
======= Memory map: ========
00400000-00403000 r-xp 00000000 00:2b 1525818                            /ufs/hommerso/SCIP/test_linear_instance
00602000-00603000 r--p 00002000 00:2b 1525818                            /ufs/hommerso/SCIP/test_linear_instance
00603000-00604000 rw-p 00003000 00:2b 1525818                            /ufs/hommerso/SCIP/test_linear_instance
00e78000-00e99000 rw-p 00000000 00:00 0                                  [heap]
3802c00000-3802c20000 r-xp 00000000 08:02 1967211                        /usr/lib64/ld-2.18.so
3802e1f000-3802e20000 r--p 0001f000 08:02 1967211                        /usr/lib64/ld-2.18.so
3802e20000-3802e21000 rw-p 00020000 08:02 1967211                        /usr/lib64/ld-2.18.so
3802e21000-3802e22000 rw-p 00000000 00:00 0 
3803000000-38031b4000 r-xp 00000000 08:02 1967262                        /usr/lib64/libc-2.18.so
38031b4000-38033b3000 ---p 001b4000 08:02 1967262                        /usr/lib64/libc-2.18.so
38033b3000-38033b7000 r--p 001b3000 08:02 1967262                        /usr/lib64/libc-2.18.so
38033b7000-38033b9000 rw-p 001b7000 08:02 1967262                        /usr/lib64/libc-2.18.so
38033b9000-38033be000 rw-p 00000000 00:00 0 
3803400000-3803505000 r-xp 00000000 08:02 1980123                        /usr/lib64/libm-2.18.so
3803505000-3803705000 ---p 00105000 08:02 1980123                        /usr/lib64/libm-2.18.so
3803705000-3803706000 r--p 00105000 08:02 1980123                        /usr/lib64/libm-2.18.so
3803706000-3803707000 rw-p 00106000 08:02 1980123                        /usr/lib64/libm-2.18.so
3804400000-3804415000 r-xp 00000000 08:02 1983836                        /usr/lib64/libgcc_s-4.8.3-20140624.so.1
3804415000-3804614000 ---p 00015000 08:02 1983836                        /usr/lib64/libgcc_s-4.8.3-20140624.so.1
3804614000-3804615000 r--p 00014000 08:02 1983836                        /usr/lib64/libgcc_s-4.8.3-20140624.so.1
3804615000-3804616000 rw-p 00015000 08:02 1983836                        /usr/lib64/libgcc_s-4.8.3-20140624.so.1
3807c00000-3807cea000 r-xp 00000000 08:02 1983837                        /usr/lib64/libstdc++.so.6.0.19
3807cea000-3807ee9000 ---p 000ea000 08:02 1983837                        /usr/lib64/libstdc++.so.6.0.19
3807ee9000-3807ef1000 r--p 000e9000 08:02 1983837                        /usr/lib64/libstdc++.so.6.0.19
3807ef1000-3807ef3000 rw-p 000f1000 08:02 1983837                        /usr/lib64/libstdc++.so.6.0.19
3807ef3000-3807f08000 rw-p 00000000 00:00 0 
7feca8220000-7feca8225000 rw-p 00000000 00:00 0 
7feca825c000-7feca8260000 rw-p 00000000 00:00 0 
7fffbb4d4000-7fffbb4f5000 rw-p 00000000 00:00 0                          [stack]
7fffbb5c7000-7fffbb5c9000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted

这是我的代码:

/*  CREATE .dat FILE WITH LINEAR NOISE_FUNCTION      */

# include <iostream>
# include <fstream>
# include <random>
# include <cmath>
using namespace std ;
typedef double* DoublePtr ;
typedef int* IntPtr ;

void make_grid(int& width, int& height, int& length, int& steps)
{
    do
    {
        cout << "Enter an even number for the width of the grid (km): " ;
        cin >> width ;
    } while (width % 2 == 1 || width <= 0) ;
    do
    {
        cout << "Enter an even number for the height of the grid (km): " ;
        cin >> height ;
    } while (height % 2 == 1 || height <= 0) ;
    do
    {
        cout << "Enter an even number for the length of the runway (km): " ;
        cin >> length ;
    } while (length > width || length <=0) ;
    do
    {
        cout << "Enter the number of measure points per km: " ;
        cin >> steps ;
    } while (steps <= 0) ;
}

void assign_noise_levels_to_aircrafts(int& nr_of_aircrafts, double& max_noise_level, double& min_noise_level, DoublePtr noise_level)
{
    do
    {
        cout << endl << "Enter number of aircraft types: " ;
        cin >> nr_of_aircrafts ;    
    } while (nr_of_aircrafts <= 0) ;
    do
    {
        cout << "Enter the maximum noise level: " ;
        cin >> max_noise_level ;
    } while (max_noise_level < 0) ;
    if(nr_of_aircrafts == 1)
    {
        noise_level[0] = max_noise_level ;
        cout << endl << "There is only one aircraft. Its noise level is " << noise_level[0] << endl ;
    }
    else
    {   
        do
        {   
            cout << "Enter the minimum noise level: " ;
            cin >> min_noise_level ;
        } while (min_noise_level > max_noise_level || min_noise_level < 0) ;
        cout << endl ;      
        for(int i=0 ; i < nr_of_aircrafts ; i++)        
        {
            noise_level[i] = max_noise_level + i * (min_noise_level - max_noise_level)/(nr_of_aircrafts - 1) ;  
            cout << "The noise level of aircraft " << i+1 << " is " << noise_level[i] << endl ;
        }
    }
}

double dist(int point_1[2], int point_2[2])
{
    double d_0 = point_1[0]-point_2[0] ;
    double d_1 = point_1[1]-point_2[1] ;
    double sum = d_0*d_0 + d_1*d_1 ;
    double distance = sqrt(sum) ;   
 return distance ;
}

void put_houses(int grid_height, int grid_width, IntPtr *nr_of_houses)
{   
    random_device rd ;
    mt19937 generator(rd()) ;
    uniform_int_distribution<int> uni(0,1) ;
    cout << endl << "The houses are located as follows:" << endl ;
    for(int i=0 ; i<grid_height ; i++)          
    {   for(int j=0 ; j<grid_width ; j++)
        {   nr_of_houses[i][j] = uni(generator) ;
            cout << nr_of_houses[i][j] << " " ;
        }
        cout << endl ;
    }
    cout << endl ;
}


int main()
{
// designing the grid
    int steps ;                         
    int width ;                         
    int height ;                            
    int length ;                            
    make_grid(width, height, length, steps) ;   
// defining aircrafts by calculating their noise levels 
    int nr_of_aircrafts ;
    double max_noise_level ;
    double min_noise_level ;
    DoublePtr noise_level ;
    noise_level = new double[nr_of_aircrafts] ;         
    assign_noise_levels_to_aircrafts(nr_of_aircrafts, max_noise_level, min_noise_level, noise_level) ;
    cout << "Done with noise levels!" << endl ;
// calculating noise pollution for every aircraft
    int grid_width = (width*steps) + 1 ;                
    int grid_height = (height*steps) + 1 ;              
    int grid_mid_width = (1/2)*width*steps ; 
    int grid_mid_height = (1/2)*height*steps ;          
    int runway_ending_1 = grid_mid_width - (1/2)*length*steps ; 
    int runway_ending_2 = grid_mid_width + (1/2)*length*steps ; 
    // ... ...
delete[] noise_level ;

// putting one house on the grid points at random
    IntPtr *nr_of_houses = new IntPtr[grid_height] ;
    for (int i=0 ; i<grid_height ; i++)
        nr_of_houses[i] = new int[grid_width] ;         
    put_houses(grid_height, grid_width, nr_of_houses) ; 
    cout << "Done with put_houses!" << endl;
for(int i=0 ; i<grid_height ; i++)
    delete[] nr_of_houses[i] ;
delete[] nr_of_houses ;
// test if the whole program is run
    cout << "Done!" << endl ;
 return 0 ;
}

使用这些动态数组的内存肯定出了问题,但我不知道到底出了什么问题。以前运行的程序在函数put_houses之后显示了此错误消息,但现在该消息出现得更早。

有人能解释一下我做错了什么,以及我应该如何解决这个问题吗?

当前的问题是:

noise_level = new double[nr_of_aircrafts];

你还没有给nr_of_aircrafts一个值。它只在稍后调用assign_noise_levels_to_aircrafts时被赋予一个值。

更普遍的问题是,您在处理指针和new,而不是让std::vector等自动管理所有数组。如果你继续这样做,你可能会期待更多模糊的内存管理问题和长时间的调试会话。