Solved

segmentation faults

Posted on 2003-12-02
9
327 Views
Last Modified: 2010-04-01
I am writing a bigInt class and i am having trouble overloading the << operator. here is my code:

ostream& operator<<(ostream& out, const bigInt& num)
{
  int temp;
  for (int i = 0; i < bigInt::SIZE; i++) {
    temp = i;
    if (temp%3==0) // comma every thousand
      cout<<",";
    cout<<num.digit[i];
  }
}

the code results in a segmentation fault. any ideas?
0
Comment
Question by:thunderflash
9 Comments
 
LVL 9

Expert Comment

by:tinchos
ID: 9863775
Hi thunderflash,

In which line are you getting the segmentation fault?

Cheers!
0
 

Author Comment

by:thunderflash
ID: 9863892
i don't know. i was wondering if someone else could spot it
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9863900
If I were you I would try debugging it, going line through line, so as to know exactly in which line it throws the error

From that method at first sight I would make sure that num.digit[i] exists

so, check that num != null and that digit have at least bigInt::SIZE elements

Hope this helps

Tincho
0
 
LVL 15

Expert Comment

by:efn
ID: 9863949
I don't see anything that would cause this problem in the code you posted.  It might help if we could see the code for the bigInt class so we don't have to guess what bigInt::SIZE and num.digit[i] are.

Aside from that, there are a couple of obvious problems.  The function is declared to return a reference to an ostream and it doesn't.  I would expect the compiler to complain about this.  And normally with a function like this, one would expect it to put out data to the parameter stream, but this implementation ignores the parameter stream and puts out data to cout.  It would be better to send the output to the out parameter stream.

--efn
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Accepted Solution

by:
sin_ earned 50 total points
ID: 9864702
Just wondering why are you doing this....
You should be rather making it like this:
ostream& operator<<(ostream& out, const bigInt& num)
{
  int temp;
  for (int i = 0; i < bigInt::SIZE; i++) {
    temp = i;
    if (temp%3==0) // comma every thousand
      out<<",";
    out<<num.digit[i];
  }
  return out;
}

0
 
LVL 13

Expert Comment

by:SteH
ID: 9865366
What is temp needed for?

ostream& operator<<(ostream& out, const bigInt& num)
{
  for (int i = 0; i < bigInt::SIZE; i++) {
    if (i%3==0) // comma every thousand
      out<<",";
    out<<num.digit[i];
  }
  return out;
}

should do the same.
0
 
LVL 13

Expert Comment

by:SteH
ID: 9865371
Is bigInt::SIZE a static variable? How is it declared?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9865456
One other minor flaw... it looks like 10,000 will be displayed as:

    ,100,00

...which might knock a coupla points off yer grade :)

-- Dan
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9868138
Do you have Visual C++? If yes, then run the program... it will halt on a segfault and take you to the debugging screen. Then you'll know exactly where the segfault occured.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now