Thread: GlobalAlloc() conversion from void to char*

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Yes, if you are compiling this as C++, then you need to cast your void* returning functions to the appropriate type.

    FileSize = (LPSTR)GlobalAlloc(GHND,Buff);

    But why are you using GlobalAlloc() in preference to the standard C++ 'new' operator for allocating memory, which does not need any casting.
    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.

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by Salem View Post
    But why are you using GlobalAlloc() in preference to the standard C++ 'new' operator for allocating memory, which does not need any casting.
    Mainly because I'm new to Win32/API/windows programming and do not know a lot of functions yet. I've been surfing around tutorials, books, and msdn to find everything so far. I will look in to other ways of allocating memory, thanks.
    Last edited by scwizzo; 06-10-2007 at 12:06 PM.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    ok i tried the new and delete operators, way easier, thank you.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Salem View Post
    But why are you using GlobalAlloc() in preference to the standard C++ 'new' operator for allocating memory, which does not need any casting.
    GlobalAlloc() is part of the API, and hence will support faster API features ( such as block transfers) if the user upgrades their system/os. whereas the functionality of 'new' is fixed at compile time.

    'new' wins on portability
    GlobalAlloc() wins on functionality

    for applications that only target Win32/Win64 systems, it makes sense to use GlobalAlloc();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 04:14 AM
  3. need help with handelling multiple source files
    By DarkMortar in forum C++ Programming
    Replies: 38
    Last Post: 05-26-2006, 10:46 PM
  4. Can't Find Conio.h?
    By drdroid in forum C++ Programming
    Replies: 27
    Last Post: 09-26-2002, 04:24 AM