ERROR: unmapped memory exception

ERROR: unmapped memory exception

Post by Michael N » Sun, 31 Dec 1899 09:00:00



Hi my program is crashing with the subject line error when using
cout << int;.

I believe it happens to be with either this code because when I remove
reference to it in the main function, cout works with integers once
again:

String & operator+(char * st,  String &st2)
{
    char * mString = st2.str;
    String mike(st);
    st2.str = new char[mike.len + st2.len + 2];
    delete [] st2.str;
    strcpy(st2.str, mike.str);
    strcat(st2.str, mString);
    return st2;

Quote:}

or this code although I'm more inclined to believe it the above:

ostream & operator<<(ostream & os, const String & st)
{
    os << st.str;
    return os;

Quote:}

both of which are friend functions.
Everything works fine until I add cout << anyIntType; in main.

Here's the entire source.
If anyone has time I'd sure appreciate some feedback.
Mike

-------------------------------------------------------------
// strng2.h -- String class definition
#ifndef _STRNG3_H_
#define _STRNG3_H_
#include <iostream>
using namespace std;

class String
{
private:
    char * str;                   // pointer to string
    int len;                      // length of string
public:
    String(const char * s);       // constructor
    String();                     // default constructor
    String(const String & st);
    ~String();                    // destructor
    int length() const { return len; }
// overloaded operators
    String & operator=(const String & st); // Assignment operator
    String & operator=(const char * s);    // Assignment operator #2
    String & operator+(const String & st);
// friend functions
    friend bool operator>(const String &st1, const String &st2);
    friend bool operator<(const String &st, const String &st2);
    friend bool operator==(const String &st, const String &st2);
    friend String & operator+(char * st,  String &st2);
    friend ostream & operator<<(ostream & os, const String & st);
    friend istream & operator>>(istream & is, String & st);

    void stringlow();
    void stringup();
    int has(const char a);

Quote:};

#endif

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

// strng2.cpp -- String class methods
#include <cstring>
#include <cctype>
#include "strng3.h"

// class methods
String::String(const char * s)  // make String from C string
{
    len = strlen(s);
    str = new char[len + 1];    // allot storage
    strcpy(str, s);             // initialize pointer

Quote:}

String & String::operator+(const String & st)
{
   char * mString = str;
   str = new char[len + st.len + 1];
   delete [] str;
   strcpy(str, mString);
   strcat(str, st.str);
   return *this;
Quote:}

String::String()                // default constructor
{
    len = 0;
    str = new char[1];
    str[0] = '\0';              // default string
Quote:}

String::String(const String & st)// copy constructor
{
    len = st.len;
    str = new char[len + 1];
    strcpy(str, st.str);

Quote:}

String::~String()                 // destructor
{
    delete [] str;                // required
Quote:}

    // assign a String to a String
String & String::operator=(const String & st)
{
    if (this == &st)
        return *this;
    delete [] str;
    len = st.len;
    str = new char[len + 1];
    strcpy(str, st.str);
    return *this;
Quote:}

    // assign a C string to a String
String & String::operator=(const char * s)
{
    delete [] str;
    len = strlen(s);
    str = new char[len + 1];
    strcpy(str, s);
    return *this;
Quote:}

    // true if st1 follows st2 in collating sequence
bool operator>(const String &st1, const String &st2)
{
    if (strcmp(st1.str, st2.str) > 0)
        return true;
    else
        return false;

Quote:}

    // true if st1 precedes st2 in collating sequence
bool operator<(const String &st1, const String &st2)
{
    if (strcmp(st1.str, st2.str) < 0)
        return true;
    else
        return false;
Quote:}

String & operator+(char * st,  String &st2)
{
    char * mString = st2.str;
    String mike(st);
    st2.str = new char[mike.len + st2.len + 2];
    delete [] st2.str;
    strcpy(st2.str, mike.str);
    strcat(st2.str, mString);
    return st2;
Quote:}

// friends
    // true if st1 is the same as st2
bool operator==(const String &st1, const String &st2)
{
    if (strcmp(st1.str, st2.str) == 0)
        return true;
    else
        return false;
Quote:}

void String::stringup()
{
   char *line = str;
   while(*line)
   {
      if (islower(*line))
      *line = *line - ' ';
      line++;
   }
Quote:}

void String::stringlow()
{
   char *line = str;
   while(*line)
   {
      if (isupper(*line))
      *line = *line + ' ';
      line++;
   }
Quote:}

int String::has(const char  a)
{

   int count = 0;
   char *line = str;
   while(*line)
   {
      if(*line == a)
         count++;
      line++;

   }
   return count;

Quote:}

    // display string
ostream & operator<<(ostream & os, const String & st)
{
    os << st.str;
    return os;
Quote:}

    // quick and dirty String input
istream & operator>>(istream & is, String & st)
{
    char temp[80];
    is.get(temp, 80);
    if (is)
        st = temp;
    while (is && is.get() != '\n')
        continue;
    return is;

Quote:}

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

// main.cpp
#include <iostream>
using namespace std;

#include "strng3.h"

int main()
{

   String s1 = " Hello, I'm glad to see you";
   String s2 = " my friends";
   String s3 = "Hello" + s1;

   cout << s3;
   cout << endl;
   int z = 12;
   cout << z; // CRASH!!

   cout << endl;
   s3.stringup();
   cout << s3;
   s3.stringlow();
   cout << endl;
   cout << s3;
   z = s3.has('l');
   cout << endl;
   cout << z;

   return 0;

Quote:}

--
Michael Nigrin
 
 
 

ERROR: unmapped memory exception

Post by darkride.. » Sun, 31 Dec 1899 09:00:00




> Hi my program is crashing with the subject line error when using
> cout << int;.

> I believe it happens to be with either this code because when I remove
> reference to it in the main function, cout works with integers once
> again:

> String & operator+(char * st,  String &st2)
> {
>     char * mString = st2.str;
>     String mike(st);
>     st2.str = new char[mike.len + st2.len + 2];
>     delete [] st2.str;
>     strcpy(st2.str, mike.str);
>     strcat(st2.str, mString);
>     return st2;
> }

<snip>

Well...your assigning mString to st2.str...then later deleting st2.str.
 Therefor your use of mString(which points to memory already
deallocated) in the line strcat(st2.str, mString); is undefined(buggy)
behaivor.  You need to make a copy of the actual data that st points to,
not just the pointer.  Your use of the local object mike doesn't seem
necessary either(though its unrelated to the bug).  Hopefully that
clears up the problem.
Micah

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

ERROR: unmapped memory exception

Post by Michael N » Sun, 31 Dec 1899 09:00:00




Well I changed the function to read:

String & operator+(char * st,  String &st2)
{
    char * mString = st2.str;
    char mike[20];
    strcpy(mike, st);
    st2.str = new char[strlen(mike) + st2.len + 2];
    strcpy(st2.str, mike);
    strcat(st2.str, mString);
    return st2;

Quote:}

and am still getting the error, any other ideas?

Mike



> > Hi my program is crashing with the subject line error when using
> > cout << int;.

> > I believe it happens to be with either this code because when I remove
> > reference to it in the main function, cout works with integers once
> > again:

> > String & operator+(char * st,  String &st2)
> > {
> >     char * mString = st2.str;
> >     String mike(st);
> >     st2.str = new char[mike.len + st2.len + 2];
> >     delete [] st2.str;
> >     strcpy(st2.str, mike.str);
> >     strcat(st2.str, mString);
> >     return st2;
> > }
> <snip>

> Well...your assigning mString to st2.str...then later deleting st2.str.
>  Therefor your use of mString(which points to memory already
> deallocated) in the line strcat(st2.str, mString); is undefined(buggy)
> behaivor.  You need to make a copy of the actual data that st points to,
> not just the pointer.  Your use of the local object mike doesn't seem
> necessary either(though its unrelated to the bug).  Hopefully that
> clears up the problem.
> Micah

> Sent via Deja.com http://www.deja.com/
> Before you buy.

 
 
 

ERROR: unmapped memory exception

Post by darkride.. » Sun, 31 Dec 1899 09:00:00


Well, one thing about operator+(at least as its usually used) is you
don't want to modify its arguments.  I think I would implement it as
follows(untested code, I don't gaurentee anything):

String & operator+(char* st, const String &st2)
{
   String temp;
   // We need room for both chars + 1 NULL terminator
   temp.str = new char[strlen(st) + strlen(st2) + 1];
   strcpy(temp.str,st);
   strcat(temp.str,st2.str);
   return temp;

Quote:}

I'm not exactly sure what's causing the crash...one problem is you have
a memory leak...your allocating memory for st2.str to point to, but you
don't delete the old allocated memory.  Also you maybe overruning the
bounds of mike.  Hope this helps,
Micah

Quote:> Well I changed the function to read:

> String & operator+(char * st,  String &st2)
> {
>     char * mString = st2.str;
>     char mike[20];
>     strcpy(mike, st);
>     st2.str = new char[strlen(mike) + st2.len + 2];
>     strcpy(st2.str, mike);
>     strcat(st2.str, mString);
>     return st2;
> }

> and am still getting the error, any other ideas?

> Mike

Sent via Deja.com http://www.deja.com/
Before you buy.
 
 
 

ERROR: unmapped memory exception

Post by darkride.. » Sun, 31 Dec 1899 09:00:00


I screwed up here.  Returning by reference is a bad idea.  The scope of
temp is only within the function, returning by reference will definately
cause problems.  Should be

String operator+(char* st, const String& st2);

I would even suggest you should make it(just to make sure you aren't
modifying the contents of either of the parameters):

String operator+(const char* st, const String& st2);

Sorry about that, should have been paying more attention.
Micah

Quote:> Well, one thing about operator+(at least as its usually used) is you
> don't want to modify its arguments.  I think I would implement it as
> follows(untested code, I don't gaurentee anything):

> String & operator+(char* st, const String &st2)
> {
>    String temp;
>    // We need room for both chars + 1 NULL terminator
>    temp.str = new char[strlen(st) + strlen(st2) + 1];
>    strcpy(temp.str,st);
>    strcat(temp.str,st2.str);
>    return temp;
> }

> I'm not exactly sure what's causing the crash...one problem is you
have
> a memory leak...your allocating memory for st2.str to point to, but
you
> don't delete the old allocated memory.  Also you maybe overruning the
> bounds of mike.  Hope this helps,
> Micah

> > Well I changed the function to read:

> > String & operator+(char * st,  String &st2)
> > {
> >     char * mString = st2.str;
> >     char mike[20];
> >     strcpy(mike, st);
> >     st2.str = new char[strlen(mike) + st2.len + 2];
> >     strcpy(st2.str, mike);
> >     strcat(st2.str, mString);
> >     return st2;
> > }

> > and am still getting the error, any other ideas?

> > Mike

> Sent via Deja.com http://www.deja.com/
> Before you buy.

Sent via Deja.com http://www.deja.com/
Before you buy.