Results 1 to 14 of 14

Thread: Quick question on LocalAlloc & new operator

  1. #1

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Quick question on LocalAlloc & new operator

    Hi,

    Juz have a quick question on the following operator:

    New
    Does the new and delete must be within the same function?

    For instance,

    PHP Code:
    char  *lpszBuffer=NULL;

    void Function1 (void)
    {
       
    lpszBuffer = new char[128];
    }

    void Function2 (void)
    {
       
    delete [] lpszBuffer;
    }

    // Or must be in this...

    void Function1 (void)
    {
        
    lpszBuffer =  new char[128];
        :
        :
        
    delete [] lpszBuffer;


    LocalAlloc
    Does the we need to cast the pointer when we call the LocalFree?

    For instance,

    PHP Code:
    void Function1 (void)
    {
       
    LPSTR lpszBuffer = (LPSTR)LocalAlloc(LPTR128);
       :
       :
       :
       
    LocalFree (lpszBuffer);
    }

    // Or this way...
    void Function1 (void)
    {
       
    LPSTR lpszBuffer = (LPSTR)LocalAlloc(LPTR128);
       :
       :
       :
       
    LocalFree ((LPSTR)lpszBuffer);

    regards,
    Chris.C

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    new and delete - No, they don't. But be very careful if they're not; the problems associated with these are usually to do with forgetting what you've allocated.

    There are frequently better ways to do it, but if you MUST use a pointer, check out the standard library auto_ptr<> template.

    LocalAlloc / LocalFree - Deprecated, use HeapAlloc and HeapFree. I think C++ supports automatic conversion of any pointer type to void*, but not the other way round (which C does, but is very non-type-safe).

    I don't think polymorphism works through HeapAlloc so you probably won't have any pointer conversion issues.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Note that of all memory allocation functions ONLY new calls class constructors. So when you allocate memory for class objects, ALWAYS use new.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    ...unless you use placement new, in which case you can use HeapAlloc/HeapFree (but don't try it just yet )
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Right, but you still need the keyword new somewhere.
    But a non-overloaded new results in a call to malloc anyway, and malloc in Win32 CRTs is implemented using HeapAlloc (on a seperate heap). Placement new is nice when you want to place a lot of objects in virtual memory (VirtualAlloc).

    You can also overload new for your classes, which enables you to use a more direct route to HeapAlloc.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Yeah, it's new that calls the constructors. Although.....I wonder whether you can do away with it and call the function separately...hmmm....
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Interesting question, but how do you explicitly call the constructor of an existing object?

    Of course, if you KNOW that the constructor is just {}, you can simply omit calling it, but that is very bad practice...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    class thingie {
    public:
    	thingie(int x) : m_x(x) { }
    
    	inline int x() const { return m_x; }
    
    private:
    	int m_x;
    };
    
    int main() {
    	thingie blah(5);
    	cout << "blah: " << blah.x() << endl;
    
    	thingie *other = new thingie(10);
    	cout << "other: " << other->x() << endl;
    	delete other;
    
    	void *rawptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(thingie));
    	thingie *ptr = reinterpret_cast<thingie*>(rawptr);
    	ptr->thingie::thingie(20);
    	cout << "ptr: " << ptr->x() << endl;
    	ptr->~thingie();
    	HeapFree(GetProcessHeap(), 0, ptr);
    
    	return 0;
    }
    ...works with VC7

    The ptr->thingie::thingie(20) bit only worked like that, I couldn't do ptr->thingie(20).

    If you omit the constructor call then it just prints 0 for that, after the HEAP_ZERO_MEMORY flag. Interestingly, there's no definition for the destructor *smirk*
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What happens if you make m_x const?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Same happens.
    Code:
    class thingie {
    public:
    	thingie(int x) : m_x(x) {
    		cout << "thingie constructor!\n";
    	}
    
    	inline const int x() const { return m_x; }
    
    private:
    	const int m_x;
    };
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11
    jim mcnamara
    Guest
    Try ptr[20]->x

  12. #12

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Originally posted by CornedBee
    Right, but you still need the keyword new somewhere.
    But a non-overloaded new results in a call to malloc anyway, and malloc in Win32 CRTs is implemented using HeapAlloc (on a seperate heap). Placement new is nice when you want to place a lot of objects in virtual memory (VirtualAlloc).

    You can also overload new for your classes, which enables you to use a more direct route to HeapAlloc.
    First , thank both of you to share your knowledge with me. But I still have some doubt... hope you can explain more, so I can wrote my C/C++ more efficient

    - Is that a good pratice to call the malloc?
    - But i use 'new' to local memory for a string, does it still valid for Note that of all memory allocation functions ONLY new calls class constructors. So when you allocate memory for class objects, ALWAYS use new. confused at this point.
    - Since in WIN32 CRT, malloc is implement using HeapAlloc, so is that good to use HeapAlloc (since, it too dangerous to do it) or stick to malloc?


    Also, I have wrote a program (Windows CE) which implicit link to 12 DLLs, after I ran it for 15-30 minutesm, I notices that the program memory reduce 1-2MB and the virtual memory also reduce 1-2MB.

    My doubt is, since I did not call any VirtualAlloc, why the virtual memory will used up after 15-30 minutes? Which, i only call the new/delete and LocalAlloc/LocalFree.

    Also, I have lot thread in either in the DLLs or main program and they are call in the following method. Will this cause any memory leak as mention?

    PHP Code:

    int MyFunction 
    (void)
    {
       
    HANDLE hThread=NULL;
       
    DWORD dwtid=0;
       
    TCHAR   *lpszString=NULL;

       
    lpszString = (TCHAR*)LocalAlloc(LPTR32);
       
    _tcscpy(lpszStringTEXT("1234567890123456789012345678901"));
       
    hThread CreateThread(NULL0MyThreadmydata0, &dwtid);
       
    Sleep(5000);
       
    CloseHandle(hThread);

       
    LocalFree(lpszString);
       return 
    0;
    }


    DWORD WINAPI MyThread (LPVOID pParam)
    {
       
    TCHAR *lpszBuffer = (TCHAR*)pParam;

       
    // Do all others coding here with reference to the pass in data.

       
    ExitThread(WM_QUIT);
       return 
    0;


    Thanks,

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    a) In C++ malloc shouldn't be used anymore. You can always use new instead and it will save you the trouble of calculating bytes and casting pointers.

    b) Simple data types don't have constructors (at least not real constructors), but you can still use new for them. But if you have a little class:
    class small {
    int m_i;
    pulic:
    small(int i) : m_i(i) {};
    };
    then you should always use new so the constructor is called which in this case is the only valid way of creating a class object.

    c) Why is HeapAlloc dangerous? It isn't any more dangerous than malloc. Use new.

    d) That code piece is dangerous. I suppose the "Sleep(5000)" is there to ensure the created thread has ended. You shouldn't do that, because with threads, you NEVER can be sure what happened. You should use "WaitForSingleObject(hThread, IFINITE);" instead, which will only return when the thread has ended. This ensures that you don't free the memory the thread might currently be using.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    HeapAlloc is ok if you're trying to minimise file size (you know how much I'm a Nazi on this sometimes), but for classes I always use new.

    I use HeapAlloc / malloc sometimes, but mostly I just use new[] on an array of unsigned chars if I want a buffer of some size.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width