Thread: Suggestions on this C style code

  1. #1
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310

    Question Suggestions on this C style code

    I want to make a global variable:
    PHP Code:
    char *str
    Let's start by allocating some space:
    PHP Code:
    str GloballAlloc(GPTR10); 
    later pass some string:
    PHP Code:
    lstrcpy(str"123"); 
    let's say, later I want to make a bigger string
    PHP Code:
    lstrcpy(str"somestringthatisbigger"); 
    How can I implement GlobalReAlloc? Is there another way to re-alloc some variable for future smaller or bigger strings?
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I don't understand.... What's wrong with using GlobalReAlloc()? If you use GlobalAlloc() then I think that's pretty much what you're supposed to do to resize it.

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    or why not make it portable and just use malloc?
    (didn't even know we had PHP tags, lol)

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Just use GlobalRealloc with the new size.

    Btw names starting with str, mem and underscores are reserved by the implementation. You're better off choosing a different name like somestr, mystr or something that actually represents what the variable is used for. errorstring, username or something

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Okay... Why use GloballAlloc() over malloc()?

    Also make sure you check the return result of GloballAlloc() (NULL if it failed, sounds more and more like malloc to me)

    I wouldn't be surprised if GlobalAlloc() was just an alias for malloc()...

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    GlobalAlloc() and related functions are Windows API functions.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes, but what's the real difference?

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Here's some info on memory management for Windows:

    http://msdn2.microsoft.com/en-us/library/ms810603.aspx

    Every process in Windows NT has one heap called the default heap. Processes can also have as many other dynamic heaps as they wish, simply by creating and destroying them on the fly. The Win32 subsystem uses the default heap for all global and local memory management functions, and the C run-time library uses the default heap for supporting malloc functions. The heap memory functions, which indicate a specific heap by its handle, use dynamic heaps. The behavior of dynamic heaps is discussed in the "Win32 Heap Memory API" section later in this article.

    The default and dynamic heaps are basically the same thing, but the default heap has the special characteristic of being identifiable as the default. This is how the C run-time library and the Win32 subsystem identify which heap to allocate from. The GetProcessHeap function returns a handle to the default heap for a process. Since functions such as GlobalAlloc or malloc are executed within the context of the thread that called them, they can simply call GetProcessHeap to retrieve a handle to the default heap, and then manage memory accordingly.
    Basically, malloc() and GlobalAlloc() allocate memory from the same location. GlobalAlloc() allows you to do a little bit more. malloc() always returns a pointer to the block of memory you allocate. Since it's a pointer, it doesn't move around. GlobalAlloc(), however, can be requested to return a HANDLE instead of a pointer to the memory block. A HANDLE in this context can be internally managed so the actual memory block can be moved around inside the heap, depending upon various different circumstances, aiding in the memory management process by making it easier to deal with memory fragmentation. This is a little more cumbersome to use than a straight pointer (which it can also return).

    In addition, GlobalAlloc() can zero out the memory, similar to calloc().

    Overall, you can use either malloc() or GlobalAlloc() as long as you know what you're doing. If you're writing a full fledged Windows program, and have little to no concern how this will run on Unix or any Mac OS, then you should probably look into using the Windows API.

    If you're trying to be 100% portable, then definitely use malloc().

    Edit:

    Managing memory in Windows prior to Win32 involved a great deal of uncertainty about using the C run-time (CRT) library. With Win32 there should be little hesitation. The CRT library in Win32 is implemented in a manner similar to FIXED memory allocated via the local and global memory management functions. The CRT library is also implemented using the same default heap manager as the global and local memory management functions.

    Subsequent memory allocations via malloc, GlobalAlloc, and LocalAlloc return pointers to memory allocated from the same heap. The heap manager does not divide its space among the CRT and global/local memory functions, and it does not maintain separate heaps for these functions. Instead, it treats them the same, promoting consistent behavior across the types of functions. As a result, you can now write code using the functions you're most comfortable with. And, if you're interested in portability, you can safely use the CRT functions exclusively for managing heap memory.
    This just reiterates that if you use GlobalAlloc() a certain way (ie. Fixed memory, returning a pointer to it), it will behave just like malloc(). You can use either in this case, but consistency is probably a good thing no matter what you decide.
    Last edited by MacGyver; 05-30-2007 at 04:33 AM.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Very well explained
    Thanks

    Still by the OPs example I don't see any advantage.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Agreed. There is not that much advantage at that point if they both do the same. The OP is using it the same way as malloc() behaves. [Edit] One advantage over using malloc(), is that I believe you can retrieve the size of a block of memory by using GlobalSize() on the pointer/HANDLE. Not necessary, but nice. [/Edit]

    To further squash the party, though, MSDN recommends that GlobalAlloc() not even be used (and hence I would assume that counts for malloc() as well).

    Instead, they recommend using HeapAlloc() and the like. Isn't memory management fun? lol....
    Last edited by MacGyver; 05-30-2007 at 05:56 AM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Back in the days of old, when windows 3.x was nothing more than a DOS wrapper, then GlobalAlloc actually did something useful and different.

    But in a true 32-bit OS with proper VM, it doesn't seem to have a great deal of use.
    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.

  12. #12
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Quote Originally Posted by Salem View Post
    Back in the days of old, when windows 3.x was nothing more than a DOS wrapper, then GlobalAlloc actually did something useful and different.

    But in a true 32-bit OS with proper VM, it doesn't seem to have a great deal of use.
    How about the returning of a HANDLE? isnt that a good use?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Which style of if loops is most common?
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 08-25-2005, 03:18 PM
  2. Code in MLA style papers
    By Thantos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 12-14-2003, 08:08 AM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM