Segmentation fault if I dont use printf?

A

aamircheema

Hi,

I have written a program (my first big program in c++). When I run the
program it gives segmentation fault but when i use a printf statement
to debug the program, it runs normally. I am very confused that what
may be the reason?
Of course I dont want to use printf because it is in loop and prints so
many things which I don't want.

Any Ideas?

Thanks,
Aamir
 
M

mlimber

Hi,

I have written a program (my first big program in c++). When I run the
program it gives segmentation fault but when i use a printf statement
to debug the program, it runs normally. I am very confused that what
may be the reason?
Of course I dont want to use printf because it is in loop and prints so
many things which I don't want.

Any Ideas?

Yes: it is likely you have a wayward pointer or are going past your
array bounds. The printf masks it (because your program's code and data
layout is changed by the presence of that line), but the problem is
still there. Delve into the program with your debugger to see what you
can find, and/or try to reduce the program to a *minimal* but
*complete* program that demonstrates the problem and that you can post
here for us to inspect.

Cheers! --M
 
P

Puppet_Sock

I have written a program (my first big program in c++). When I run the
program it gives segmentation fault but when i use a printf statement
to debug the program, it runs normally. I am very confused that what
may be the reason?
Of course I dont want to use printf because it is in loop and prints so
many things which I don't want.

Any Ideas?

Round up the usual suspects:
- wild pointer or iterator
- off-the-end error

So look for things like:
- bad pointer arithemetic (pa + some value that is past the memory
you allocated, or pa[someval] that is the same)
- deleting an object, then referring to it again
- slicing an object due to inheritance and referring to it through a
pointer of the wrong type
- check any type casts you've done, especially old C-type casts
(Think about not doing that if possible.)
- actions on template collections (list, vector, map, etc.) that make
an iterator invalid, then still using the iterator

Also, be sure to turn your compiler to its most picky setting. It may
catch some bad things for you. Even if it does not catch this specific
bug it is a good policy if you can.
Socks
 
A

aamir

Noah said:

Thanks everybody for the reply.

After using a debugger it showed me some memory leaks but I am
unable to understand this one. Debugger message is

==11474== Use of uninitialised value of size 8
==11474== at 0x804F03B: FindTree(AnswerPoint, AvlNode*)
(avltree.cc:65)
==11474== by 0x8049BED: main (prog2.cc:652)

And the part of avltree.cc looks like

=======================================================
Position_tree
FindTree( AnswerPoint X, AvlTree T )
{
if( T == NULL )
{
return NULL;
}
if( X.distance < T->Element.distance ) // ==>debugger is
talking about this line
return FindTree( X, T->Left );

if( X.distance > T->Element.distance )
return FindTree( X, T->Right );
if(X.distance==T->Element.distance)
{
if(X.pid==T->Element.pid)
return T;
else
{
if(X.pid>T->Element.pid)
return FindTree(X,T->Right);
else
return FindTree(X,T->Left);
}
}
return T;
}
================================================

The line about which debugger is talking is shown in the above code.
But I have made it pretty sure that in prog.cc at line 652 , the
distance value is not unitialized.

Do you get some idea of where the error may be?
Can I use some if statement to check if the value is unitialized or
not?
e.g; can i say
if(X.distance==NULL)
printf("No value in
X.distance");

X.distance is a double value.

Thanks again for your help.
Regards,
Aamir
 
N

Noah Roberts

aamir said:
=======================================================
Position_tree
FindTree( AnswerPoint X, AvlTree T )

Did you realize you are passing by value?
if( X.distance < T->Element.distance ) // ==>debugger is
talking about this line
The line about which debugger is talking is shown in the above code.
But I have made it pretty sure that in prog.cc at line 652 , the
distance value is not unitialized.

Depends on what 'distance' is and what the copy constructor is doing.
 
A

aamir

Did you realize you are passing by value?

Sorry but I didn't understand what do you mean.
Depends on what 'distance' is and what the copy constructor is doing.

distance is a double value, and I don't know what else I should add to
give you more understanding of the problem :s
 
P

Paul

aamir said:
Sorry but I didn't understand what do you mean.

FindTree( AnswerPoint X, AvlTree T )

You are passing the AnswerPoint by value, and the AvlTree by value. This
means that the compiler will make copies of these objects when passed to the
function. Do you want that to occur?

Unless you have a compelling reason to do so, these should be passed by
(const) reference. Then you don't incur the penalty of the compiler making
copies of these objects.

FindTree( AnswerPoint& X, AvlTree& T )

Which begs the question -- You wrote somewhat non-trivial code, and you are
not aware of pass-by-value as opposed to pass-by-reference? Knowledge of
how parameters are passed in C++ is fundamental to writing proper C++
programs.

Paul
 
N

Noah Roberts

aamir said:
Sorry but I didn't understand what do you mean.


distance is a double value, and I don't know what else I should add to
give you more understanding of the problem :s

You should always do as the FAQ says unless there is some compelling
reason why you can't. The FAQ says to provide the minimal amound of
code that is complete and exhibits your problem. In other words, it
compiles and breaks in the way you are asking about.

If your copy constructor is not initializing your variable that could
be what your debugger is getting mad about. You are passing the
class/struct by value so it is copied. It is not the original one you
passed in. Is all the data being copied over or is your copy
constructor missing stuff? I don't know...

All I can see is that it is used and I have your assurance that you
think that value is initialized by the time it gets there. However,
you don't know what I mean about passing by value, which isn't a big
thing because you are learning (well, it is important) but it means you
very well might not know if the value is initialized or not.

Provide just enough that someone can copy and paste your code into a
file and compile it. Make sure it also exhibits your problem. Maybe
in so separating out the code you will find the answer yourself...its
happened to me hundreds of times.
 
O

Old Wolf

Paul said:
FindTree( AnswerPoint X, AvlTree T )

You are passing the AnswerPoint by value, and the AvlTree by value. This
means that the compiler will make copies of these objects when passed to the
function. Do you want that to occur?

Actually, AvlTree is a typedef for AvlNode* , as is apparent from
the original post.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top