stupid talloc questions

Matthias Dieter Wallnöfer mdw at
Thu Sep 24 08:56:46 MDT 2009

Hi Holger!

Holger Hetterich schrieb:
> Hi,
> still I am not really used to talloc.
> Therefore I would like to ask some questions, and in advance forgive me if this are
> the most stupid questions you've ever seen on this list. The first three questions are
> just for understanding and "to make sure" (I've read the talloc-guide but just want to
> get this clean), the last is about talloc_tos().
> The following code is untested, just written:
> void *context;
> char *a;
> a = talloc (context, char *);
> if ( a!= NULL) { /*do something */ }
> talloc_free(context);
> My understanding is that talloc gives me an "unlimited" number of memory under a context.
No, you specify the size with the second argument of "talloc" (in this 
case "char *": that means you will get four or eight bytes (depends on 
the platform) for the "char*"-pointer). After the call is performed it 
returns a pointer to the memory place (similar to "malloc" - but with 
the right type - in this example it is from type "char**").
I don't think that you really want to achieve this. I bet that you 
wanted to allocate a string buffer. To do this with a predefined fixed 
size you should use "talloc_array" (in your case: "talloc_array(context, 
char, <str-length+1 (termination character)>)"). For makeing it 
afterwards bigger consider using "talloc_realloc".
To be able to allocate memory with specifying only the size (like 
"malloc") use "talloc_size". This returns a "void*" pointer. You are 
then able to cast the result pointer to the right type. I would use this 
only where it does really make sense since - as you might imagine - you 
have no type checking.
> So freeing context should free a.
> Is this correct?
Yeah - a parent context frees in general also all child contexts (but 
there exist some exceptions on special cases). But if childs have only 
one parent this works.
> Next:
>         void *context;
>         char *a;
>         char *b;
>         a = talloc (context, char *);
>         b = talloc (context, char *);
>         if ( a!=NULL && b!=NULL ) { /*do something */ }
>         talloc_free(context);
> If what is said above is true, in this case, a and b should be freed. Correct?
> Next:
>       void *context;
>       char *a;
>       char *b;
>       char *c;
>       a = talloc (context, char *);
>       b = talloc (a, char *);
>       c = talloc (b, char *);
>       /* do something */
>       talloc_free(b);
> Is this possible? According to the docs, the returned value is itself a talloc context.
> So freeing b should result in freeing b and c. Correct?
> Next:
> char *a;
> a = talloc ( talloc_tos(), char *);
> if ( a!=NULL) /* do something */
> I suppose tos() means top-of-stack. When and how will this memory be freed?
> Do I have to use talloc_free on this? And if so, with which context?
Sorry, this feature I never used. I cannot answer here.
> Thanks and sorry for the stupid questions.
> Holger
Matthias Dieter Wallnöfer
> --
> Holger Hetterich, hhetter at, 
>   SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nürnberg)
>    Maxfeldstr. 5, 90409 Nürnberg, Germany

More information about the samba-technical mailing list