Thread: Size of allocated memory

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Size of allocated memory

    If you want to allocate n bytes with malloc, does it allocate exactly n bytes, or the smallest greater or equal number of bytes that is a multiple of four? Or does it depent on which system you are running?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It depends entirely on your system.
    All that you know for sure is that it is at least the amount you asked for.

    Debug versions for instance can allocate more dead space than necessary to detect 'off-by-1' overruns without necessarily crashing the whole program by doing something bad to a vital part of the memory pool data structures.
    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.

  3. #3
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    I want to add that you shouldn't use malloc in a C++ application. The 'malloc' way of allocating memory isn't compatible with C++ class features. Use 'new' and 'delete', it's pretty much the standard for memory allocation,
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  4. #4
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Okey, that seems to make sense.

    I'm using DevCpp and WinXP, so I guess there is some way to find out how much a malloc function in my program (not in debug mode) allocates. Is there some way to find out for other systems and other compilers, maybe in precompiling or in runtime?

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Well, if you get a exception error when you run the program you know you have oversteped the boundarys of that particular type of memory. ie: the stack is full. Like Sentral said, avoid Malloc() unless you plan to learn straight C program memory managment. New a delete are safer in my view, you can set a deative pointer to NULL. When I learnt C at school, we were taught to not set a pointer to NULL, but I always do now, and have done for the past 3 years,

    Practise safe computing folkes - initalize your pointers and NULL deleted ones!!
    Double Helix STL

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There is no way. While the MS CRT does provide a function that reports the size of a dynamic block, it reports the number of bytes you requested, not those actually allocated.
    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

  7. #7
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Ok, thanks. The thing was that I wanted to make my own vector class. So that when the memory I requested gets full, I will have to request for another larger block of memory. I figured it maybe would be smart to request for a multiple of 4 or 8 bytes. However I can do that anyway. You never know.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> The thing was that I wanted to make my own vector class.
    Hopefully just as a learning exercise. It is highly unlikely that creating your own is a good idea for any other reason.

    >> I figured it maybe would be smart to request for a multiple of 4 or 8 bytes.
    It is. In fact, if you do some research on the platform you are targeting, you might find specific information on what values are good to use. There are also general guidelines that are good as well. For example, if you were writing a pool allocator you might want to allocate chunks that fit on a page in memory.

    In general, since many objects stored in a container are multiples of 4 bytes on 32 bit machines, you probably don't have to worry about that too much. Also, note that many vector implementations double the allocated space each time more space is needed, which provides a nice power of two size.

  9. #9
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    >> Hopefully just as a learning exercise. It is highly unlikely that creating your own is a good idea for any other reason.

    Yes, only as an exercise.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  3. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  4. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  5. total size of dynamic memory allocated array
    By trekker in forum C Programming
    Replies: 10
    Last Post: 03-10-2002, 12:59 PM