Like Tree12Likes
  • 1 Post By Salem
  • 2 Post By VirtualAce
  • 1 Post By brewbuck
  • 1 Post By whiteflags
  • 2 Post By King Mir
  • 1 Post By Elysia
  • 2 Post By Elkvis
  • 2 Post By Elysia

Memory allocation

This is a discussion on Memory allocation within the C++ Programming forums, part of the General Programming Boards category; I need to use a char * buf which size augment gradually. The way I do it is not working ...

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

    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);
      }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    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?
    Ducky likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    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?
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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.
    Ducky and Elysia like this.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    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.
    Ducky likes this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

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

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,050
    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.
    Elysia and Ducky like this.
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    Why are you using all these Win32 functions in the first place? What's wrong with the C++ standard library?
    Elkvis likes this.
    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
    796
    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    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
    796
    Now that would be pushing it, wouldn't it? ;-)
    Compiler MSVC++ 2013 with Code::Blocks.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    2,501
    but it follows your line of thinking. if you're learning C++, learn C++. learn win32 later.
    Salem and King Mir like this.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    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.
    Ducky and King Mir like this.
    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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21