473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 4866
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: strout | last post by:
function F(e) { return function(){P(e)} } Can anybody tell me what the code is doing? If return another function all in a function I would do function F(e)
2
by: Bryan Parkoff | last post by:
I set Function Pointer variable to private so unauthorized users cannot access it, but they are allowed to use Get_ and Set_ functions to modify Function Pointer variable. Look at my example. ...
15
by: Simon Nickerson | last post by:
I have a function which looks like this: void rho(matrix_t *out, matrix_t *in) { static int firsttime = 1; static matrix_t *words; /* ... other variables ... */ if (firsttime) { firsttime...
11
by: Sushil | last post by:
Hi Gurus I've tried to come up with a small logical example of my problem. The problem is platform specific (MIPS) which I understand should not be discussed here. So here goes my example: ...
12
by: confusedcoder | last post by:
Suppose I have the following function: void doSomething(char** array, int size); Normally, I would call the function with an array of, say, 3 elements... char* strs = { "str1","str2","str3"...
7
by: lovecreatesbea... | last post by:
Shoud we declare non-pointer function parameters with const keywords? int main(void){ int f(const int i); int i; f(i); return 0; }
4
by: msq2004 | last post by:
Write a program using malloc function. In which you take input from user and allocate memory equal to square of this number. Which multiply numbers and draw a table in the following format? Hint:...
18
by: HIRA HAMEED | last post by:
hi send me solution of this asignment Write a program using malloc function. In which you take input from user and allocate memory equal to square of this number. Which multiply numbers...
3
by: poojagupta | last post by:
Hi,I am confused and need the answer of the question mentioned below soon. return(x,y) is valid? Can a return() function return more than one value within one statement. eg: #include<stdio.h>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.