linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Memory bug somewhere
@ 2003-05-24 17:48 David Eduardo Gomez Noguera
  2003-05-24 18:08 ` Glynn Clements
  0 siblings, 1 reply; 4+ messages in thread
From: David Eduardo Gomez Noguera @ 2003-05-24 17:48 UTC (permalink / raw)
  To: linux-c-programming

Hello.

I have been having a program crash in this function.
It basically recives a pointer to a structure 

typedef struct hufftree {
  HuffCode code;
  struct hufftree *l;
  struct hufftree *r;
}hufftree;

a code, a size (of the code) and a data asigned to that huffman code.

This function inserts the data at the end of the path marked by the code
and the size of the code.

It segfaults after some asignments (some calls to ins_code), always at
the same point.

Can anybody here see what is wrong with this?

compilation like is just 

gcc -g -lm file.c -o file

and its glibc-2.3.2-4.80 (know if there is any issue with malloc there?)

-------------------------CUT HERE-----------------------------

int
ins_code (hufftree * tree, unsigned short code, unsigned char size,
	  unsigned char dato)
{
	hufftree *tree2;
	unsigned short i, k;

	tree2 = tree;

	for (i = size; i > 0; i--)
	{

		if (code & ((unsigned short) 1 << (i - 1)))
		{
			if (tree2->l == NULL)
			{
				printf ("before\n");
				tree2->l = malloc (sizeof (hufftree));
				printf ("after\n");
				tree2->l->l = NULL;
				tree2->l->r = NULL;
			}
			tree2 = tree2->l;
		}
		else
		{
			if (tree2->r == NULL)
			{
				printf ("before\n");
				tree2->r = malloc (sizeof (hufftree));
				printf ("after\n");
				tree2->r->l = NULL;
				tree2->r->r = NULL;
			}
			tree2 = tree2->r;
		}
	}
	tree2->code.used = dato;
	tree2->code.code = code;
	return 0;
}
----------------------CUT HERE------------------------------


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-05-24 23:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-05-24 17:48 Memory bug somewhere David Eduardo Gomez Noguera
2003-05-24 18:08 ` Glynn Clements
2003-05-24 19:47   ` David Eduardo Gomez Noguera
2003-05-24 23:52     ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).