Solved

segmentation faults

Posted on 2003-12-02
9
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

749 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