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_con cat(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_n ode_ptr != '\0') {
*result_ptr = *string_node_pt r;
string_node_ptr ++;
result_ptr++;
}
node = node->next;
}
*result_ptr = '\0';
return result;
}
Usage:
char* result = string_list_con cat(list);
printf("Output is: %s\n", result);
free(result);
Cheers
Chris 3 4889
"Chris Dollin" <ch**********@h p.comschrieb im Newsbeitrag
news:fj******** **@tadcaster.hp l.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_con cat(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
Joachim Schmitz wrote:
>>char* string_list_con cat(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
Joachim Schmitz wrote:
"Chris Dollin" <ch**********@h p.comschrieb im Newsbeitrag
news:fj******** **@tadcaster.hp l.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_con cat(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... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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)
|
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.
unsigned char foo(void)
{
return 0xC1;
}
unsigned char (*pfoo)(void) = foo;
|
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 = 0
|
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:
Code is doing malloc of variable sizes.
The last byte of malloc.ed memory is written a magic.
|
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" };
doSomething(strs, 3);
| |
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;
}
|
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: User enters 3 then program allocates equal to 9 integer memories.
Output:1
Enter a single digit number:
2
The multiplication table of 2 is:
1 2
|
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 and draw a table in the following format?
Hint: User enters 3 then program allocates equal to 9 integer memories.
Output:1
Enter a single digit number:
|
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>
#include<iostream.h>
int sum(int i,int j);
main()
{
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |