Thread: Why return malloc'd char array not work, but local char array does?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    Why return malloc'd char array not work, but local char array does?

    Inside a function, I create a char array which I pass to a library function that fills it with a value. I then pass this back to the caller. When I do it by creating an array with malloc (and then freeing it outside the function), it only fills the array with the first 3 characters. When i create it as a local array, it works (and simply warns me I'm passing a local array back out).

    Code:
    //This does NOT Work:
    char* getStringWithMalloc(...elided...)
    {
          char* data = ( char* ) malloc( 256 * sizeof( char ) );
          fillArray( data );
          return data;
    }
    
    //This DOES work:
    char* getString(...elided...)
    {
          char data[256];
          fillArray( data );
          return data;
    }
    
    int main(...)
    {
          //Does not work, only grabs first three characters
          char* value = getDataWithMalloc(...);
          printf( value );
          free( value );
    
          //This DOES work
          printf( getDataWithMalloc(...) );
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    That's weird. Have you tried stepping through with a debugger?

    EDIT:
    This thread is clearly of duplicate of this one, hence it is now closed.
    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

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think this is the third thread about the same subject. Maybe we should merge them together.

    --
    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.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    "Work"?

    The first one should work, but has a horrible interface.
    The second is simply broken. Deadly, even.

    Whatever error you're seeing, though, it's got to be in fillArray.

    Also, this is pretty much C.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 11-18-2008, 02:59 AM
  2. C problem with legacy code
    By andy_baptiste in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 06:14 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM