Thread: Malloc and realloc boom

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    52

    Malloc and realloc boom

    I got confused with some examples,

    wich one is correct?

    char s = malloc(81);

    or

    char s = malloc(81*sizeof(char));


    ?

    -----

    when i do:

    str = realloc(str, XX * sizeof(char));

    does the realloc free the old str memory?

    -----

    its

    str = realloc(str, XX * sizeof(char));
    or
    str = realloc(str, XX);

    ?

    Thanks!

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    malloc(81 * sizeof(char)) and malloc(81) are almost identical. If it's specifically for a string, than maybe you should use the first one, but if it's just for 81 bytes (can be used for whatever, not just strings) than use the second one. sizeof(char) should return 1. Yes realloc does free the original.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Since sizeof(char) is DEFINED by the C standard to be 1, multiplying by it makes little sense.

    realloc() _MAY_ free the memory originally allocated and allocate a different new block. It's more likely to do this if the new size is larger than the original size (depending on how much spare space there is available in the current allocation). If you are reducing the size but not by a lot (as in less than 10s or 100s of times the old size), then it will probably just keep the original memory.

    --
    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
    Registered User
    Join Date
    May 2008
    Posts
    52
    Why does malloc works in bytes not in bits?
    In assembly one char took me just 4 bits (one word).

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    52
    Quote Originally Posted by matsp View Post
    Since sizeof(char) is DEFINED by the C standard to be 1, multiplying by it makes little sense.
    --
    mats
    Doesnt the size used to represent data changes from machine to machine? Standard char can take 1 byte, but on other machine it can take more, or am i wrong?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The C standard DEFINES char as "one unit of size", and the unit of size is also "byte" - even if a "byte" is not precisely 8 bits [I believe it defines that it's AT LEAST 8 bits - which allows machines with for example 9 bit chars to work].

    Computers in general can not work with chunks of memory smaller than a byte, so there's no point in malloc dealing in smaller units. The fact is that most implementations of malloc will round the size up to some arbitrary chunk size, such as 32 or 64 bytes. A single byte is the absolutely smallest portion of memory that can be atomically updated by the processor - anything smaller and it will need to be split into

    A char will not fit in 4 bits - , that is, unless you have a character set of only 16 characters. You can potentially fit A-Z (upper case only), some punctuations and digits in 6 bits (or if you trick about a bit, 3 chars in 15 bits, using Radix-50).

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

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by NeMewSys View Post
    Why does malloc works in bytes not in bits?
    In assembly one char took me just 4 bits (one word).
    On most 32-bit machines a WORD is 32 bits, not 4. If I remember correctly a WORD is defined as the size of the address bus, so 32 bits for you Windows machine and 64 for you Gamecube.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    52
    Lol, i said a big mistake, in assembly i used 8 digits in Hex to represent one char, it means 16 bits = 1 char..

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    52
    When i do char *s = malloc(5); the final result is:

    [][][][][\0]

    or just:

    [][][][][] ?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by NeMewSys View Post
    Lol, i said a big mistake, in assembly i used 8 digits in Hex to represent one char, it means 16 bits = 1 char..
    8 hex digits = 4 bytes or 32 bits. 16 bits in x86 assembler is a WORD, which is 4 hex digits (it's defined as a word for all 16, 32 and 64-bit versions of x86, for 8, 16, 32, 64 and 128-bit bus width).

    A "word" is normally the size of a register (or sometimes the bus), but x86 has had several "extensions" to it, but a word is still the same as the original 8086 size, because otherwise old code would break on new machines/assemblers/compilers.

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

  11. #11
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by NeMewSys View Post
    When i do char *s = malloc(5); the final result is:

    [][][][][\0]

    or just:

    [][][][][] ?
    The latter. malloc just gives you the bytes, it doesn't do anything with them. To initialise the memory, use calloc.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc, realloc..wat on earth!
    By zesty in forum C Programming
    Replies: 3
    Last Post: 12-21-2007, 01:42 PM
  2. malloc and realloc
    By jayfriend in forum C Programming
    Replies: 4
    Last Post: 01-05-2007, 02:25 PM
  3. malloc, realloc
    By figo2476 in forum C Programming
    Replies: 3
    Last Post: 04-28-2006, 10:11 PM
  4. Linked list versus malloc and realloc.
    By Bajanine in forum C Programming
    Replies: 2
    Last Post: 06-20-2005, 08:08 PM
  5. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM