malloc always setting length of 8?

This is a discussion on malloc always setting length of 8? within the C++ Programming forums, part of the General Programming Boards category; Why would it do that? Here's basically what it is... Code: char* file_source; file_source = (char*) malloc (strlen("Whatever I want")); ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    27

    malloc always setting length of 8?

    Why would it do that?
    Here's basically what it is...
    Code:
    char* file_source;
      file_source = (char*) malloc (strlen("Whatever I want"));
    No matter what I do, setting a static int in malloc, strlen, whatever, it gives me 8. I have about 3 or 4 malloc calls above this one that work fine. Why is this one giving me problems? No matter what I name it, its an issue o_0;


    Hmm.. Just played a bit with this... I commented out the above fclose, and malloc set fine. Why would fclose effect a malloc?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    How do you know you are only getting 8 bytes?
    And since this is in the C++ board you really should be using new

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > Why would fclose effect a malloc?
    You've basically trashed the malloc memory pool with some previous mis-use of memory. Once you've done that, what happens next is totally random (that's undefined to you).

    Consider the following abuses
    - use of a pointer before malloc is called
    - use of a pointer after free has been called
    - freeing the same memory more than once
    - stepping off the end of malloc'ed memory
    - getting the size wrong for allocated memory

    Your example code fails on this last point. If you want to allocate enough space for a string, then you need strlen(string)+1 bytes to store it in - strlen() does not count the \0 at the end.
    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
    Jul 2004
    Posts
    27
    Thanks salem. Though every other malloc I use I don't add the +1 to the end, and I have no issues. This one also works fine when I remove the fclose() above it.

    Thantos: I get "8" returned when I do a strlen() on the variable :/

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Zarkhalar
    I get "8" returned when I do a strlen() on the variable :/
    are you actually copying the string into the allocated memory? you know calling malloc(strlen("some string")+1); only allocates that memory, it doesnt copy the string value into it.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    27
    Yep, I know. But when I do a strlen() on any other malloc'd variable it shows 0, while this one shows 8 o_0;

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    that could be caused by any number of reasons. The most obvious to me is that the particular area in memory has some left over values in it. Remember malloc doesn't zero out the memory.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > Thanks salem. Though every other malloc I use I don't add the +1 to the end, and I have no issues.
    That's just storing up trouble for the future.
    Your program is already broken, you just don't know it yet.

    > Yep, I know. But when I do a strlen() on any other malloc'd variable it shows 0, while this one shows 8
    All malloc memory should be treated as uninitialised.
    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.

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. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  3. the length of my array is not what I expected
    By luca in forum C Programming
    Replies: 7
    Last Post: 12-05-2006, 02:14 AM
  4. Replies: 2
    Last Post: 03-24-2006, 07:36 PM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM

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