473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Segmentation fault if I dont use printf?

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

Jun 6 '06 #1
9 8987
aamirche...@gmail.com wrote:
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

Jun 6 '06 #2
aa*********@gmail.com wrote:
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

Jun 6 '06 #3

aa*********@gmail.com wrote:
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.


http://en.wikipedia.org/wiki/Heisenbug#Heisenbugs

Jun 6 '06 #4

Noah Roberts wrote:
aa*********@gmail.com wrote:
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.


http://en.wikipedia.org/wiki/Heisenbug#Heisenbugs


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

Jun 6 '06 #5

aamir wrote:
================================================== =====
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.

Jun 6 '06 #6
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

Jun 6 '06 #7

"aamir" <aa*********@gmail.com> wrote in message
news:11**********************@f6g2000cwb.googlegro ups.com...
Did you realize you are passing by value?


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
Jun 7 '06 #8

aamir wrote:
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


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.

Jun 7 '06 #9
Paul wrote:
Did you realize you are passing by value?


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?


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

Jun 8 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Anks | last post by:
i am unable to find why following code is giving segmentation fault.... way to produce seg fault: run the program... give input 12345678....enter any key except 'x'.... again give 12345678 as...
5
by: Ravikant | last post by:
Hello, Please check the following code .Let me know why its giving the segmentation fault while running it.First it asks to enter the name ,when entered it gives segmentation fault. But if I dont...
6
by: damian birchler | last post by:
If I run the following I get a segmentation fault: #define NAMELEN 15 #define NPERS 10 typedef struct pers { char name; int money; } pers_t;
1
by: Dawn Minnis | last post by:
Hey guys - this code when called with parameters: driver.o n n 12 12 12 12 12 12 2.6 3.2 is kicking back a segmentation fault. I've read the rest of the postings but am still confused. Can...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
8
by: Andrea | last post by:
I wrote this code: void * xmalloc (size_t size){ register void *value = OPENSSL_malloc(size); if (value == 0) printf("virtual memory exhausted"); return value; } int _chooseTSK(char*...
5
by: doni | last post by:
Hi, I am a beginner to C and I wrote a program that takes arguments and prints it in hex. I am getting a segmentation fault if I dont pass any arguments instead I want it to display the Usage...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.