Unexpected Termination

K

KS

I have taken up C++ after about 4 years with Java and I am facing a
problem with the code that I have written. The problem is that the
execution unexpectedly terminates without any error or dump. Are there
any commonly known bugs that can cause this problem?

I would really appreciate it if you would take a look at the source. I
have uploaded the complete source file at:
http://www.grex.org/~kpp/gamultiknapsack.cpp
(You will need the orlib1.txt in the same directory to execute this
file).

Thank you.

KS
 
M

Markus Moll

Hi
I have taken up C++ after about 4 years with Java and I am facing a
problem with the code that I have written. The problem is that the
execution unexpectedly terminates without any error or dump. Are there
any commonly known bugs that can cause this problem?

There are so many known bugs that can cause this problem that I am not going
to list them here.
You should try using a debugger (with a debug build) to find out which one
you are affected by.

Markus
 
H

Howard

KS said:
I have taken up C++ after about 4 years with Java and I am facing a
problem with the code that I have written. The problem is that the
execution unexpectedly terminates without any error or dump. Are there
any commonly known bugs that can cause this problem?

Are you sure there's a problem? On my machine, when I run from the IDE, I
need to add a cin statement (or something similar) at the end of the program
to keep the window open so I can read the output. Otherwise, it finishes
the execution normally, then closes the console window before I can read
what it wrote.

If you're sure there's a problem, then use your debugger to step through and
see where something goes wrong (or at least post a minimal compilable
example which demonstrates the problem that we can look at here).

-Howard
 
B

BigBrian

KS said:
I have taken up C++ after about 4 years with Java and I am facing a
problem with the code that I have written. The problem is that the
execution unexpectedly terminates without any error or dump. Are there
any commonly known bugs that can cause this problem?

There are many ways to write buggy code. There's no way to tell which
bug you have implemented.
I would really appreciate it if you would take a look at the source. I
have uploaded the complete source file at:
http://www.grex.org/~kpp/gamultiknapsack.cpp
(You will need the orlib1.txt in the same directory to execute this
file).

ICK. I didn't really look at the code to close, but this is (some of)
what goes to STDERR when I run the executable built from your source.


a.out(3939) malloc: *** Deallocation of a pointer not malloced:
0x500520; This could be a double free(), or free() called with the
middle of an allocated block; Try setting environment variable
MallocHelp to see tools to help debug
a.out(3939) malloc: *** error for object 0x500570: double free
a.out(3939) malloc: *** set a breakpoint in szone_error to debug
a.out(3939) malloc: *** Deallocation of a pointer not malloced:
0x500520; This could be a double free(), or free() called with the
middle of an allocated block; Try setting environment variable
MallocHelp to see tools to help debug
a.out(3939) malloc: *** error for object 0x500570: double free
a.out(3939) malloc: *** set a breakpoint in szone_error to debug
a.out(3939) malloc: *** Deallocation of a pointer not malloced:
0x500520; This could be a double free(), or free() called with the
middle of an allocated block; Try setting environment variable
MallocHelp to see tools to help debug
a.out(3939) malloc: *** error for object 0x500570: double free
a.out(3939) malloc: *** set a breakpoint in szone_error to debug
a.out(3939) malloc: *** Deallocation of a pointer not malloced:
0x500520; This could be a double free(), or free() called with the
middle of an allocated block; Try setting environment variable
MallocHelp to see tools to help debug
a.out(3939) malloc: *** error for object 0x500570: double free
a.out(3939) malloc: *** set a breakpoint in szone_error to debug
a.out(3939) malloc: *** Deallocation of a pointer not malloced:
0x500520; This could be a double free(), or free() called with the
middle of an allocated block; Try setting environment variable
MallocHelp to see tools to help debug
 
B

BigBrian

Howard said:
Are you sure there's a problem?

Oh, there are problems....
There's code like this...


class KNode
{
public:
int * knapsack;

KNode() {}
KNode( int * bla ) { knapsack = bla; }
~KNode() { delete knapsack; }
};

knapsack is unitilized if the object is default constructed, and then
its deleted in the destructor.

This is just one of the many problems with this code.

-Brian
 
C

Cy Edmunds

KS said:
I have taken up C++ after about 4 years with Java and I am facing a
problem with the code that I have written. The problem is that the
execution unexpectedly terminates without any error or dump. Are there
any commonly known bugs that can cause this problem?

I would really appreciate it if you would take a look at the source. I
have uploaded the complete source file at:
http://www.grex.org/~kpp/gamultiknapsack.cpp
(You will need the orlib1.txt in the same directory to execute this
file).

Thank you.

KS

I would advise you to recode your program without using operator new at all.
Use std::vector for all arrays. In C++ you can't return an array but you can
return a std::vector.

Don't write a destructor unless you know what you're doing. KNode, for
example, will cause undefined behavior if you just copy an instance. The
pointers get deleted twice. That's because you have a destructor but no copy
constructor or assignment operator. (Look up "rule of three".) If you follow
my advice about std::vector you will not have to write any destructors.

If you insist on allocating C-style arrays, make sure you delete them with
delete [] not just plain delete. In other words:

int *p = new int[3];
delete [] p;

Using delete p; here is undefined behavior.

Cy
 
K

KS

Oh, there are problems....
knapsack is unitilized if the object is default constructed, and then
its deleted in the destructor.

This is just one of the many problems with this code.

-Brian

Yes that was the problem. Thank you for the replies.
(The mingw-g++ produced executable just terminates without any error
message)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top