2
我第一次使用動態數組。我喜歡使用它們的原因是我想創建可變大小的數組。C++錯誤消息免費():無效下一個大小(快)
在運行該程序時,我在函數「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 198/usr/lib64/libm-2.18.so
3803505000-3803705000 ---p 00105000 08:02 198/usr/lib64/libm-2.18.so
3803705000-3803706000 r--p 00105000 08:02 198/usr/lib64/libm-2.18.so
3803706000-3803707000 rw-p 00106000 08:02 198/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
之後立即顯示此錯誤消息,但現在消息正在更早出現。
任何人都可以請解釋我做錯了什麼,我應該如何解決這個問題?
因爲這是C++,請嘗試使用更安全的類型,例如'std :: vector'和'std :: map',而不是C數組。那樣的話,內存是爲你處理的,所以你不應該得到任何無效的釋放。 – matsjoyce 2014-09-02 09:33:55
請勿使用動態數組。它們是首先不應該存在的語言的錯誤特徵。改用像std :: vector這樣的庫解決方案。 – 2014-09-02 09:39:43