PDA

Click to See Complete Forum and Search --> : Dynamic Memory Allocation


made_of_asp
Jan 23rd, 2003, 12:32 AM
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 :confused:

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

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

Thanks for any help :)

Zaei
Jan 23rd, 2003, 12:54 AM
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.

made_of_asp
Jan 23rd, 2003, 01:00 AM
Thanks Z.

CornedBee
Jan 23rd, 2003, 04:21 AM
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.

Zaei
Jan 23rd, 2003, 11:43 AM
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.

CornedBee
Jan 23rd, 2003, 02:14 PM
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#.

Zaei
Jan 23rd, 2003, 02:23 PM
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.

Chris
Jan 23rd, 2003, 08:33 PM
Is that VirtualAlloc is using the machine virtual memory? rather than the physical memory?

CornedBee
Jan 23rd, 2003, 09:32 PM
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).

CornedBee
Jan 23rd, 2003, 09:34 PM
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.