Thread: Why is malloc'd char array not working but char p[256] is?

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

    Why is malloc'd char array not working but char p[256] is?

    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This is related to your other question with usb values, isn't it. My guess would be that fillarray is told the size of the array with sizeof...

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

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Yeah, I thought that sizeof() would be the same for a malloc'd array and a char[] array.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by 6tr6tr
    Yeah, I thought that sizeof() would be the same for a malloc'd array and a char[] array.
    hmm... but if you are passing the array to fillArray(), the array decays to a pointer to its first element, so there would be no difference... unless your example of fillArray() excluded the size argument.
    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

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by 6tr6tr View Post
    Yeah, I thought that sizeof() would be the same for a malloc'd array and a char[] array.
    It is not. Sizeof is resolved at compile time, and although the compiler may well understand the function malloc, it wouldn't understand if you actually want the size of the allocated memory or the size of the pointer. There are potentially non-standard functions that gives the size of the allocation, but you would be in an awkward position of dependancy on a particular compiler or C library if you start using them.

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

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    //This DOES work:
    char* getString(...elided...)
    {
          char data[256];
          fillArray( data );
          return data;
    }
    But of course you must know that returning a local variable like that is completely wrong, since the memory where that local variable resides is reused after the function returns and will eventually get overwritten with other stuff.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    Code:
    //This DOES work:
    char* getString(...elided...)
    {
          char data[256];
          fillArray( data );
          return data;
    }
    But of course you must know that returning a local variable like that is completely wrong, since the memory where that local variable resides is reused after the function returns and will eventually get overwritten with other stuff.
    As I pointed out in the third thread on the same subject by the same author - that's why having multiple threads is a bad thing.

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

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    As I pointed out in the third thread on the same subject by the same author - that's why having multiple threads is a bad thing.

    --
    Mats
    I don't read all the posts - just the interesting ones, so that's why I didn't see it.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Please don't ever do that ...elided... thing. Whenever you do that, you haven't done enough of what you should be doing to solve the problem yourself.

    If you haven't eliminated the effects those parts have on your problem then you need to do more work to eliminate those parts as a possible cause before posting. Once you've determined that it isn't related to the problem, then your simplest complete compileable example that you post shouldn't contain it since it is no longer relevant. If you determine that it is part of the problem then you'd be wasting your time and ours by leaving it out.

    Usually it is part of the problem. People have a habbit of leaving the part out that they think isn't the problem. If where they thought the problem was, actually was where the problem was, then they would already know the solution.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    I don't read all the posts - just the interesting ones, so that's why I didn't see it.
    Yes, and my point was that there shouldn't be three different threads discussing the exact same subject with slightly different titles and somewhat different content.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  2. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  3. help with array of char and char **
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-20-2002, 02:23 PM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM