LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-14-2007, 04:17 PM   #1
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Rep: Reputation: 31
Another simple C string question


Not really sure how the compiler handles this. Let's say a function returns a newly allocated string. If you use the function as a parameter to another function, how would you free the returned string? You couldn't... right?

ie:
Code:
char* str1 = "Some string";
/* ... */
printf ("string: %s", strdup(str));
/* what happens to the string returned by strdup? */
 
Old 01-14-2007, 04:42 PM   #2
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Not easily. Try using a temporary variable:

Code:
strTemp = strdup(str);
printf("string: %s", strTemp);
free(strTemp);
(Of course, because printf doesn't modify its arguments (unless "%n" is used), there's no point in calling strdup there.)

Last edited by Dan04; 01-14-2007 at 09:02 PM.
 
Old 01-14-2007, 05:06 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Dan04, did you perhaps mean:
Code:
strTemp = strdup(str);
printf("string: %s", strtTemp);
free(strTemp);
Unless I misunderstand something (which is, of course, quite possible) your suggestion doesn't actually free the memory used in the call.
 
Old 01-14-2007, 05:33 PM   #4
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Original Poster
Rep: Reputation: 31
That's what I was thinking... but I see this done from time to time. My original posting would be a memory leak then, correct?
 
Old 01-14-2007, 09:03 PM   #5
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Quote:
Originally Posted by PTrenholme
Dan04, did you perhaps mean:
Code:
strTemp = strdup(str);
printf("string: %s", strtTemp);
free(strTemp);
Yes. Thanks for noticing.
 
Old 01-16-2007, 01:35 PM   #6
KenJennings
LQ Newbie
 
Registered: Mar 2005
Location: FL, USA
Distribution: SuSE
Posts: 28

Rep: Reputation: 15
Quote:
Originally Posted by MicahCarrick
That's what I was thinking... but I see this done from time to time. My original posting would be a memory leak then, correct?
Yes, it is a memory leak. Wherever you see it done spank (politely correct ) the developer responsible.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
simple C++ string questioin, i hope Ha1f Programming 2 10-12-2005 08:23 PM
Simple bash script help, grabbing part of a string colabus Linux - Newbie 3 04-25-2005 09:42 AM
Replace a string including simple quotes. philipina Programming 4 03-18-2004 08:01 AM
spliting a simple string kubicon Linux - General 2 02-27-2004 04:11 AM
simple C string question mvendramini Programming 9 09-30-2003 07:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration