468,484 Members | 1,675 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

return char pointer which is malloc'ed in function

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
3 4620
"Chris Dollin" <ch**********@hp.comschrieb im Newsbeitrag
news:fj**********@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
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
Joachim Schmitz wrote:
"Chris Dollin" <ch**********@hp.comschrieb im Newsbeitrag
news:fj**********@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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by strout | last post: by
2 posts views Thread by Bryan Parkoff | last post: by
15 posts views Thread by Simon Nickerson | last post: by
11 posts views Thread by Sushil | last post: by
12 posts views Thread by confusedcoder | last post: by
7 posts views Thread by lovecreatesbea... | last post: by
4 posts views Thread by msq2004 | last post: by
18 posts views Thread by HIRA HAMEED | last post: by
3 posts views Thread by poojagupta | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.