i have a code written in c++ , it works correctly on visual studio but when i transfer it to linux (QT Designer) i get a run time error shown below:
*** glibc detected *** /home/hfarazi/Test/QT/Test-build-desktop/Test: malloc(): memory corruption: 0x082c2c80 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x6ff22)[0x42cf22]
/lib/i386-linux-gnu/libc.so.6(+0x718be)[0x42e8be]
/lib/i386-linux-gnu/libc.so.6(__libc_malloc+0x68)[0x4307f8]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_Znwj+0x27)[0x2c89d7]
/usr/lib/i386-linux-gnu/libstdc++.so.6(_Znaj+0x1b)[0x2c8afb]
/home/hfarazi/Test/QT/Test-build-desktop/Test[0x804abdc]
/home/hfarazi/Test/QT/Test-build-desktop/Test[0x8062860]
/home/hfarazi/Test/QT/Test-build-desktop/Test[0x80644b5]
/home/hfarazi/Test/QT/Test-build-desktop/Test[0x806e449]
/home/hfarazi/Test/QT/Test-build-desktop/Test[0x80604d5]
/home/hfarazi/Test/QT/Test-build-desktop/Test[0x8049205]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x3d6113]
/home/hfarazi/Test/QT/Test-build-desktop/Test[0x8049351]
======= Memory map: ========
00219000-002f7000 r-xp 00000000 07:00 527926 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
002f7000-002f8000 ---p 000de000 07:00 527926 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
002f8000-002fc000 r--p 000de000 07:00 527926 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
002fc000-002fd000 rw-p 000e2000 07:00 527926 /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
002fd000-00304000 rw-p 00000000 00:00 0
00339000-00357000 r-xp 00000000 07:00 266017 /lib/i386-linux-gnu/ld-2.13.so
00357000-00358000 r--p 0001d000 07:00 266017 /lib/i386-linux-gnu/ld-2.13.so
00358000-00359000 rw-p 0001e000 07:00 266017 /lib/i386-linux-gnu/ld-2.13.so
003a2000-003b9000 r-xp 00000000 07:00 273114 /lib/i386-linux-gnu/libpthread-2.13.so
003b9000-003ba000 r--p 00016000 07:00 273114 /lib/i386-linux-gnu/libpthread-2.13.so
003ba000-003bb000 rw-p 00017000 07:00 273114 /lib/i386-linux-gnu/libpthread-2.13.so
003bb000-003bd000 rw-p 00000000 00:00 0
003bd000-00535000 r-xp 00000000 07:00 273100 /lib/i386-linux-gnu/libc-2.13.so
00535000-00537000 r--p 00178000 07:00 273100 /lib/i386-linux-gnu/libc-2.13.so
00537000-00538000 rw-p 0017a000 07:00 273100 /lib/i386-linux-gnu/libc-2.13.so
00538000-0053b000 rw-p 00000000 00:00 0
00777000-0079f000 r-xp 00000000 07:00 273104 /lib/i386-linux-gnu/libm-2.13.so
0079f000-007a0000 r--p 00028000 07:00 273104 /lib/i386-linux-gnu/libm-2.13.so
007a0000-007a1000 rw-p 00029000 07:00 273104 /lib/i386-linux-gnu/libm-2.13.so
00bf7000-00bf8000 r-xp 00000000 00:00 0 [vdso]
00df3000-00e0f000 r-xp 00000000 07:00 262077 /lib/i386-linux-gnu/libgcc_s.so.1
00e0f000-00e10000 r--p 0001b000 07:00 262077 /lib/i386-linux-gnu/libgcc_s.so.1
00e10000-00e11000 rw-p 0001c000 07:00 262077 /lib/i386-linux-gnu/libgcc_s.so.1
08048000-08075000 r-xp 00000000 07:00 401931 /home/hfarazi/Test/QT/Test-build-desktop/Test
08075000-08076000 r--p 0002c000 07:00 401931 /home/hfarazi/Test/QT/Test-build-desktop/Test
08076000-08077000 rw-p 0002d000 07:00 401931 /home/hfarazi/Test/QT/Test-build-desktop/Test
082ba000-082db000 rw-p 00000000 00:00 0 [heap]
b7600000-b7621000 rw-p 00000000 00:00 0
b7621000-b7700000 ---p 00000000 00:00 0
b77f4000-b77f8000 rw-p 00000000 00:00 0
b780a000-b780d000 rw-p 00000000 00:00 0
bff44000-bff65000 rw-p 00000000 00:00 0 [stack]
could you help me finding the error! and as it is a company project i could not easily send the code. thanx in advance.
ithink the problem is in this class , an idea?
#define NOT_VALID NULL
#define STACK_INITIAL_ALLOC 10
#define STACK_CHUNK_ALLOC 10
template<class T>
class stack
{
public:
stack()
: value(0), length(0), allocated(0)
{ }
stack(const T &value)
: value(0), length(0), allocated(0)
{
push(value);
}
~stack()
{
if (value)
delete [] value;
}
void push(const T &_value)
{
if (length == allocated)
allocate(); // Allocate more memory
value[length++] = _value;
}
T &pop()
{
if (length > 0)
{
//shrink();
length--;
return value[length];
}
}
T &peak()
{
if (length > 0)
return value[length - 1];
//else
// throw runtime_error("stack empty");
}
// Return the number of length in the stack
size_t count() const
{
return length;
}
size_t get_allocated() const
{
return allocated;
}
private:
T *value; // The actual stack
size_t length; // Number of length in stack
size_t allocated; // Allocated length in stack
void copy(T *from, T *to)
{
for (size_t i = 0; i < length; i++)
*to++ = *from++;
}
void allocate()
{
if (value == 0)
{
allocated = STACK_INITIAL_ALLOC;
value = new T[allocated];
}
else
{
// We need to allocate more memory
size_t new_allocated = allocated + STACK_CHUNK_ALLOC;
T *new_value = new T[new_allocated];
// Copy from old stack to new stack
copy(value, new_value);
// Delete the old value
delete [] value;
allocated = new_allocated;
value = new_value;
}
}
// Shrink the stack, if lots of it is unused
void shrink()
{
// The limit which the number of length must be lower than
size_t shrink_limit = allocated - STACK_CHUNK_ALLOC * 2;
// Do not shrink if we will get lower than the initial allocation (shrink_limit > STACK_INITIAL_ALLOC)
if (shrink_limit > STACK_INITIAL_ALLOC && length < shrink_limit)
{
// We can shrink the allocated memory a little
size_t new_allocated = allocated - STACK_CHUNK_ALLOC;
T *new_value = new T[new_allocated];
copy(value, new_value);
delete [] value;
value = new_value;
allocated = new_allocated;
}
}
};
If you're not dealing with threads then most probably this kind of problem may occur if you are somehow reading/writing out of array bounds.
As the result for this kind operations is undefined then the problem may not be seen in some specific host/compilator and may occur only when you transfer your codes to another host.
Sometimes this kind of problems are really hard to figure out if your program is huge.
One more advise beside of using debuggers is to eliminate part of your codes (if possible) and try to narrow the place where the problem comes from.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.