www.delorie.com/djgpp/bugs/show.cgi   search  
Bug 000317

When Created: 03/05/2000 04:00:45
Against DJGPP version: 2.03
By whom: none@sorry.com
Abstract: Char-Array - Pointer Problem
Depending on the directory, the program is run in, different additional chars are displayed.


//------------------------------------------------------------------------------



// 1). Compiled with DJGPP, the program doesn't work correctly.


#include <iostream>

char a='a';                          // "a" is the char to be saved in the char-                                     // array.

unsigned long x=1;                   // "x" needs to be "1"; so when "x"'s in-
                                     // cremented, the char-array provides
                                     // space for at least one char and the
                                     // LastIndexSign.

const short y=0;                     // set the number of extra chars to be
                                     // saved in the char-array.

void test(char *b)                   // "b" is the char-array,
{
 for(unsigned long i=0;i<x-1+y;i++)  // saving "a" for "x"-1 time; the "x"th
                                     // time is the LastIndexSign.
                                     // You can however save it more often,
                                     // even if the char-array is supposed to be
                                     // not big enough.
 {

  /*           // making the last char in the char-array a space; y needs to be
  if(i=x-2+y)  // init. with 2, so that the "right" number of "a"s is saved
  {            // right from the beginning.
   b[i]=' ';   // This structure makes the prgram not work correctly when com-
   break;      // piled with DJGPP!
  }
  */


  b[i]=a;
 }
}

int main()
{
 while((int)a!=48)                    // "0" ends program.
 {
  for(unsigned int i=0;i<23;i++)
  {
   x++;

   char *c=new char[x];               // declaring the char-array with the size 
                                      // of "x".

   test(c);


   cout << x-1+y;                     // if "x"'s init with "2", write "x-2+y"
                                      // to show the right number of chars saved
                                      // in the char-array.

   if(x<11-y)                         // That's just an layout feature.
   {
    cout << " : ";
   }
   if(x>10-y)
   {
    cout << ": ";
   }

   cout << c << endl;

   c=0;                               // 'delete c' creates a "glitched" program
                                      // even when compiled whith Borland.
  }

  cin >> a;
 }
}


//------------------------------------------------------------------------------



// 2). However the program should work like here.


#include <iostream>

int main()                // "enabling" the c-comments shows that the "function"
                          // works theoretically;
                          // with these comments left in, the program works cor-
                          // rect to a certain degree. But then there's the pro-
                          // blem with ANSI C++ not allowing declaring a char-
                          // array with a non-constant type (at least my Borland
                          // compiler says this; DJGPP compiles without war-
                          // nings). *1
{
 char a='a';
 /* const */  unsigned long x=1;  // set to somewhat above 5, when "enabling"
                                  // outcommented sourcecode.

 while((int)a!=48)
 {
  for(unsigned int i=0;i<23;i++)
  {
   /* // */  x++;

   char c[x];

   for(unsigned long i=0;i<x-1;i++)
   {
    c[i]=a;
   }

   cout << x-1 << ": " << c << endl;
  }

  cin >> a;
 }
}


/*
*1  Because of this, I started to use the 'new' operator, as then Borland
didn't want to have a non-constant type. Then I discovered that at certain
array-lengths, additional chars are displayed. So I "wrote" the "Char-Pointer
Problem"-Program.
When I monitored the function, which should copy the appropriate chars,
and 'cout'ed them, I saw that the correct chars were copied. But the char-array
seems to contain additional chars when 'cout'ed. That was how this lot of
writing all started.
Where's the error?
*/


//------------------------------------------------------------------------------



Compiling the   Char-Pointer Problem  -  Source Code   using my Borland
Compiler produces a correctly running program.

So the problem seems to be with DJGPP - by the way, can you compile without the
DPMI-code?
Does DJGPP actually do any optimizations when compiling without switches?

Note added: 03/06/2000 16:00:02
By whom: broeker@physik.rwth-aachen.de
The bug is in the example source. Although it's talking about the
need to store a 'LastIndexSign' (i.e. the terminating NUL character
needed for a char * or char [] to be a string), it does not
ever actually write one into the storage gotten from 'new'.

That's why the program prints whatever random garbage happens to
be found beyond the end of the char *c.

Add a line 

	b[x-1+y]='\0';

directly before the closing brace of function 'test', and the error
will be gone.

The difference in behaviour between DJGPP and Borland C++ is probably
caused by Borland zeroing out the new[]ed array. That's inefficient,
so DJGPP doesn't do that.

Closed on 03/06/2000 16:00:34: Non-Bug: user program error.
By whom: broeker@physik.rwth-aachen.de



  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2010   by DJ Delorie     Updated Jul 2010