364,036 Members | 5222 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

return char pointer which is malloc'ed in function

Khookie
P: n/a
Khookie
Hi everyone

I'm not sure whether this is good practice or not, so I thought I'll
ask.

I've got a function that returns a char pointer, which is malloc'ed in
the function itself, as per below code. It's basically a singly
linked list of strings that are concatenated into a big string.

My question is - since you have to know to free it yourself (usage
below), is this considered bad practice? Or should the user of the
function know?

Function:
char* string_list_concat(string_list *list) {
char* result = malloc(list->length + 1);
char* result_ptr = result;

// Create string
string_node *node = list->bol;
while (node != list->eol) {
char* string_node_ptr = node->string;
while(*string_node_ptr != '\0') {
*result_ptr = *string_node_ptr;
string_node_ptr++;
result_ptr++;
}
node = node->next;
}
*result_ptr = '\0';
return result;
}

Usage:
char* result = string_list_concat(list);
printf("Output is: %s\n", result);
free(result);

Cheers

Chris
Dec 10 '07 #1
Share this Question
Share on Google+
3 Replies


Joachim Schmitz
P: n/a
Joachim Schmitz
"Chris Dollin" <chris.dollin@hp.comschrieb im Newsbeitrag
news:fjjgci$56g$1@tadcaster.hpl.hp.com...
Khookie wrote:
>
>I've got a function that returns a char pointer, which is malloc'ed in
>the function itself, as per below code. It's basically a singly
>linked list of strings that are concatenated into a big string.
>>
>My question is - since you have to know to free it yourself (usage
>below), is this considered bad practice? Or should the user of the
>function know?
>
(answer below)
>
>>
>Function:
>char* string_list_concat(string_list *list) {
> char* result = malloc(list->length + 1);
>
That can't be right: I think you have a missing multiplication by
`sizeof *result`.
Which here is 1 anyway, sizeof(char)...

Bye, Jojo


Dec 10 '07 #2

Chris Dollin
P: n/a
Chris Dollin
Joachim Schmitz wrote:
>>char* string_list_concat(string_list *list) {
>> char* result = malloc(list->length + 1);
>>
>That can't be right: I think you have a missing multiplication by
>`sizeof *result`.
Which here is 1 anyway, sizeof(char)...
Colour me idiotic: I kept seeing `result` there as `char **`. Thanks, Jojo.
Apologies to Khookie (and everyone else) for inflicting my Stupidity Of
The Day [1] on them.

[1] Please let it be the only one.

--
Chris "glasses - check; keys - check; king - check, BOOM" Dollin

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Dec 10 '07 #3

regis
P: n/a
regis
Joachim Schmitz wrote:
"Chris Dollin" <chris.dollin@hp.comschrieb im Newsbeitrag
news:fjjgci$56g$1@tadcaster.hpl.hp.com...
>
>>Khookie wrote:
>>
>>
>>>I've got a function that returns a char pointer, which is malloc'ed in
>>>the function itself, as per below code. It's basically a singly
>>>linked list of strings that are concatenated into a big string.
>>>
>>>My question is - since you have to know to free it yourself (usage
>>>below), is this considered bad practice? Or should the user of the
>>>function know?
>>
>>(answer below)
>>
>>>Function:
>>>char* string_list_concat(string_list *list) {
>> char* result = malloc(list->length + 1);
>>
>>That can't be right: I think you have a missing multiplication by
>>`sizeof *result`.
>
Which here is 1 anyway, sizeof(char)...
one would have expected that list->length
is the number of nodes of the list, instead of
the sum of strlen() applied to all node->string of the list...
Dec 11 '07 #4

Post your reply

Help answer this question



Didn't find the answer to your C / C++ question?

You can also browse similar questions: C / C++