Thread: *szString = things question/memory question

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    *szString = things question/memory question

    Hi,
    I wasn't sure what to google for this one, but I did try (at least a little).

    I'm wondering what happens, with respect to memory, when you do this

    Code:
    void myFunction(char* szInput)
    {
    char* szTemp = "Hello";
    szInput = szTemp;
    return;
    }
    The local copy of szInput is replaced by szTemp. No malloc was done, so no free'ing needs to be done on szTemp.
    What about szInput? Will the implementation "know" to free the original location of the copy of szInput, or is that a memory leak?

    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    szInput is itself just a copy of something else, so overwriting it has no consequence to the caller.

    Code:
    p = malloc(10);
    myFunction( p );
    // p is unchanged here, you still need to free it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Thanks.
    Short followup question - something I don't really understand about char* in general.
    if I did say
    Code:
     void myFunction(int* pnInput)
    {
    *pnInput = 500;
    return
    }
    Then the caller would actually notice the change in pnInput.
    But in
    Code:
     void myFunction(char* szInput)
    {
    *szInput = 50;
    return
    }
    I think it crashes (can't try at the mo).
    (I'm aware that the proper way to do it is to have the function header use a char** pszInput, but I just want to understand what's going on anyway)
    Are char*s privileged in some way?
    These are pretty basic questions, so if you don't want to answer, I'd be absolutely glad to just be linked to an article or something, so I can read up myself and play around.

    Thanks

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well as soon as you start doing *param = value, you need to make sure your input pointer is pointing at valid memory.

    Otherwise various random things, like crashing, will start to happen.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  2. Question about binary trees and files
    By satory in forum C Programming
    Replies: 9
    Last Post: 03-06-2006, 06:28 AM
  3. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM
  4. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM