Thread: Space allocated for character string

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    13

    Space allocated for character string

    I have created the following function to print out an error message. The message comes back into a character string pszMessage from a call to LSgetErrorMessage.

    Code:
    void printError(pLSenv pEnv, char* origin, int nErrorCode) {
      char* pszMessage = "";
      int nn = LSgetErrorMessage(pEnv, nErrorCode, pszMessage);
      printf( "\nError from %s: %d - %s\n",origin,nErrorCode,pszMessage);
    }
    It is called called by the main program:
    Code:
    printError(env,"LScreateEnv",nErrorCode);
    I am concerned that the string returned in pszMessage might be overwriting memory, but I'm not sure. Is the above code ok or will it stomp on memory. If so, how should it be modified to be safe?

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    695
    Worse! You can't write to the constant empty string pszMessage is pointing to!

    Code:
    void printError(pLSenv pEnv, char* origin, int nErrorCode) {
      char pszMessage[512] = "";
    
      int nn = LSgetErrorMessage(pEnv, nErrorCode, pszMessage);
    
      printf( "\nError from %s: %d - %s\n",origin,nErrorCode,pszMessage);
    
    }
    Now you have a local char array of 512 bytes, and is NULLed out.
    The size, currently 512 should be a #define, instead of a "Majic Number".

    Expand the size if you think the error message might be longer.

  3. #3
    Registered User
    Join Date
    Sep 2015
    Posts
    13
    Ok great thanks.
    Do I not need to free the memory for pszMessage when I leave this function? Or will it be automatically freed when exiting the function?

  4. #4
    Registered User
    Join Date
    Sep 2015
    Posts
    13
    I guess it's just a local variable (on the stack), so gets freed automatically on return. It would different I guess if I used calloc to create the space for the Message. Then I would have to free() it. Correct?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,501
    It's an array of char, so you couldn't even manually free the memory allocated for it if you wanted to, no more than you could free the memory for nErrorCode.

    By the way, why are you using Hungarian name prefixes like p, n, and psz?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2015
    Posts
    13
    Quote Originally Posted by laserlight View Post
    By the way, why are you using Hungarian name prefixes like p, n, and psz?
    Just using the same variable names and conventions as the sample code we were given with the application.

  7. #7
    Registered User
    Join Date
    Sep 2015
    Posts
    13
    Just thinking do I need to append \0 to the string literal when calling my function?
    printError(env,"LScreateEnv",nErrorCode);

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    695
    Quote Originally Posted by ferrad View Post
    Just thinking do I need to append \0 to the string literal when calling my function?
    A String literal in double quotes, DOES contain a '\0' at the end so you do not have to add one.

    You need to study a good book on the C Language. This would explain this and other concepts a C programmer needs to know.

  9. #9
    Registered User
    Join Date
    Sep 2015
    Posts
    13
    Quote Originally Posted by rstanley View Post
    You need to study a good book on the C Language. This would explain this and other concepts a C programmer needs to know.
    I have, many times over my career, but I am a Fortran programmer and only have to deal with C stuff once in a blue moon. It always baffles me, so I try for myself, and then ask.

  10. #10
    Registered User
    Join Date
    Sep 2015
    Posts
    13

    char declaration

    BTW why does pszMessage have to be declared as:
    Code:
    char pszMessage[LS_MAX_ERROR_MESSAGE_LENGTH] = "";
    and not:
    Code:
    char* pszMessage[LS_MAX_ERROR_MESSAGE_LENGTH] = "";
    The h declaration says:
    Code:
    int LS_CALLTYPE LSgetErrorMessage(pLSenv pEnv,
                                 int     nErrorcode,
                                 char    *pachMessage);
    which to me means it should be declared as char*, but this does not compile.

  11. #11
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    695
    An array name is not a normal variable. The array name is resolved by the compiler at compile time as the address of the first element of the array. So the array name by itself IS a "char *".
    Code:
    char pszMessage[LS_MAX_ERROR_MESSAGE_LENGTH] = "";
    This is an array of chars. This is correct!


    Code:
    char* pszMessage[LS_MAX_ERROR_MESSAGE_LENGTH] = "";
    This is an array of pointers to chars, not what you want!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with ignoring "space" character in a string.
    By Grigoris Savvas in forum C Programming
    Replies: 2
    Last Post: 10-20-2012, 10:07 AM
  2. Implementing space allocated for a character array
    By nicoeschpiko in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2011, 04:36 PM
  3. Free allocated space
    By Fader_Berg in forum C Programming
    Replies: 1
    Last Post: 10-31-2010, 07:49 AM
  4. getting the size of allocated space
    By cs32 in forum C Programming
    Replies: 3
    Last Post: 02-24-2008, 08:18 AM
  5. Converting space character in string to integer
    By boostage in forum C++ Programming
    Replies: 6
    Last Post: 10-19-2006, 08:40 PM

Tags for this Thread