LinuxQuestions.org
Help answer threads with 0 replies.
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 02-23-2010, 05:26 AM   #1
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
does snprintf() allocate memory and does it need to be deallocated ?


Code:
char  *fileToBeOpened;
snprintf (fileToBeOpened, 10, "%s%d", FILEPATH, 1);
Does the above code allocate 10 bytes to variable 'fileToBeOpened' and does the variable 'fileToBeOpened' need to be free() ?

I couldn't find much information on this from Google !

Kindly guide !
 
Old 02-23-2010, 05:40 AM   #2
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
"man snprintf" doesn't say so, so why do you think snprintf will allocate some memory?
 
Old 02-23-2010, 05:43 AM   #3
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Thanks for replying !

Yes, man snprintf doesn't say so, that's why I asked here.

variable 'fileToBeOpened' is a char*, so does the data stored in variable 'FILEPATH' gets stored in memory and 'fileToBeOpened' just points to it ?

Is it so ?

Last edited by Aquarius_Girl; 02-23-2010 at 05:47 AM.
 
Old 02-23-2010, 05:47 AM   #4
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Hey,

Quote:
Originally Posted by anishakaul View Post
Code:
char  *fileToBeOpened;
snprintf (fileToBeOpened, 10, "%s%d", FILEPATH, 1);
Does the above code allocate 10 bytes to variable 'fileToBeOpened' and does the variable 'fileToBeOpened' need to be free() ?

I couldn't find much information on this from Google !

Kindly guide !
no, it does not. Your call would store at most 10 characters (incl. the terminating NUL '\0' character) in the string, but it relies on the caller to allocate the appropriate amount of memory.

The alternative to snprintf would be asprintf/asnprintf which allocates memory for you. In this case, you would then have to call free for the returned memory address. BUT, keep in mind that asprintf/asnprintf is NOT part of the C99 standard (or any C standard), though you can't rely on the portibility of this function ...

Hope that helps,

- Andi -
 
1 members found this post helpful.
Old 02-23-2010, 05:49 AM   #5
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by anishakaul View Post
variable 'fileToBeOpened' is a char*, so does the data stored in variable 'FILEPATH' gets stored in memory and 'fileToBeOpened' just points to it ?

Is it so ?
If that _would_ be the case, the interface would require to pass in a char** due to the call-by-value parameter passing by C, but in this case you just pass in a char * so that snprintf can't return the new address ...

- Andi -
 
Old 02-23-2010, 05:54 AM   #6
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by ForzaItalia2006
no, it does not. Your call would store at most 10 characters (incl. the terminating NUL '\0' character) in the string,
Thanks for enlightenment

If there is no allocation, Where do these 10 characters get stored then ? (I mean how do they get stored in the variable 'fileToBeOpened')

Please explain more ! (I am sorry if it is a dumb question)

Last edited by Aquarius_Girl; 02-23-2010 at 06:15 AM.
 
Old 02-23-2010, 06:25 AM   #7
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
If there is no allocation, Where do these 10 characters get stored then ? (I mean how do they get stored in the variable 'fileToBeOpened')
Oh I am sorry for this question !!

I did overlook the fact that I unless I myself allocate memory to the variable
'fileToBeOpened' the above shown code will give me a segmentation fault !!!!
 
  


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
How to allocate memory to Java Humtum Debian 2 10-23-2009 04:11 PM
How much memory can all the processes allocate all together? Premraja Linux - Newbie 1 05-16-2009 05:38 PM
Cannot allocate memory simha Linux - Newbie 4 01-23-2008 09:19 AM
Cannot allocate memory Terroth Linux - General 13 02-09-2005 05:22 AM
allocate more memory? sockknitter Debian 3 11-06-2004 05:23 PM

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

All times are GMT -5. The time now is 09:34 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