1

我对使用动态分配的结构数组的非常简单的应用程序有疑问。这是我的代码:

#include <iostream>
#include <string>
using namespace std;

struct MySimpleStuct
{
    string text;
    int intVal;
    double doubleVal;
};

int main(int argc, char **argv)
{
    int N;
    cin >> N;
    MySimpleStuct *s = new MySimpleStuct[N];
    delete s;
    return 0;
}

我得到了这些错误:

*** glibc detected *** ./a.out: munmap_chunk(): invalid pointer: 0x08f3c00c ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb74d5ee2]
/lib/i386-linux-gnu/libc.so.6(+0x765c5)[0xb74d65c5]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_ZdlPv+0x1f)[0xb76d251f]
./a.out[0x80487bf]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb74794d3]
./a.out[0x80486a1]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:07 393313     /home/katie/Desktop/Temp/a.out
08049000-0804a000 r--p 00000000 08:07 393313     /home/katie/Desktop/Temp/a.out
0804a000-0804b000 rw-p 00001000 08:07 393313     /home/katie/Desktop/Temp/a.out
08f3c000-08f5d000 rw-p 00000000 00:00 0          [heap]
b7432000-b7434000 rw-p 00000000 00:00 0 
b7434000-b745e000 r-xp 00000000 08:05 659543     /lib/i386-linux-gnu/libm-2.15.so
b745e000-b745f000 r--p 00029000 08:05 659543     /lib/i386-linux-gnu/libm-2.15.so
b745f000-b7460000 rw-p 0002a000 08:05 659543     /lib/i386-linux-gnu/libm-2.15.so
b7460000-b7603000 r-xp 00000000 08:05 655384     /lib/i386-linux-gnu/libc-2.15.so
b7603000-b7604000 ---p 001a3000 08:05 655384     /lib/i386-linux-gnu/libc-2.15.so
b7604000-b7606000 r--p 001a3000 08:05 655384     /lib/i386-linux-gnu/libc-2.15.so
b7606000-b7607000 rw-p 001a5000 08:05 655384     /lib/i386-linux-gnu/libc-2.15.so
b7607000-b760b000 rw-p 00000000 00:00 0 
b760b000-b7627000 r-xp 00000000 08:05 656309     /lib/i386-linux-gnu/libgcc_s.so.1
b7627000-b7628000 r--p 0001b000 08:05 656309     /lib/i386-linux-gnu/libgcc_s.so.1
b7628000-b7629000 rw-p 0001c000 08:05 656309     /lib/i386-linux-gnu/libgcc_s.so.1
b7629000-b7701000 r-xp 00000000 08:05 791499     /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b7701000-b7702000 ---p 000d8000 08:05 791499     /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b7702000-b7706000 r--p 000d8000 08:05 791499     /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b7706000-b7707000 rw-p 000dc000 08:05 791499     /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
b7707000-b770e000 rw-p 00000000 00:00 0 
b7724000-b7728000 rw-p 00000000 00:00 0 
b7728000-b7729000 r-xp 00000000 00:00 0          [vdso]
b7729000-b7749000 r-xp 00000000 08:05 659544     /lib/i386-linux-gnu/ld-2.15.so
b7749000-b774a000 r--p 0001f000 08:05 659544     /lib/i386-linux-gnu/ld-2.15.so
b774a000-b774b000 rw-p 00020000 08:05 659544     /lib/i386-linux-gnu/ld-2.15.so
bf92e000-bf94f000 rw-p 00000000 00:00 0          [stack]
Aborted (core dumped)

甚至不知道为什么?当我不删除 *s 时,一切都是正确的,但它不是解决方案......

4

2 回答 2

3

您使用new/delete, new[]/delete[]不正确,您应该delete [];在新数组时调用:

MySimpleStuct *s = new MySimpleStuct[N];
delete [] s;
于 2013-01-16T11:16:01.927 回答
0

如果您对字符串进行注释,则该错误应该会消失。

struct MySimpleStuct
{

  //string text;

  int intVal;

  double doubleVal;
};
于 2013-08-29T04:15:25.040 回答