linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Eduardo Gomez Noguera <davidgn@servidor.unam.mx>
To: linux-c-programming@vger.kernel.org
Subject: Memory bug somewhere
Date: 24 May 2003 12:48:58 -0500	[thread overview]
Message-ID: <1053798538.1248.24.camel@localhost.localdomain> (raw)

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------------------------------


             reply	other threads:[~2003-05-24 17:48 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-24 17:48 David Eduardo Gomez Noguera [this message]
2003-05-24 18:08 ` Memory bug somewhere Glynn Clements
2003-05-24 19:47   ` David Eduardo Gomez Noguera
2003-05-24 23:52     ` Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1053798538.1248.24.camel@localhost.localdomain \
    --to=davidgn@servidor.unam.mx \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).