Thread: Need some memory help

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    26

    Need some memory help

    I think I'm a little confused.
    Could someone please shed some light on memory allocation.

    Basically I would like to create a variable that can hold any amount of bytes.
    I do not know how many bytes I will receive into that variable.
    How would I create a variable with no limits to size?

    Thank you

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Short answer: You can't.

    You can allocate a set amount of memory with malloc(), and then use realloc() to extend it [as long as there is sufficient memory in the system].

    This should be done in increments of something bigger than one (because realloc() will have to copy the data when you grow it, so the "cost" of copying large amounts of data that is growing by small amounts is quite noticable) - doubling the size each time you expand it is a good plan.

    Starting at a decent size is of course a good idea.

    --
    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 ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Basically I would like to create a variable that can hold any amount of bytes.
    Create char pointer variable and allocate on byte and keep on reallocating one byte when every need. But eventually when the char pointer had gone big the realloc will suffere allocating extra bytes.

    ssharish

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As pointed out above, reallocating with only one byte every time is very inefficient. Make a larger initial buffer and double it every time you need more. Remember: we have memory a plenty in today's computers, so you don't need to be cheap.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If you're talking about an arbitrary-precision number, the magic phrase is "arbitrary-precision arithmetic". For C, a good package for this is GMP. Don't try to implement this yourself if you can help it since doing it efficiently requires machine-specific code.

    http://gmplib.org/

  6. #6
    Registered User
    Join Date
    Apr 2006
    Location
    Larnaca in Cyprus
    Posts
    32

    malloc

    Code:
    type x = (type *) malloc(sizeof(type));
    sizeof(type) counts the size of the variable for example

    Code:
    (int*)malloc(sizeof(int))
    get 4bytes mem

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by cs05pp2 View Post
    Code:
    (int*)malloc(sizeof(int))
    get 4bytes mem
    only on some systems.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Not to mention that casting malloc() is best avoided.

    But cs05pp2 is right in that it's best to use sizeof() for memory allocation instead of hard-coded values like 4 -- except for allocating characters, because sizeof(char) is always one. In other words, to allocate 4 characters, you can just use 4 rather than 4 * sizeof(char).

    It's also not a bad idea to use sizeof(*variable) rather than sizeof(type), in case you change the type of the variable.

    Code:
    type x = (type *) malloc(sizeof(type));
    Presumably you meant
    Code:
    type *x = (type *) malloc(sizeof(type));
    Anyway, my suggestion is
    Code:
    type *p = malloc(sizeof(*p));
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  5. What's the best memory (RAM) type?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 12-15-2001, 12:37 AM