Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Segmentation error

Posted on 2003-03-21
Medium Priority
Last Modified: 2011-04-14
I have written a program using class and I dont know why the program got a segmentation error.
here is my program:

#include <iostream.h>
#include <string>
#include "StudentInfo.h"
#include "class.cpp"

using namespace std;

int end;
string* buffer = new string[100];
StudentInfo Info[100];
void input(); // function for inputting data from "Input.dat" to the array of object Info
void menu(); //function for displaying menu for user input

int main()
  delete [] buffer;
 } ///////the main program ends here ///////////////////////////////////////////

StudentInfo StudentInfo::unionOfStudentSets(string* buffer1,StudentInfo Info2[100],string ma1, string ma2,int end1)
  int i,k=0;
  for (i=0; i<end1; i++)
    cout<<Info2[i].getName()<<" "<<Info2[i].getAddress()<<" "<<Info2[i].getMajor1()
        <<" "<<Info2[i].getMajor2()<<endl;
  for (i=0; i<end1; i++)
     if ((Info2[i].getMajor1() == ma1) || (Info2[i].getMajor1() == ma2) || (Info2[i].getMajor2() == ma1) || (Info2[i].getMajor2() == ma2))
          *(buffer1+1) = Info2[i].getName();
   cout<<"k "<<k<<endl;  
   for (i=0; i<100; i++)
   if (*(buffer1+i) != "")
   cout<<"buffer1 "<<i<<" "<<*(buffer1+i)<<endl;
   StudentInfo::Print(buffer1,k );

The problem should be inside the function unionOfStudentSets since if i delete this part my program doesnt have any problem.

Question by:LittlePeanut
  • 2

Accepted Solution

unknownmat earned 150 total points
ID: 8185771
Ok, I have a question related with your global variable 'end'...

1.  Does your input() function set it to anything?  If not, you should fix that (and if that is not the thing causing your seg-fault now, it will definately be a source of trouble eventually).

If input() does set end, then what value do you set it to? ... if that value is >= 100 that will cause you problems as well.

Finally ... I may be way out of line here but your code is incredibly confusing.  
 - You seem to randomly use global variables which makes it hard to track what things in your program are doing
 - Your variable names do little to suggest the function of the variables (a good rule-of-thumb is that you never differentiate your varaibles by numbered postfixes, ie. Info, Info2, buffer, buffer1, etc.)...
 - Finally, *( buffer1 + i ) is EXACTLY the same as buffer1[ i ] ... why do you insist on using pointer notation?


Expert Comment

ID: 8185775
Oh yeah, one other thing ... this code you wrote, did it compile?


This line only passes 4 variables to a function that expects 5...  Without "StudentInfo.h" and "class.cpp" I cannot verify that what I am saying is correct, but I would be very suspicious if that did compile... You might want to try changing that line to:

Info[ 0 ].unionOfStudentSets( buffer, Info, "3", "1", end );

Author Comment

ID: 8186292
Thanks for your help.
I checked my code carefully and i found out the problem is caused by my forgetting to delete a dynamically allocated string array in the input function.
I have corrected this error and everything is fine now.
Thanks for your advice and i will definitely use this service again if i encounter any problem in the future.

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

580 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