From: sls@aero.org (Sam Shen)
Subject: problems with delete in GCC 2.2.2
Organization: GNUs Not Usenet
Distribution: gnu
Date: Mon, 12 Oct 1992 23:38:52 GMT

I'm running on a sparcstation 2, SunOS 4.1.1.  The following code
doesn't have any problem running when compiled with g++ 1.40.3, or
CenterLine ObjectCenter.  But with GCC 2.2.2 it eventually
causes seg faults in free.  If I link in Sun's /usr/lib/debug/malloc.o
I get the following message:

free: blocks 0xdfc0, 0x0 overlap

IOT trap

	-Sam Shen (sls@aero.org)

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  Makefile TableCell.cc TableWidget.cc main.cc TableCell.h
#   TableWidget.h
# Wrapped by sls@batcomputer on Mon Oct 12 16:36:56 1992
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'Makefile' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'Makefile'\"
else
echo shar: Extracting \"'Makefile'\" \(437 characters\)
sed "s/^X//" >'Makefile' <<'END_OF_FILE'
X#
X# Makefile
X#
X
XLIBS = -lm
XINCLUDES = 
XCFLAGS = -g
XCCX = gcc #/usr/aero/bin/g++
X.SUFFIXES: .cc .o
X
X.cc.o:
X	$(CCX) -c $(CFLAGS) $(INCLUDES) $<
X
XSRCS	= TableCell.cc TableWidget.cc main.cc
XOBJS	= TableCell.o TableWidget.o main.o
XMALLOC	= /usr/lib/debug/malloc.o
X
Xtable: $(OBJS)
X	$(CCX) -o table $(OBJS) $(MALLOC) $(CFLAGS) $(LIBS)
X
XTableCell.o: TableCell.cc TableCell.h TableWidget.h
XTableWidget.o: TableWidget.cc TableWidget.h TableCell.h
END_OF_FILE
if test 437 -ne `wc -c <'Makefile'`; then
    echo shar: \"'Makefile'\" unpacked with wrong size!
fi
# end of 'Makefile'
fi
if test -f 'TableCell.cc' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'TableCell.cc'\"
else
echo shar: Extracting \"'TableCell.cc'\" \(2084 characters\)
sed "s/^X//" >'TableCell.cc' <<'END_OF_FILE'
Xstatic char rcsid[] = "$Id: TableCell.cc,v 1.5 1992/10/06 22:46:01 sls Exp $";
X//
X// $Log: TableCell.cc,v $
X// Revision 1.5  1992/10/06  22:46:01  sls
X// Bug fix in handling of bindings.
X//
X// Revision 1.4  1992/10/02  23:28:12  sls
X// Get rid of TableAction stuff.  Add TableBindings class.
X//
X// Revision 1.3  1992/09/30  02:56:35  sls
X// Fix bug in freeing of old string_rep.
X//
X// Revision 1.2  1992/09/30  02:44:19  sls
X// Have TableCell's draw and compute their own widths.  Line up
X// number columns with the decimal point.
X//
X// Revision 1.1  1992/09/29  22:33:11  sls
X// Initial revision
X//
X#include <stdio.h>
X#include <string.h>
X#include "TableWidget.h"
X#include "TableCell.h"
X
Xstatic char* empty_string = "";
Xstatic char* default_format = "%g";
X
XTableCell::TableCell()
X{
X    printf("TableCell 0x%x\n", this);
X    fflush(stdout);
X    type = CELL_STRING;
X    string = nil;
X    string_rep = nil;
X    number_format = default_format;
X    format = CELLF_NONE;
X}
X
XTableCell::~TableCell()
X{
X    printf("~TableCell 0x%x\n", this);
X    fflush(stdout);
X    delete string_rep;
X    delete string;
X}
X
Xvoid TableCell::Set(const char* new_string)
X{
X    delete string;
X    string = new char[strlen(new_string)+10];
X    strcpy(string, new_string);
X    ComputeStringRep(CELL_STRING);
X}
X
Xvoid TableCell::Set(float new_number)
X{
X    number = new_number;
X    ComputeStringRep(CELL_NUMBER);
X}
X
Xvoid TableCell::ComputeStringRep(CellType new_type)
X{
X    delete string_rep;
X    if (new_type == CELL_NUMBER) {
X	char buf[256];
X	sprintf(buf, number_format, number);
X	string_rep = new char[strlen(buf)+10];
X	strcpy(string_rep, buf);
X    } else if (string == nil) {
X	string_rep = nil;
X    } else {
X	string_rep = new char[strlen(string)+10];
X	strcpy(string_rep, string);
X    }
X    type = new_type;
X}
X
XTableCell& TableCell::operator=(TableCell& cell)
X{
X    type = cell.type;
X    number = cell.number;
X    delete string;
X    if (cell.string != nil) {
X	string = new char[strlen(cell.string)+10];
X	strcpy(string, cell.string);
X    } else {
X	string = nil;
X    }
X    ComputeStringRep(type);
X    return *this;
X}
END_OF_FILE
if test 2084 -ne `wc -c <'TableCell.cc'`; then
    echo shar: \"'TableCell.cc'\" unpacked with wrong size!
fi
# end of 'TableCell.cc'
fi
if test -f 'TableWidget.cc' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'TableWidget.cc'\"
else
echo shar: Extracting \"'TableWidget.cc'\" \(4240 characters\)
sed "s/^X//" >'TableWidget.cc' <<'END_OF_FILE'
Xstatic char rcsid[] = "$Id: TableWidget.cc,v 1.5 1992/10/06 22:46:49 sls Exp $";
X//
X// $Log: TableWidget.cc,v $
X// Revision 1.5  1992/10/06  22:46:49  sls
X// Bug fix in HighlightCell.  Add GetCellGeometry.
X//
X// Revision 1.4  1992/10/02  23:28:12  sls
X// Get rid of TableAction stuff.  Add TableBindings class.
X//
X// Revision 1.3  1992/09/30  02:55:42  sls
X// Delete column_widths and dp_offsets in destructor.
X//
X// Revision 1.2  1992/09/30  02:44:19  sls
X// Have TableCell's draw and compute their own widths.  Line up
X// number columns with the decimal point.
X//
X// Revision 1.1  1992/09/29  22:33:11  sls
X// Initial revision
X//
X
X#include "TableWidget.h"
X#include "TableCell.h"
X#include <stdio.h>
X#include <string.h>
X
X#define MULTIPLIER_ROWS 2
X#define MULTIPLIER_COLS 3/2
X
X#define cell_padx 3
X#define cell_pady 3
X#define header_pady 4
X#define header_padx 3
X
XTableWidget::TableWidget()
X{
X    size_cols = 0;
X    size_rows = 0;
X    cells = nil;
X    column_formats = nil;
X    column_widths = nil;
X    dp_offsets = nil;
X    column_headers = nil;
X    n_rows = 0;
X    n_cols = 0;
X    ExpandCells(16, 4);
X}
X
XTableWidget::~TableWidget()
X{
X    int i;
X    for (i = 0; i < size_rows; i++)
X	delete [size_cols] cells[i];
X    delete [size_cols] cells;
X    delete [size_cols] column_formats;
X    delete [size_cols] column_headers;
X    delete [size_cols] column_widths;
X    delete [size_cols] dp_offsets;
X}
X
Xtypedef unsigned int boolean;
X#define true 1
X#define false 0
X
Xvoid TableWidget::CheckReference(int row, int col)
X{
X    int new_rows = size_rows, new_cols = size_cols;
X    boolean expand = false;
X    if (row >= n_rows)
X	n_rows = row + 1;
X    if (col >= n_cols)
X	n_cols = col + 1;    
X    if (n_rows >= size_rows) {
X	new_rows = size_rows * MULTIPLIER_ROWS;
X	expand = true;
X    }
X    if (n_cols >= size_cols) {
X	new_cols = size_cols * MULTIPLIER_COLS;
X	expand = true;
X    }
X    if (expand)
X	ExpandCells(new_rows, new_cols);
X}
X
Xvoid TableWidget::ExpandCells(int new_rows, int new_cols)
X{
X    printf("ExpandCells, new_rows=%d, new_cols=%d\n", new_rows, new_cols);
X    fflush(stdout);
X    TableCell** new_cells;
X    CellFormatType* new_column_formats;
X    int* new_column_widths;
X    int* new_dp_offsets;
X    char** new_column_headers;
X    new_cells = new TableCell*[new_rows];
X    new_column_formats = new CellFormatType[new_cols];
X    new_column_headers = new char*[new_cols];
X    new_column_widths = new int[new_cols];
X    new_dp_offsets = new int[new_cols];
X    int i,j;
X    for (j = 0; j < new_rows; j++) {
X	new_cells[j] = new TableCell[new_cols];
X    }
X    for (i = 0; i < new_cols; i++) {
X	new_column_formats[i] = CELLF_STRING;
X	new_column_headers[i] = nil;
X	new_column_widths[i] = 0;
X	new_dp_offsets[i] = -1;
X    }
X    for (j = 0; j < size_rows; j++) {
X	for (i = 0; i < size_cols; i++) {
X	    new_cells[j][i] = cells[j][i];
X	}
X    }
X    for (i = 0; i < size_cols; i++) {
X	new_column_formats[i] = column_formats[i];
X	new_column_headers[i] = column_headers[i];
X	new_column_widths[i] = column_widths[i];
X	new_dp_offsets[i] = dp_offsets[i];
X    }
X    for (j = 0; j < size_rows; j++)
X	delete [size_cols] cells[j];
X    delete [size_cols] cells;
X    delete [size_cols] column_formats;
X    delete [size_cols] column_widths;
X    delete [size_cols] dp_offsets;
X    delete [size_cols] column_headers;
X    cells = new_cells;
X    column_formats = new_column_formats;
X    column_headers = new_column_headers;
X    column_widths = new_column_widths;
X    dp_offsets = new_dp_offsets;
X    size_cols = new_cols;
X    size_rows = new_rows;
X}
X
Xvoid TableWidget::SetColumnFormat(int col, CellFormatType type)
X{
X    CheckReference(0, col);
X    column_formats[col] = type;
X}
X
Xvoid TableWidget::SetColumnHeader(int col, const char* header)
X{
X    CheckReference(0, col);
X    delete column_headers[col];
X    column_headers[col] = new char[strlen(header)+1];
X    strcpy(column_headers[col], header);
X}
X
Xvoid TableWidget::SetCell(int row, int col, const char* string)
X{
X    CheckReference(row, col);
X    cells[row][col].Set(string);
X}
X
Xvoid TableWidget::SetCell(int row, int col, float number)
X{
X    CheckReference(row, col);
X    cells[row][col].Set(number);
X}
X
XTableCell& TableWidget::GetCell(int row, int col)
X{
X    CheckReference(row, col);
X    return cells[row][col];
X}
END_OF_FILE
if test 4240 -ne `wc -c <'TableWidget.cc'`; then
    echo shar: \"'TableWidget.cc'\" unpacked with wrong size!
fi
# end of 'TableWidget.cc'
fi
if test -f 'main.cc' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'main.cc'\"
else
echo shar: Extracting \"'main.cc'\" \(1963 characters\)
sed "s/^X//" >'main.cc' <<'END_OF_FILE'
X#include <stdio.h>
X#include <math.h>
X#include <string.h>
X#include "TableWidget.h"
X#include "TableCell.h"
X
Xextern "C" void exit(int);
X
Xextern "C" void malloc_debug(int);
X
Xint main(int argc, char * argv[])
X{
X    int row, col;
X    malloc_debug(2);
X    
X    TableWidget* table = new TableWidget();
X    FILE* fp = fopen("/usr/pub/ascii", "r");
X    char buf[128];
X    row = 0;
X    table->SetColumnHeader(0, "Table of Ascii Values");
X    while (fgets(buf, 128, fp)) {
X	int i, start = 1;
X	col = 0;
X	for (i = 1; buf[i]; i++) {
X	    if (buf[i] == '|' && buf[i+2] != '|') {
X		buf[i] = '\0';
X		table->SetCell(row, col, buf + start);
X		start = i + 1;
X		col++;
X	    }
X	}
X	row++;
X    }
X    fclose(fp);
X
X    TableWidget* etable = new TableWidget();
X    etable->SetColumnHeader(0, "n");
X    etable->SetColumnHeader(1, "exp(n)");
X    etable->SetColumnFormat(0, CELLF_NUMBER);
X    etable->SetColumnFormat(1, CELLF_NUMBER);
X    double x;
X    row = 0;
X    for (x = -10.0; x < 11.0; x++) {
X        etable->SetCell(row, 0, x);
X        etable->SetCell(row, 1, exp(x));
X        row++;
X    }
X    etable->SetCell(row, 0, "Hello");
X    etable->SetCell(row, 1, "World!");
X    
X    TableWidget* mixed_table = new TableWidget();
X    mixed_table->SetColumnHeader(0, "Number");
X    mixed_table->SetColumnFormat(0, CELLF_NUMBER);    
X    mixed_table->SetColumnHeader(1, "String");
X    mixed_table->SetColumnFormat(1, CELLF_STRING);
X    mixed_table->SetColumnHeader(2, "Center");
X    mixed_table->SetColumnFormat(2, CELLF_CSTRING);
X    mixed_table->SetColumnHeader(3, "Right");
X    mixed_table->SetColumnFormat(3, CELLF_RSTRING);
X    fp = fopen("/usr/dict/words", "r");
X    for (row = 0; row < 30; row += 2) {
X        char buf[100];
X        int i;
X        for (i = 0; i < 4; i++) {
X            fgets(buf, 100, fp);
X            buf[strlen(buf)-1] = '\0';
X            mixed_table->SetCell(row, i, buf);
X            mixed_table->SetCell(row+1, i, exp((float) row));
X        }
X    }
X    fclose(fp);
X
X}
END_OF_FILE
if test 1963 -ne `wc -c <'main.cc'`; then
    echo shar: \"'main.cc'\" unpacked with wrong size!
fi
# end of 'main.cc'
fi
if test -f 'TableCell.h' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'TableCell.h'\"
else
echo shar: Extracting \"'TableCell.h'\" \(1368 characters\)
sed "s/^X//" >'TableCell.h' <<'END_OF_FILE'
X#ifndef TableCell_h
X#define TableCell_h
X/* $Id: TableCell.h,v 1.4 1992/10/02 23:28:12 sls Exp $ */
X/*
X * $Log: TableCell.h,v $
X * Revision 1.4  1992/10/02  23:28:12  sls
X * Get rid of TableAction stuff.  Add TableBindings class.
X *
X * Revision 1.3  1992/09/30  02:56:35  sls
X * Fix bug in freeing of old string_rep.
X *
X * Revision 1.2  1992/09/30  02:44:19  sls
X * Have TableCell's draw and compute their own widths.  Line up
X * number columns with the decimal point.
X *
X * Revision 1.1  1992/09/29  22:33:11  sls
X * Initial revision
X *
X */
X
X#ifndef nil
X#define nil 0
X#endif
X
X
Xclass TableCell {
Xpublic:
X    TableCell();
X    virtual ~TableCell();
X    inline CellType GetType() { return type; };
X    inline char* GetString() { return string; };
X    inline float GetNumber() { return number; };
X    inline const char* GetStringRep()
X	{
X	    if (string_rep == nil)
X		return "";
X	    else
X		return string_rep;
X	};
X    inline CellFormatType GetFormat() { return format; };
X    inline void SetFormat(CellFormatType t) { format = t; };
X    void Set(const char* string);
X    void Set(float number);
X    TableCell& operator=(TableCell& cell);
X    
Xprivate:
X    void ComputeStringRep(CellType new_type);
X    CellType type;
X    CellFormatType format;
X    char* string_rep;
X    char* number_format;
X    int width;
X    int left_width;
X    char* string;
X    float number;
X};
X
X#endif
END_OF_FILE
if test 1368 -ne `wc -c <'TableCell.h'`; then
    echo shar: \"'TableCell.h'\" unpacked with wrong size!
fi
# end of 'TableCell.h'
fi
if test -f 'TableWidget.h' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'TableWidget.h'\"
else
echo shar: Extracting \"'TableWidget.h'\" \(1621 characters\)
sed "s/^X//" >'TableWidget.h' <<'END_OF_FILE'
X#ifndef Table_h
X#define Table_h
X/* $Id: TableWidget.h,v 1.4 1992/10/06 22:46:49 sls Exp $ */
X/*
X * $Log: TableWidget.h,v $
X * Revision 1.4  1992/10/06  22:46:49  sls
X * Bug fix in HighlightCell.  Add GetCellGeometry.
X *
X * Revision 1.3  1992/10/02  23:28:12  sls
X * Get rid of TableAction stuff.  Add TableBindings class.
X *
X * Revision 1.2  1992/09/30  02:44:19  sls
X * Have TableCell's draw and compute their own widths.  Line up
X * number columns with the decimal point.
X *
X * Revision 1.1  1992/09/29  22:33:34  sls
X * Initial revision
X *
X */
X
Xclass TableCell;
X
X#ifndef nil
X#define nil 0
X#endif
X
Xenum CellType { CELL_STRING, CELL_NUMBER };
Xenum CellFormatType {
X    CELLF_STRING, CELLF_NUMBER, CELLF_RSTRING, CELLF_CSTRING, CELLF_NONE
X};
X
Xclass TableWidget {
Xpublic:
X    TableWidget();
X    virtual ~TableWidget();
X
X    // interface functions
X    void SetColumnFormat(int col, CellFormatType type);
X    void SetNumberColumnFormat(int col, const char* format);
X    void SetColumnHeader(int col, const char* header);
X    void SetCell(int row, int column, const char* string);
X    void SetCell(int row, int column, float number);
X    TableCell& GetCell(int row, int column);
X    
Xprotected:
X    void CheckReference(int row, int col);
X    void ExpandCells(int new_rows, int new_cols);
X    
X    TableCell** cells;		    /* array of cells */
X    CellFormatType* column_formats; /* type of columns */
X    char** column_headers;	    /* headers of columns */
X    int* column_widths;		    /* widths */
X    int* dp_offsets;		    /* offset of decimal point */
X    
X    int n_cols, n_rows;
X    int size_cols, size_rows;
X
X};
X
X#endif
END_OF_FILE
if test 1621 -ne `wc -c <'TableWidget.h'`; then
    echo shar: \"'TableWidget.h'\" unpacked with wrong size!
fi
# end of 'TableWidget.h'
fi
echo shar: End of shell archive.
exit 0


Newsgroups: gnu.g++.bug