Solved

segmentation faults

Posted on 2003-12-02
9
326 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
Comment Utility
Hi thunderflash,

In which line are you getting the segmentation fault?

Cheers!
0
 

Author Comment

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

Expert Comment

by:tinchos
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 2

Accepted Solution

by:
sin_ earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Is bigInt::SIZE a static variable? How is it declared?
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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++.

762 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

13 Experts available now in Live!

Get 1:1 Help Now