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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

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