Thread: Memory allocation

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Memory allocation

    I need to use a char * buf which size augment gradually.
    The way I do it is not working I guess that what crashes the program.
    Do I need to Free it everytime or can I just Allocate a new size without Freeing it before?

    Code:
      // global
      char * buf;
     while(1)
     {
            buf  = (char*) GlobalAlloc(GMEM_FIXED, newsize);
               
            FunctionThatUses_buf();
    
            GlobalFree(buf);
      
            Sleep(1000 * 30);
      }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What do you mean 'uses'?

    GlobalAlloc function (Windows)
    GlobalAlloc returns a HANDLE, not a pointer.
    Did you get a warning about this before throwing in a cast to make the compiler shut up?

    Also,
    New applications should use the heap functions unless documentation states that a global function should be used.
    What's wrong with using new and delete?
    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
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Ouh, thanks for clearing that up.

    It uses the char * buf to write text to a file.
    Yes I got a compile error cannot convert from 'HGLOBAL' to 'char *' but I thought it was normal with C++ compiler.

    I thought "new" calls GlobalAlloc() thats why I used it. So than what's GlobalAlloc() is for?
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Althought I think new does use GlobalAlloc it is best to use new b/c it does far more than just call that method. Get into the habit of using new and delete.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Ducky View Post
    So than what's GlobalAlloc() is for?
    GlobalAlloc() exists so that old programs that use it can keep working.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It is also a good idea for an Operating System to have something like GlobalAlloc since memory allocation falls under its array of tasks.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    GlobalAlloc is a legacy, windows specific OS API call that provides slightly more fine control over allocation than new, at the cost of portability. Don't use it.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why are you using all these Win32 functions in the first place? What's wrong with the C++ standard library?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Well I read a lot of stuff and it confuses me sometimes.
    I read somewhere that C++ functions call the lower level Win32 API functions so then why not just call them directly.
    Using Windows 10 with Code Blocks and MingW.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    All library functions are just a bunch of assembly instructions. Why not just write in assembly directly instead?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Now that would be pushing it, wouldn't it? ;-)
    Using Windows 10 with Code Blocks and MingW.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    but it follows your line of thinking. if you're learning C++, learn C++. learn win32 later.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Ducky View Post
    Now that would be pushing it, wouldn't it? ;-)
    I would say the same about what you are doing.
    If you insist on using lower level functions just because higher level functions call them, then you might as well use assembly because all higher level functions are just a bunch of assembly instructions.
    Of course higher level stuff calls lower level stuff. That's how all is built. You build and abstract the lower level stuff. By doing so, you get a better interface that is easier to use.
    That is how C is built - by building on hardware. C++ builds on C. C#, Java, etc, also builds on C and C++.
    If you can use higher level stuff, then do it. Don't lower yourself to the level of lower level stuff. This should go without saying.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory allocation
    By lars-erik in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2011, 08:24 AM
  2. help on memory allocation(?)
    By adameye in forum C Programming
    Replies: 3
    Last Post: 09-02-2009, 02:31 PM
  3. memory allocation
    By cs32 in forum C Programming
    Replies: 6
    Last Post: 02-18-2008, 04:28 PM
  4. Memory allocation
    By kernelio in forum C Programming
    Replies: 6
    Last Post: 10-11-2007, 10:19 AM
  5. Memory allocation
    By Storm in forum C Programming
    Replies: 13
    Last Post: 05-19-2004, 08:47 AM