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
Solved

segmentation faults

Posted on 2003-12-02
9
330 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

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

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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.

840 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