Results 1 to 10 of 10

Thread: Dynamic Memory Allocation

  1. #1

    Thread Starter
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394

    Question Dynamic Memory Allocation

    Hi,

    I have found Three (3) ways to allocate dynamic memory. I am quit confused about which one I should use in my Win32 API app.

    malloc.h - malloc() calloc() and more
    I always use this one

    windows.h - HeapCreate() Heap Alloc() and more
    I dont know anything about these

    windows.h - VirtualAlloc() and more
    I think they are for large block memory allocation

    If anyone can explain any safety/security issues between these, please help.

    Thanks for any help
    VS.NET 2003

    Need to email me?

  2. #2
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Assuming C, malloc(). That way you are a bit closer to portable code. Besides, deep down, malloc is implemented with win32 API calls on windows anyway.

    If C++, use new, which is generally malloc, and calls a constructor.

    Z.

  3. #3

    Thread Starter
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    Thanks Z.
    VS.NET 2003

    Need to email me?

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    When I write a larger app where I have the CRT anyway I use new in C++. I don't use C.
    When writing a small app or maybe only a single utility function I try to stay independent of the CRT and use HeapAlloc.
    When I have to allocate really a lot memory (say, more than 1 MB) I use VirtualAlloc unless I really go for portable code.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    If Im writing a small utility, Ill end up writing C, mostly because In the end, Im going to want to use it in Lunix, so I keep the code as standard as possible, in a console.

    Z.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I'm writing them in C++, but still adhering to the standard and not using any features that might not work in not fully compliant libraries.

    I used to write small GUI utilities with dialog-based MFC, but in the future I'll write them in C#.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Originally posted by CornedBee
    I'm writing them in C++, but still adhering to the standard and not using any features that might not work in not fully compliant libraries.

    I used to write small GUI utilities with dialog-based MFC, but in the future I'll write them in C#.
    GUIs take too long to write, so ill either do it in VB, or make it console. My linux computers default to run in console mode, and I cant even get to my server except through telnet, so I guess that explains it =).

    Z.

  8. #8
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Is that VirtualAlloc is using the machine virtual memory? rather than the physical memory?

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No, not that. Virtual refers to the fact that you can reserve an address range using VirtualAlloc without actually mapping the address range to physical memory (RAM or virtual memory). This can have large advantages where you have pointer arithmetics over a large area of pointers (common example is a spreadsheet app with a theoretical array of ~10000*~500 cells) where many of the actual pointers remain unused (most cells are usually empty).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Originally posted by Zaei
    GUIs take too long to write, so ill either do it in VB, or make it console. My linux computers default to run in console mode, and I cant even get to my server except through telnet, so I guess that explains it =).

    Z.
    I can't stand VB, so I used MFC dialogs and now C#. Just as quick (especially C#) and more to my style.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width