gpt4 book ai didi

c++ - 损坏的双链表 : 0x0804d0c0 (C++)?

转载 作者:太空宇宙 更新时间:2023-11-04 15:26:42 27 4
gpt4 key购买 nike

注意:我以前从来没有遇到过这种规模的问题,也不知道如何布局,如果我布局错误,请解释我应该如何布局。如果您不能或不愿意帮助该程序,我很乐意了解这个问题。

我一直在写一段代码来练习面向对象的编程,但是我遇到了一个我以前从未见过的错误(我只做了几个月并且没有得到很多没有时间学习,因为我的时间都浪费在了义务教育上)。内核本身(显然)在我的代码中产生了这个错误。但是运行 gdb(带有调试符号)并没有让我知道行号。放入 printf 语句显示它正在循环 while 循环,即使不再满足条件?这对我来说似乎很奇怪,因为它似乎不满足条件,但它一直在循环。它在发生这种情况时出现错误的原因(我认为)是因为在“Stack”类中的某个地方它为整数数组重新分配了负大小的内存 - 虽然,当我向 Stack::reloc 添加一个 if 语句以便防止它重新分配负数数组大小它仍然通过错误。往下看它显示了其他几条信息,(引用 gdb):

程序收到信号 SIGABRT,已中止。__kernel_vsyscall() 中的 0x0012d422

我并没有从 SIGABRT 的 wiki 页面学到很多东西,而且我认为十六进制数对我来说没有任何意义。这是我的代码,分为两个类和主要部分:

标题:

#include <cstdio>
#include <cstdlib>

类堆栈:

class Stack {
private:
int *stack;
int size;
void init(int *top) {
*top = 0;
}

void push2(int *s,int* top, int element) {
s[(*top)++] = element;
}

int pop2(int *s,int *top) {
return s[--(*top)];
}
void reloc(int diff) {
if ((top+diff)>1) {
realloc(stack, (size+diff));
size = size+diff;
}
}

public:
int top;
Stack() {
init(&top);
size = 100;
stack = (int *)malloc(100 * sizeof(int));
}
void push(int element) {
if (top>=(size-1)) reloc(100);
push2(stack, &top, element);
}
int pop() {
if ((top<(size-120)) && (top>100)) reloc(-100);
return (pop2(stack, &top));
}
int sth() {
return size;
}

};

宇宙类:

/*Universe is the casual layout for the universe*/
class Universe {
private:
int y_cog; //y axis center of gravity
int x_cog; //x axis "
int force; //weight of universe
int fps; //frames per second
int g; //acceleration under gravity
int G; //gravitational constant

//NB: velocity in d/s
//all stacks defined
Stack y_pos; //y axis position
Stack x_pos; //x axis "
Stack mass; //mass of object
Stack y_vel; //velocity on y axis
Stack x_vel; //velocity on x axis
Stack y_pos2; //stacks for repeating
Stack x_pos2;
Stack mass2;
Stack y_vel2;
Stack x_vel2;

//function 'increments' the center of gravity
void cog(int y, int x, int m) {
//ratio is offset of universe cog
//where 100 is exectly half way
int ratio = ((m*100)/(force))/2;
//set the center of gravity the ratio beween the objects
y_cog = y_cog-(((y_cog-y)/100)*ratio);
x_cog = x_cog-(((x_cog-x)/100)*ratio);
force = m+force;
}

/*moves the object at the top of the stack and alters velocity
according to previouse velocity and force towards the universe
center of gravity*/
void move() {
int yp, xp, m, yv, xv, vi, yvi, xvi, rm;
yp = y_pos2.pop();
xp = x_pos2.pop();
m = mass2.pop();
yv = y_vel2.pop();
xv = x_vel2.pop();
//important part
//set the position according to velocity and frame rate
yp = yp+(yv/fps);
xp = xp+(xv/fps);
//find the ratio share of velocity
vi = ((g*100)/fps); //velocity increase
rm = ((y_cog-yp)+(x_cog-xp)); //ratio maximum
if (rm==0) rm = 1;
yvi = ((vi/rm)*(y_cog-yp))/100;
xvi = ((vi/rm)*(x_cog-xp))/100;
yv = yvi;
xv = xvi;
//push everything back
y_pos.push(yp);
x_pos.push(xp);
mass.push(m);
y_vel.push(yv);
x_vel.push(xv);
}

public:
Universe() {
y_cog = 0;
x_cog = 0;
force = 0;
fps = 10;
g = 10;
//all stacks initialised
y_pos = Stack();
x_pos = Stack();
mass = Stack();
y_vel = Stack();
x_vel = Stack();
}

void add(int yp, int xp, int m, int yv, int xv) { //adds object to stack
y_pos.push(yp);
x_pos.push(xp);
mass.push(m);
y_vel.push(yv);
x_vel.push(xv);
}

void run() {
int yp, xp, m, yv, xv;
while (y_pos.top<=0) { //reposition center of gravity
//mainly pushing things around
yp = y_pos.pop();
xp = x_pos.pop();
m = mass.pop();
yv = y_vel.pop();
xv = x_vel.pop();
cog(yp, xp, m); //the important part
y_pos2.push(yp);
x_pos2.push(xp);
mass2.push(m);
y_vel2.push(yv);
x_vel2.push(xv);
}
while (y_pos2.top<1) {
move();
}
}

void outh() { //human output
int yp, xp, m, yv, xv;
while (y_pos.top<=0) {
yp = y_pos.pop();
xp = x_pos.pop();
m = mass.pop();
yv = y_vel.pop();
xv = x_vel.pop();
y_pos2.push(yp);
x_pos2.push(xp);
mass2.push(m);
y_vel2.push(yv);
x_vel2.push(xv);
printf("%d, %d\t%d\t%d\n", yp, xp, m, yv+xv);
}
while (y_pos2.top<=0) {
y_pos.push(y_pos2.pop());
x_pos.push(x_pos2.pop());
mass.push(mass.pop());
y_vel.push(y_vel.pop());
x_vel.push(x_vel.pop());
}
}
};

主要功能:

int main() {
Universe main = Universe();
main.add(10, 10, 10, 0, 0);
main.add(5, 5, 5, 0, 0);
int i;
for (i = 1; i<100; i++) {
main.run();
main.outh();
printf("\n");
}
return 0;
}

GDB运行结果(带调试符号):

Starting program: /home/james/Desktop/coding/Universe/debug 
*** glibc detected *** /home/james/Desktop/coding/Universe/debug: corrupted double-linked list: 0x0804d0c0 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(+0x6b591)[0x2d4591]
/lib/tls/i686/cmov/libc.so.6(+0x70ce3)[0x2d9ce3]
/lib/tls/i686/cmov/libc.so.6(realloc+0xdd)[0x2d9f9d]
/home/james/Desktop/coding/Universe/debug[0x80486b2]
/home/james/Desktop/coding/Universe/debug[0x804872c]
/home/james/Desktop/coding/Universe/debug[0x80489b0]
/home/james/Desktop/coding/Universe/debug[0x8048cec]
/home/james/Desktop/coding/Universe/debug[0x80485fd]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x27fbd6]
/home/james/Desktop/coding/Universe/debug[0x80484d1]
======= Memory map: ========
00110000-0012b000 r-xp 00000000 08:01 393241 /lib/ld-2.11.1.so
0012b000-0012c000 r--p 0001a000 08:01 393241 /lib/ld-2.11.1.so
0012c000-0012d000 rw-p 0001b000 08:01 393241 /lib/ld-2.11.1.so
0012d000-0012e000 r-xp 00000000 00:00 0 [vdso]
0012e000-00217000 r-xp 00000000 08:01 2625203 /usr/lib/libstdc++.so.6.0.13
00217000-00218000 ---p 000e9000 08:01 2625203 /usr/lib/libstdc++.so.6.0.13
00218000-0021c000 r--p 000e9000 08:01 2625203 /usr/lib/libstdc++.so.6.0.13
0021c000-0021d000 rw-p 000ed000 08:01 2625203 /usr/lib/libstdc++.so.6.0.13
0021d000-00224000 rw-p 00000000 00:00 0
00224000-00248000 r-xp 00000000 08:01 525255 /lib/tls/i686/cmov/libm-2.11.1.so
00248000-00249000 r--p 00023000 08:01 525255 /lib/tls/i686/cmov/libm-2.11.1.so
00249000-0024a000 rw-p 00024000 08:01 525255 /lib/tls/i686/cmov/libm-2.11.1.so
0024a000-00267000 r-xp 00000000 08:01 393299 /lib/libgcc_s.so.1
00267000-00268000 r--p 0001c000 08:01 393299 /lib/libgcc_s.so.1
00268000-00269000 rw-p 0001d000 08:01 393299 /lib/libgcc_s.so.1
00269000-003bc000 r-xp 00000000 08:01 525247 /lib/tls/i686/cmov/libc-2.11.1.so
003bc000-003bd000 ---p 00153000 08:01 525247 /lib/tls/i686/cmov/libc-2.11.1.so
003bd000-003bf000 r--p 00153000 08:01 525247 /lib/tls/i686/cmov/libc-2.11.1.so
003bf000-003c0000 rw-p 00155000 08:01 525247 /lib/tls/i686/cmov/libc-2.11.1.so
003c0000-003c3000 rw-p 00000000 00:00 0
08048000-0804a000 r-xp 00000000 08:01 132105 /home/james/Desktop/coding/Universe/debug
0804a000-0804b000 r--p 00001000 08:01 132105 /home/james/Desktop/coding/Universe/debug
0804b000-0804c000 rw-p 00002000 08:01 132105 /home/james/Desktop/coding/Universe/debug
0804c000-0806d000 rw-p 00000000 00:00 0 [heap]
b7e00000-b7e21000 rw-p 00000000 00:00 0
b7e21000-b7f00000 ---p 00000000 00:00 0
b7fed000-b7fef000 rw-p 00000000 00:00 0
b7ffe000-b8000000 rw-p 00000000 00:00 0
bffeb000-c0000000 rw-p 00000000 00:00 0 [stack]

Program received signal SIGABRT, Aborted.
0x0012d422 in __kernel_vsyscall ()

最佳答案

一个大问题,realloc() 行应该是:

stack = realloc(stack, (size+diff));

关于c++ - 损坏的双链表 : 0x0804d0c0 (C++)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7137952/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com