Why does my program crash when compiled w/ g++?

I don't understand why g++ doesn't like my code. I can compile and run this program just fine under Windows 7 using the cl compiler from Microsoft's "Visual Studio 2008", but when I compile the exact same program in Ubuntu Linux with g++ it crashes during the delete sequence when I run it.

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;

int main() {
	typedef int* IntArrayPtr;
	cout << "Input 2 sizes for the array\n";
	int x,y;
	cin >> x >> y;
	srand(time(NULL));
	IntArrayPtr *m = new IntArrayPtr[x];	// Create 1st level of Array
	
	for (int i = 0; i < x; i++)
		m[i]=new int[y];		// Creating 2nd dimension

	for (int i = 0; i < x; i++)
		for (int j=0; j < y; j++)
			m[i][j] = rand()%100;	// Filling array with random numbers

	for (int i = 0; i < x; i++) {	// Printing matrix
		for (int j=0; j < y; j++) {
			cout << i << "," << j << "==";
			if (m[i][j]<10) cout << "0";  // Keep it nice and even
			cout << m[i][j] << " ";
		}
		cout << endl;			// After each row, do an endl
	}

cout << x << endl;			// locating the error.
	for (int i = 0; i < x; i++) {
		cout << i << ".";
		delete [] m;				// Delete array
	}
	delete []m;						// Delete array;
}


The error apparently occurs IN the FOR loop, because it never even prints "0."

Here is my output:

Input 2 sizes for the array
10 10
0,0==81 0,1==97 0,2==88 0,3==34 0,4==59 0,5==21 0,6==00 0,7==60 0,8==43 0,9==06 
1,0==33 1,1==19 1,2==20 1,3==04 1,4==17 1,5==34 1,6==55 1,7==17 1,8==13 1,9==61 
2,0==07 2,1==69 2,2==56 2,3==08 2,4==56 2,5==07 2,6==81 2,7==66 2,8==29 2,9==26 
3,0==82 3,1==10 3,2==75 3,3==70 3,4==97 3,5==34 3,6==44 3,7==97 3,8==94 3,9==87 
4,0==55 4,1==28 4,2==59 4,3==75 4,4==32 4,5==28 4,6==61 4,7==87 4,8==45 4,9==27 
5,0==00 5,1==04 5,2==96 5,3==09 5,4==12 5,5==04 5,6==68 5,7==46 5,8==70 5,9==98 
6,0==24 6,1==04 6,2==60 6,3==99 6,4==27 6,5==57 6,6==33 6,7==71 6,8==07 6,9==28 
7,0==10 7,1==62 7,2==08 7,3==69 7,4==90 7,5==92 7,6==97 7,7==51 7,8==31 7,9==95 
8,0==78 8,1==31 8,2==99 8,3==26 8,4==40 8,5==64 8,6==30 8,7==09 8,8==62 8,9==53 
9,0==59 9,1==86 9,2==09 9,3==19 9,4==85 9,5==36 9,6==29 9,7==18 9,8==07 9,9==36 
10
*** glibc detected *** ./lab08-2: double free or corruption (fasttop): 0x09386008 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(+0x6b591)[0x17b591]
/lib/tls/i686/cmov/libc.so.6(+0x6cde8)[0x17cde8]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0x17fecd]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xf61741]
/usr/lib/libstdc++.so.6(_ZdaPv+0x1d)[0xf6179d]
./lab08-2[0x8048b46]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x126bd6]
./lab08-2[0x8048821]
======= Memory map: ========
00110000-00263000 r-xp 00000000 08:07 656114     /lib/tls/i686/cmov/libc-2.11.1.so
00263000-00264000 ---p 00153000 08:07 656114     /lib/tls/i686/cmov/libc-2.11.1.so
00264000-00266000 r--p 00153000 08:07 656114     /lib/tls/i686/cmov/libc-2.11.1.so
00266000-00267000 rw-p 00155000 08:07 656114     /lib/tls/i686/cmov/libc-2.11.1.so
00267000-0026a000 rw-p 00000000 00:00 0 
0062c000-00650000 r-xp 00000000 08:07 659375     /lib/tls/i686/cmov/libm-2.11.1.so
00650000-00651000 r--p 00023000 08:07 659375     /lib/tls/i686/cmov/libm-2.11.1.so
00651000-00652000 rw-p 00024000 08:07 659375     /lib/tls/i686/cmov/libm-2.11.1.so
00843000-00844000 r-xp 00000000 00:00 0          [vdso]
0094f000-0096a000 r-xp 00000000 08:07 655416     /lib/ld-2.11.1.so
0096a000-0096b000 r--p 0001a000 08:07 655416     /lib/ld-2.11.1.so
0096b000-0096c000 rw-p 0001b000 08:07 655416     /lib/ld-2.11.1.so
00cc4000-00ce1000 r-xp 00000000 08:07 655443     /lib/libgcc_s.so.1
00ce1000-00ce2000 r--p 0001c000 08:07 655443     /lib/libgcc_s.so.1
00ce2000-00ce3000 rw-p 0001d000 08:07 655443     /lib/libgcc_s.so.1
00ea6000-00f8f000 r-xp 00000000 08:07 3017275    /usr/lib/libstdc++.so.6.0.13
00f8f000-00f90000 ---p 000e9000 08:07 3017275    /usr/lib/libstdc++.so.6.0.13
00f90000-00f94000 r--p 000e9000 08:07 3017275    /usr/lib/libstdc++.so.6.0.13
00f94000-00f95000 rw-p 000ed000 08:07 3017275    /usr/lib/libstdc++.so.6.0.13
00f95000-00f9c000 rw-p 00000000 00:00 0 
08048000-08049000 r-xp 00000000 08:05 12707      /media/768AD3658AD32083/Users/James/Documents/Visual Studio 2008/Projects/c201/lab08-2
08049000-0804a000 r--p 00000000 08:05 12707      /media/768AD3658AD32083/Users/James/Documents/Visual Studio 2008/Projects/c201/lab08-2
0804a000-0804b000 rw-p 00001000 08:05 12707      /media/768AD3658AD32083/Users/James/Documents/Visual Studio 2008/Projects/c201/lab08-2
09386000-093a7000 rw-p 00000000 00:00 0          [heap]
b7700000-b7721000 rw-p 00000000 00:00 0 
b7721000-b7800000 ---p 00000000 00:00 0 
b78e8000-b78ea000 rw-p 00000000 00:00 0 
b78fa000-b78fe000 rw-p 00000000 00:00 0 
bfd64000-bfd79000 rw-p 00000000 00:00 0          [stack]
0.1.Aborted


Any insight would be appreciated!

Thanks!
1
2
3
4
5
for (int i = 0; i < x; i++) {
  cout << i << ".";
  delete [] m; // <- crash
}
delete []m;
Last edited on
ne555 (255) said

1
2
3
4
5
for (int i = 0; i < x; i++) {
  cout << i << ".";
  delete [] m; // <- crash
}
delete []m;


Duh'oh!

Thanks!
Topic archived. No new replies allowed.