*char and *t_symbol confusion

Oct 14, 2006 at 10:34am

*char and *t_symbol confusion

I have a t_symbol *prefix, member of my object struct and set by an
argument in my *new function :

if ( (ac==1) && (av->a_type == A_SYM) )
{
x->prefix = av->a_w.w_sym;
if (debug) post(“prefix :%s”, x->prefix->s_name);
}

Ok. Then i have a t_symbol *target, also member of my struct, which i
try to be set dynamically when a list is received. The first int of the
list should be concatenated (?) with the prefix to become a receiver’s
name. I do :

char *temp = (char *) malloc(sizeof(x->prefix)+ sizeof(int));
sprintf(temp, “%s%d”, x->prefix->s_name, av+0);
x->target = gensym(temp);
free(temp); // BTW, if you think it’s dirty, you can give me a better
way ;-)
if (debug) post(“List sent to %s”, x->target->s_name);

and i know it’s not good ’cause i’m messing *char & *t_symbol…

Any help ?

cheers

f.e


f.e chanfrault | aka | personal computer music
> >>>>>> http://www.personal-computer-music.com
> >>>>>> |sublime music for a desperate people|

#28140
Oct 14, 2006 at 10:41am

Sorry mates, the problem was the av type, not the chars or symbols :

sprintf(temp, “%s%d”, x->prefix->s_name, av->a_w.w_long);

But still wanting any better ideas about my pointers dirtyness…

cheers

f.e

f.e chanfrault | aka | personal computer music
> >>>>>> http://www.personal-computer-music.com
> >>>>>> |sublime music for a desperate people|

f.e wrote:
> I have a t_symbol *prefix, member of my object struct and set by an
> argument in my *new function :
>
> if ( (ac==1) && (av->a_type == A_SYM) )
> {
> x->prefix = av->a_w.w_sym;
> if (debug) post(“prefix :%s”, x->prefix->s_name);
> }
>
> Ok. Then i have a t_symbol *target, also member of my struct, which i
> try to be set dynamically when a list is received. The first int of
> the list should be concatenated (?) with the prefix to become a
> receiver’s name. I do :
>
> char *temp = (char *) malloc(sizeof(x->prefix)+ sizeof(int));
> sprintf(temp, “%s%d”, x->prefix->s_name, av+0);
> x->target = gensym(temp);
> free(temp); // BTW, if you think it’s dirty, you can give me a better
> way ;-)
> if (debug) post(“List sent to %s”, x->target->s_name);
>
> and i know it’s not good ’cause i’m messing *char & *t_symbol…
>
> Any help ?
>
> cheers
>
> f.e
>

#86097
Oct 14, 2006 at 10:48am

f.e wrote:

> I have a t_symbol *prefix, member of my object struct and set by an
> argument in my *new function :
>
> if ( (ac==1) && (av->a_type == A_SYM) )
> {
> x->prefix = av->a_w.w_sym;
> if (debug) post(“prefix :%s”, x->prefix->s_name);
> }
>
> Ok. Then i have a t_symbol *target, also member of my struct, which i
> try to be set dynamically when a list is received. The first int of the
> list should be concatenated (?) with the prefix to become a receiver’s
> name. I do :
>
> char *temp = (char *) malloc(sizeof(x->prefix)+ sizeof(int));

If you want to append an integer number (i.e. ’23′ or ’173′) to a
string, the above code is wrong, because you alloc memory for
sizeof(int) for your number (which shoule work for up to 4 digits, but
not with more)! The real size (in char) depends on the value and you
don’t the final value know until you have converted into to char*.

I would probably do something like that:

char num[32]; // lets assume that ints don’t have more than 32 digits
sprintf(num, “%d”, (int)argv[0].a_w.w_long);
char *temp = (char *)malloc((strlen(x->prefix) + strlen(num)) *
sizeof(char));

> sprintf(temp, “%s%d”, x->prefix->s_name, av+0);

Maybe use strcpy() here, since you now have num already.

hope this helps,
Olaf

#86098
Oct 14, 2006 at 10:51am

Hey, great ! That’s safer, for sure. Thanks Olaf

f.e

f.e chanfrault | aka | personal computer music
> >>>>>> http://www.personal-computer-music.com
> >>>>>> |sublime music for a desperate people|

Olaf Matthes wrote:
> f.e wrote:
>
>> I have a t_symbol *prefix, member of my object struct and set by an
>> argument in my *new function :
>>
>> if ( (ac==1) && (av->a_type == A_SYM) )
>> {
>> x->prefix = av->a_w.w_sym;
>> if (debug) post(“prefix :%s”, x->prefix->s_name);
>> }
>>
>> Ok. Then i have a t_symbol *target, also member of my struct, which i
>> try to be set dynamically when a list is received. The first int of
>> the list should be concatenated (?) with the prefix to become a
>> receiver’s name. I do :
>>
>> char *temp = (char *) malloc(sizeof(x->prefix)+ sizeof(int));
>
> If you want to append an integer number (i.e. ’23′ or ’173′) to a
> string, the above code is wrong, because you alloc memory for
> sizeof(int) for your number (which shoule work for up to 4 digits, but
> not with more)! The real size (in char) depends on the value and you
> don’t the final value know until you have converted into to char*.
>
> I would probably do something like that:
>
> char num[32]; // lets assume that ints don’t have more than 32 digits
> sprintf(num, “%d”, (int)argv[0].a_w.w_long);
> char *temp = (char *)malloc((strlen(x->prefix) + strlen(num)) *
> sizeof(char));
>
>> sprintf(temp, “%s%d”, x->prefix->s_name, av+0);
>
> Maybe use strcpy() here, since you now have num already.
>
> hope this helps,
> Olaf
>

#86099

You must be logged in to reply to this topic.