Thread: What if malloc returns a zero-length space?

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    64

    What if malloc returns a zero-length space?

    no error in compiling time, no error while running, but I just feel insecure. I tested the size of returned space, it was 4. don't know why not 0?????
    what wrong with it?
    Code:
    char *space = NULL;
    size = GetSize(text);
    /* zero returned to "size" */
    
    space = malloc(size);
    lstrcpy(space, "\0");
    /* initialize "space" */

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I'm guessing you're testing the size of the returned space by doing something like:
    Code:
    int sizeofspace = sizeof(space)
    This is returning the size of the pointer which points to the allocated memory. The reason it is returning 4 is that pointers on 32 bit machines are 4 bytes. If malloc() succeeds, then the size of the buffer that is returned is always equal to the size in bytes of the value passed to malloc().

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    >space = malloc(size);
    If size is zero, then you may find that space is actually a NULL pointer (or maybe not). In any event, you can't dereference the pointer since no bytes have been allocated.

    The only valid thing you could do at this point would be
    free( space );
    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.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    It's hard to explain without the context in the code. I will paste them here.

    Here I need to make sure one thing first. How to initial a variable as follows:

    Code:
    LPSTR textOutput = NULL;
    textOutput = malloc(1024);
    lstrcpy(textOutput, "\0");  /* Initialization, is it correct? */

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Don't know what lstrcpy is. I'd use
    Code:
    if ( 0 != textOutput ) { /* malloc successful */
        textOutput[0]='\0'; /* initialize to "" */ 
        .....
    else {
       /* malloc did not return a valid pointer */
    }
    Kurt

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    lstrcpy is almost the same as strcpy. (Maybe, not very sure.)

    And question here is, malloc(0) also returns a valid pointer but not NULL, so I am worrying when textOutput[0] = '\0', it is exactly writing on a place where doesn't belong to it.

  7. #7
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    lstrcpy is almost the same as strcpy. (Maybe, not very sure.)
    If you're not sure what it does, you really shouldn't be using it.
    And question here is, malloc(0) also returns a valid pointer but not NULL, so I am worrying when textOutput[0] = '\0', it is exactly writing on a place where doesn't belong to it.
    Your best bet is to avoid passing 0 to malloc() in the first place. It's an easy case to test for, or you can write your code to keep it from ever giving you 0 for a non-error condition.
    Just because I don't care doesn't mean I don't understand.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    64
    I just referred to msdn help and knew that lstrspy also deals with wide charactor, unicode.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Out of space when compiling kernel
    By NuNn in forum Linux Programming
    Replies: 3
    Last Post: 04-01-2009, 02:43 PM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. Recursion Revisited again, and again!
    By clegs in forum C++ Programming
    Replies: 93
    Last Post: 12-08-2007, 08:02 PM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM