delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/01/27/04:34:35

Message-ID: <3A72A43B.1217@earthlink.net>
Date: Sat, 27 Jan 2001 02:34:35 -0800
From: Shelby Worley <sworley AT earthlink DOT net>
X-Mailer: Mozilla 3.0C-NSCP (Win16; U)
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: new gnu 'new'' bug ?
Reply-To: djgpp AT delorie DOT com

Sirs,
     Sorry for the 'subject' line - couldn't resist.  I  searched your
bug archive and could not find this one.

     Program created by gxx compiler with C++ code.

     Problem producing code below;

----------------------------------------------------------------
     // String::   a simple string class consisting of a (1)pointer
     //            to a dynamically allocated 0-terminated string,
     //            the (2) string length, and (3) the allocated total
     //            memory.

     String a, b, c, d;
     //  code giving user selected codes to b,c,d
     a = b + c + d;      // Program issues a SIGSEGV exception here
                         // under certain conditions.
                         //

     // remainder of program
--------------------------------------------------------------------


     "BUG" found by use of GDB

    The problem occurred by the action of the copy constructor for
string.
Since 'a' was initially an empty String, new memory had to be allocated
to hold the string "portion" of 'a'.

          The sum (concatenation) of strings b,c,and d were held in 14
bits
of memory allocated by the 'new' operator.

    When the copy constructor issued "(pointer variable) = new
char[14};",
the range chosen was only 16 bits higher than memory holding the sum.
The 'new' operator modified 4 bits before the new memory, overwriting
the last character with 0x11.  Then it tried to copy the strings,
generating the exception.

    Since allocations fell on 16 byte intervals, if the sum string was
longer, the string would not be overwritten.

    Work-around.  Be sure that there are at least 4 more character
                  allocated than needed.


    Thank you very much for this good C++ compiler so that I can learn
C++ programming in a unix like DOS environment.  There may be some
compiler switches that address the problem above, but I am only just now
becoming comfortable with "info"

        -   Shelby Worley

        - sworley AT earthlink DOT net

- Raw text -


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