Re: Allocating memory for struct - when?

From: Old Wolf (oldwolf_at_inspire.net.nz)
Date: 05/03/04


Date: 2 May 2004 22:55:21 -0700

fix <fix@here.com> wrote:
> I am writing a program using some structs, it is not running and I
> believe it is because there's some memory leak - the debugger tells me
> that the code causes the problem is in the malloc function.

> typedef struct {
> void *obj;
> char key[];
> } HashTableEntry;

BTW, you can't have arrays of these.

> What I do to initialize the object is:
>
> HashTableEntry *hte = malloc(sizeof(HashTableEntry));
> // "key" is a defined char array
> strcpy(hte->key, key);
> hte->obj = NULL;

What you should be doing is:
  HashTableEntry *hte = malloc(strlen(key) + 1 + sizeof *hte);
  strcpy(hte->key, key);
  hte->obj = NULL;

Note that sizeof(*hte) is the same as sizeof(void*), the member "key"
does not have a size that the compiler knows about. You won't be
able to access the size of the entry later on in your code.
Because of this, you can't safely put a new value into the key (unless
it has the same length). So I would consider making key "const char"
instead of "char" (and casting it in the call to strcpy).

> And one more thing is, I can't reverse the order of the two members

Obviously not, how would the compiler know how much memory to
leave before the start of "obj" ?

If this is all confusing to you, you might want to consider
the other poster's suggestion of having "char *key;" and allocating
key with malloc too; although it seems to me that this is a perfect
example of a situation that flexible array members were designed for.



Relevant Pages

  • Re: Need help with large file reading and writing.
    ... Memory leak: I did use some allocatable matrice and forget to deallocate them, but I deallocate them or just declare them as static after you mentioned. ... the compiler problem about the limitation of buffer memory. ... My first guess would be that the compiler and/or operating system (you ...
    (comp.lang.fortran)
  • Re: xmalloc string functions
    ... char *foo, *bar; ... will quit and a memory leak will occur because the return ... value of dup() was not freed */ ...
    (comp.lang.c)
  • Re: pointers and strings
    ... The only correct and defined way to print the value of a pointer to ... > char *p; ... you would be creating a memory leak because you would be throwing away ... have an appropriate newsgroups line in your header for your mail to be seen, ...
    (comp.lang.c.moderated)
  • Re: Memory leak using NewAnsiString
    ... > there are some memory leaks, most of them involving the use of strings, ... > The debugger says that 's' generates a memory leak, ... > freed automatically by the delphi compiler? ... Pass the 's' string as a var parameter to the function? ...
    (borland.public.delphi.language.objectpascal)
  • Re: Non-paged pool memory leak in when huge number of files is created
    ... It's most likely not a memory leak. ... Usually file systems do not delete immediately at the file closing process the internal data structures which have been created for support of work with the opened files. ... char filename; ...
    (microsoft.public.win32.programmer.kernel)