LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   does snprintf() allocate memory and does it need to be deallocated ? (https://www.linuxquestions.org/questions/programming-9/does-snprintf-allocate-memory-and-does-it-need-to-be-deallocated-790972/)

Aquarius_Girl 02-23-2010 05:26 AM

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 !

jf.argentino 02-23-2010 05:40 AM

"man snprintf" doesn't say so, so why do you think snprintf will allocate some memory?

Aquarius_Girl 02-23-2010 05:43 AM

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 ?

ForzaItalia2006 02-23-2010 05:47 AM

Hey,

Quote:

Originally Posted by anishakaul (Post 3873635)
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 -

ForzaItalia2006 02-23-2010 05:49 AM

Quote:

Originally Posted by anishakaul (Post 3873652)
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 -

Aquarius_Girl 02-23-2010 05:54 AM

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)

Aquarius_Girl 02-23-2010 06:25 AM

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 !!!!


All times are GMT -5. The time now is 11:00 PM.