C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-10-2009, 09:45 AM   #1
Registered User
 
Join Date: Feb 2009
Posts: 278
malloc/free of return value

I'm writing a function that returns a C-style string (char *). THe function performs several operations on the 2 incoming strings and returns the result. While I'm performing these operations I use a temp variable...

Code:
char * sTemp = malloc((sizeof(s1) * strlen(s1)) + (sizeof(s2) * strlen(s2)));
Then at the end of the function I return sTemp. But what about deallocating the memory I allocated for sTemp? Is is automatically deallocated when the function completes? if not can I deallocate AFTER the return statement?
Bladactania is offline   Reply With Quote
Old 02-10-2009, 09:48 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by Bladactania
Then at the end of the function I return sTemp. But what about deallocating the memory I allocated for sTemp?
The caller (or the caller's caller, etc) should be responsible for deallocating the memory.

Quote:
Originally Posted by Bladactania
Is is automatically deallocated when the function completes?
No, and that is a Good Thing, otherwise the caller would then have a pointer to deallocated memory.

Quote:
Originally Posted by Bladactania
if not can I deallocate AFTER the return statement?
No, but then as I noted, the caller can deallocate the memory, so it is kind of like deallocating after the return statement.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 02-10-2009, 09:50 AM   #3
Registered User
 
Join Date: Feb 2009
Posts: 278
Thanks for the quick reply...

How does the caller deallocate the memory for a pointer with function level scope?
Bladactania is offline   Reply With Quote
Old 02-10-2009, 09:54 AM   #4
Registered User
 
Join Date: Feb 2009
Posts: 278
Another thing... If I'm returning a pointer to a string like this, do I need to allocate the memory for the pointer I'm returning the value into?

Code:
test = malloc(.....);
test = mystringfunction(s1, s2);
Bladactania is offline   Reply With Quote
Old 02-10-2009, 10:00 AM   #5
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by Bladactania
How does the caller deallocate the memory for a pointer with function level scope?
It uses a pointer. The pointer in the function is destroyed when it goes out of scope, so another pointer is needed to store the address of the dynamically allocated object.

Quote:
Originally Posted by Bladactania
If I'm returning a pointer to a string like this, do I need to allocate the memory for the pointer I'm returning the value into?
Well, you probably do not need to dynamically allocate memory for such a pointer as a local variable will do.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 02-10-2009, 10:02 AM   #6
Registered User
 
Join Date: Feb 2009
Posts: 278
Here's my function Code...

Code:
 char * String_Concat (const char *s1, const char *s2) {
   char *sTemp = malloc((sizeof(s1) * strlen(s1)) + (sizeof(s2) * strlen(s2)));

   strcpy(sTemp, s1);
   strcat(sTemp, s2);

   return sTemp;
 }
In main I call the function like so...

Code:
char   *sDiagnostic_Log_Path; // Full path of diagnostic log file

sDiagnostic_Log_Path = String_Concat(sFlash_Card_Base_Path, sDiagnostic_Log_Name);
Is that sufficient? I'm terribly scared of memory leaks!
Bladactania is offline   Reply With Quote
Old 02-10-2009, 10:03 AM   #7
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Code:
char* foo() { return malloc(10); }
void foo2(char** p) { *p = malloc(10); }
int main()
{
    char* p;
    p = foo();
    free(p);
    foo2(&p);
    free(p);
}
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 02-10-2009, 11:29 AM   #8
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> char *sTemp = malloc((sizeof(s1) * strlen(s1)) + (sizeof(s2) * strlen(s2)));
Why sizeof(s1) ?
That's the size of the pointer, not a single char.

Cheaply, it's
char *sTemp = malloc( strlen(s1) + strlen(s2) + 1);

Pedantically, in line with what you're doing
char *sTemp = malloc((sizeof(s1[0]) * strlen(s1)) + (sizeof(s2[0]) * strlen(s2)) + 1);
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 02-10-2009, 12:03 PM   #9
Registered User
 
Join Date: Feb 2009
Posts: 278
Could I use sizeof(*s1)? I suppose I could use sizeof(char) since I know it will be a character.
Bladactania is offline   Reply With Quote
Old 02-10-2009, 12:04 PM   #10
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
If you like, it's the same thing.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 02-10-2009, 12:06 PM   #11
Registered User
 
Join Date: Feb 2009
Posts: 278
when using sizeof(*s1) I get a "Nonportable pointer conversion" warning... Is there a way to avoid this?
Bladactania is offline   Reply With Quote
Old 02-10-2009, 12:09 PM   #12
Registered User
 
Join Date: Feb 2009
Posts: 138
avoid it by not using sizeof(*s1). sizeof(char) is 1 anyway and N*1 is always going to be N so it's redundant.
Meldreth is offline   Reply With Quote
Old 02-10-2009, 12:14 PM   #13
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
What a strange error message from applying sizeof to something.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 02-10-2009, 12:25 PM   #14
Registered User
 
Join Date: Feb 2009
Posts: 278
I think the warning comes from the strlen(s1) not the sizeof(char).
Bladactania is offline   Reply With Quote
Old 02-10-2009, 12:25 PM   #15
Registered User
 
Join Date: Feb 2009
Posts: 278
I am using Turbo C version 3.0... *sigh*
Bladactania is offline   Reply With Quote
Reply

Tags
functions, malloc, memory, pointers, strings

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Another weird error rwmarsh Game Programming 4 09-24-2006 10:00 PM
Why only 32x32? (OpenGL) [Please help] Queatrix Game Programming 2 01-23-2006 02:39 PM
opengl help heat511 Game Programming 4 04-05-2004 01:08 AM
opengl code not working Unregistered Windows Programming 4 02-14-2002 10:01 PM
Algorithm to walk through a maze. Nutshell C Programming 30 01-21-2002 01:54 AM


All times are GMT -6. The time now is 09:06 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22