473,390 Members | 1,149 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,390 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 4864
"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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.