What if malloc returns a zero-length space?

This is a discussion on What if malloc returns a zero-length space? within the C Programming forums, part of the General Programming Boards category; no error in compiling time, no error while running, but I just feel insecure. I tested the size of returned ...

  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,264
    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
    32,834
    >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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    ZuK
    ZuK is offline
    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, 05:05 AM
  2. Out of space when compiling kernel
    By NuNn in forum Linux Programming
    Replies: 3
    Last Post: 04-01-2009, 03:43 PM
  3. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  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, 01:04 PM

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