how sprintf works ?

This is a discussion on how sprintf works ? within the C Programming forums, part of the General Programming Boards category; Hello my code goes like this: Code: char* pszLogStr = NULL; pszLogStr = (char *)calloc(100,sizeof(char)); if (pszLogStr == NULL) { ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    how sprintf works ?

    Hello my code goes like this:

    Code:
    char* pszLogStr = NULL;
    pszLogStr = (char *)calloc(100,sizeof(char));
    	if (pszLogStr == NULL)
    		{
    		return(1);
    		}
    sprintf(pszLogStr,"Looks might be deceiving : %d",007);
    free(pszLogStr);
    Please let me know if sprintf changes the memory address of pszLogStr or only does a string copy on it so that I can free it later.

    Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It just does a (complicated) string copy - it doesn't change the address of your point [in fact, it can not do that, since you are passing a pointer, not a pointer to pointer].

    On a side-note: I personally wouldn't use calloc (or other malloc-related functions) for a buffer of 100 bytes - just use a local string variable. The overhead of malloc is not reflected in the memory saving you make (malloc itself will use up at least 16 bytes in the first place).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    Thanks. But I have another question.

    Suppose I use the local string as you suggested, will the memory for that string be freed once I get out of the function? I know that the variable is no longer accessible if I get out of the function but is the memory for the local variables freed too ?

    As far as im aware there is no garbage collection in C. So how will this help?

    Also is it correct that Calloc looks for contiguous memory location to allocate whereas Malloc does not look for contiguous memory location?
    Last edited by dunxton; 03-25-2009 at 07:57 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The memory used by the local array (string) will be freed when you leave the function, yes. If that's not what you want, then you have to allocate memory (or pass a pointer in from a lower level call) to store the string in [e.g. you want to return a string from a function]. If you allocate memory, then remember to free it when it's done with.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anybody show me why this works??
    By tzuch in forum C Programming
    Replies: 2
    Last Post: 03-29-2008, 09:03 AM
  2. sprintf
    By krappykoder in forum C++ Programming
    Replies: 12
    Last Post: 11-18-2004, 04:45 PM
  3. sprintf and sscanf
    By tommy69 in forum C Programming
    Replies: 10
    Last Post: 04-22-2004, 08:00 PM
  4. sprintf problem
    By waxby in forum C Programming
    Replies: 4
    Last Post: 01-15-2003, 02:48 PM
  5. is there any way to write a float to a ch string
    By mayfda in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2002, 05:04 PM

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