Solved

segmentation faults

Posted on 2003-12-02
9
332 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
Technology Partners: 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

717 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