[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
malloc: top chunk is corrupt
From: |
iq-0 |
Subject: |
malloc: top chunk is corrupt |
Date: |
Tue, 06 Feb 2001 19:53:00 +0100 |
User-agent: |
Mozilla/5.0 (X11; U; Linux 2.2.16-22 i686; en-US; 0.7) Gecko/20010105 |
hello,
I get this error when I compile my program with C++ and setting
MALLOC_CHECK_=1. The program makes extensive use of realloc. (I've
ripped the attachements out of my own program, so in some cases you
would normally do a malloc).
I've compiled them with
g++ -g -Wall
Note:
Some parts are bit dirty that's because of previous hacks to locate
problem. (e.g. there was a calloc)
What it does:
Returns a unique index for name (char*), it uses hashtables (simple
hashing (add all chars and then modulo(%) #of buckets)). First it does
this on strlength, then on hash, and inside hashbucket sorted. When
encountering a double name it returns that index.
System information:
g++ -v: gcc version 2.96 20000731 (Red Hat Linux 7.0)
ld: GNU ld version 2.10.90 (with BFD 2.10.0.18)
libc: 2.2 (redhat 2.2.0-12 i686)
P.s.:
I know there are probably more methods to find double names and give
unique indexes, but this is a quick hack (other nice sollution took too
long to add all 3 million names).
regards,
justin....
#include <iostream>
#include <string.h>
#include "NameRegistry.h"
int main ()
{
NameRegistry names;
char buffer[4096];
while (cin >> buffer)
{
names.registerName(buffer, strlen(buffer));
}
}
#include "NameRegistry.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int NameRegistry::registerName (char * _name, int _length, strlist_node *_node)
{
register unsigned int i;
register int j;
unsigned int retval;
unsigned int nodelen;
char *current, *offset;
nodelen = _length + 4;
i = 0;
j = -1;
current = _node->strings;
while (i < _node->count && j < 0 )
{
j = strncmp(current, _name, _length );
if (j == 0)
{
memcpy ( &retval, (current + _length + 1) , 4);
return retval;
}
i++;
current += nodelen;
}
_node->count++;
_node->strings = (char *) realloc (_node->strings, _node->count *
nodelen );
perror("log:");
offset = _node->strings + (i * nodelen);
if ( i < _node->count - 1 )
{
memmove ( offset + nodelen, offset, (_node->count - i) *
nodelen );
}
memcpy ( offset , _name , _length );
memcpy ( offset + _length , &indexCounter , sizeof(unsigned int) );
retval = indexCounter;
indexCounter++;
return retval;
}
void NameRegistry::reallocNames (int _stepping)
{
register int i;
register int j;
register strlist_node **current;
names = (strlist_node **) realloc ( names, sizeof(strlist_node*) * (
maxLength + _stepping ) );
current = names + maxLength;
for ( i = 0 ; i < _stepping ; i++ )
{
(*current) = (strlist_node *) realloc ( 0, HASHBLOCKS *
sizeof(strlist_node) );
for (j = 0; j < HASHBLOCKS; j++)
{
(*current)[j].count = 0;
(*current)[j].strings = 0;
}
current++;
}
maxLength += _stepping;
}
NameRegistry::NameRegistry () : names(0), maxLength(0), indexCounter(0)
{
reallocNames (1);
}
NameRegistry::~NameRegistry ()
{
register int i, j;
register strlist_node **current;
current = names;
for ( i = 0 ; i < maxLength ; i++)
{
for (j = 0 ; j < HASHBLOCKS ; j++)
{
if ((*current)[j].count != 0)
{
realloc ( (*current)[j].strings , 0 );
}
}
realloc (current, 0);
current ++;
}
realloc (names, 0);
}
int NameRegistry::registerName (char * _name, int _length)
{
strlist_node *hashlist;
register int i;
register char * cp;
unsigned int hashvalue;
if (_length > maxLength)
{
reallocNames ( _length - maxLength );
}
cp = _name;
hashvalue = 0;
for ( i = 0 ; i < _length ; i++ )
{
hashvalue += (*cp);
cp++;
}
hashvalue %= HASHBLOCKS;
hashlist = names [_length - 1];
hashlist += hashvalue;
return registerName (_name, _length, hashlist);
}
#ifndef NAME_REGISTRY_H
#define NAME_REGISTRY_H
#define HASHBLOCKS 16
typedef struct
{
unsigned int count;
char * strings;
} strlist_node;
class NameRegistry
{
private:
void reallocNames (int stepping);
int registerName (char *name, int length, strlist_node *node);
protected:
strlist_node **names;
unsigned short maxLength;
unsigned int indexCounter;
public:
NameRegistry ();
~NameRegistry ();
int registerName (char *name, int length);
};
#else
class NameRegistry;
#endif
- malloc: top chunk is corrupt,
iq-0 <=