www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/22/15:00:42

From: "Sal" <Sal AT Sal DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Program Crashes!!! Please help! Memory errors!!!
Date: Mon, 22 Jun 1998 11:23:30 -0600
Lines: 101
NNTP-Posting-Host: 129.37.218.96
Message-ID: <358e8ecb.0@news2.ibm.net>
Organization: IBM.NET
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

OK....  I wrote this little program to sort a set of names and corresponding
numbers.  It compiles fine... but when I run it it crashes.  It usually
crashes in the middle of the data inpput part.  It tells me "Exiting due to
signal SIGSEGV       General Protection Fault at eip=000058dc ........".
Sometimes it gets past the data input part and gives me an "out of memory"
error.  Sometimes it just prints out a bunch of garbage and hangs.  I'm new
to C++ so excuse my ignorance of these things.  Whenever I used to program
in Pascal I never had any type of memory problems...

Here is the code; please tell me what you think.

----------------------------------------------------------------------------

# include <fstream>
# include <string>
void swapnum (int &x, int &y)
{
 int temp;
 temp=x;
 x=y;
 y=temp;
}
void swapstring (string &a, string &b)
{
 string temp2;
 temp2 = a;
 a=b;
 b=temp2;
}

void getnum (int &num)
{
 cout << "How many items to sort?? ";
 cin >> num;
 cout << endl;
}

void getdata (string datast[], int data[], int num)
{

 for (int i=0; i<=num-1; i++)
     {
      cout << "Enter name # "<< i+1 << " : ";
      cin >> datast[i];
      cout << endl;
      cout <<"Enter " << datast[i] <<" 's number : ";
      cin >> data[i];
      cout << endl;
     }
}
void sortst (string datast[], int data[], int num)
{
 for (int i=0; i<=num-1; i++)
      {
      for (int j=i+1; j<=num-1; j++)
          {
           if (datast[i] > datast[j])
             {
              swapstring (datast[i], datast[j]);
              swapnum (data[i], data[j]);
             }
          }
      }
 for (int i=0; i<= num-1 ; i++)
     {
       cout << datast[i] <<" - " << data[i] << endl;
     }
}

void main ()
{
 int num;
 getnum (num);
 string datast[num-1];
 int data[num-1];
 getdata (datast, data, num);
 sortst (datast, data, num);
}


----------------------------------------------------------------------------
----------

Thanks allot!!!


Salvador Santolucito III


                                            o
                          __o         </\_
                          \<        __/\
   /\o_             (()) (())           /
^^^^^^^^^^
"I wish that I could think of an inspiring quote..."
                                 -Salvador Santolucito III





- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019