Thread: Heap File Allocation:?

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    7

    Heap File Allocation:?

    Im following some examples of opening dialog for a builder of hexedit files. Here the code I dont know why the HeapAlloc is used!

    Code:
    void* locate(size_t size){
    	return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
    }
    Here the OpenFIleDialog:

    Code:
            WCHAR curdir[MAX_PATH];
    	WCHAR *filepath = (WCHAR*)locate(MAX_PATH * sizeof(WCHAR));
    	GetCurrentDirectoryW(sizeof(curdir), curdir);
    	OPENFILENAMEW ofn;
    And in the part of saving:

    Code:
    DWORD fileSize = GetFileSize(hFile, NULL), read;BYTE* pByte = (BYTE*)locate(fileSize);
    ReadFile(hFile, pByte, fileSize, &read, 0);
    Thx Again guys!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,514
    Quote Originally Posted by Benzina
    Here the code I dont know why the HeapAlloc is used!
    I would expect it to be used when dynamic memory is required or at least convenient, or when stack memory is expected to be insufficient. So examine the code and see whether these apply.

    By the way, did you retype the code? locate sounds like an odd name for the function; perhaps it was meant to be named allocate.

    Quote Originally Posted by Benzina
    Here the OpenFIleDialog:
    There's insufficient information to tell you why locate was used, and hence to tell you why HeapAlloc was used: locate was used to allocate memory for filepath, but the usage of filepath was not shown.

    Quote Originally Posted by Benzina
    And in the part of saving:
    Here it makes sense: you want to allocate memory corresponding to fileSize in order to read the corresponding number of bytes. fileSize is of course can vary at runtime, hence HeapAlloc is appropriate since you thus need dynamic memory allocation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2019
    Posts
    7
    Quote Originally Posted by laserlight View Post
    By the way, did you retype the code? locate sounds like an odd name for the function; perhaps it was meant to be named allocate.
    It isnt my code.

    Quote Originally Posted by laserlight View Post
    There's insufficient information to tell you why locate was used, and hence to tell you why HeapAlloc was used: locate was used to allocate memory for filepath, but the usage of filepath was not shown.
    Here you have mroe code. Its the file opening. But I dont know why it allocates in the Heap?

    Code:
    WCHAR curdir[MAX_PATH];
    	WCHAR *filepath = (WCHAR*)locate(MAX_PATH * sizeof(WCHAR));
    	GetCurrentDirectoryW(sizeof(curdir), curdir);
    	OPENFILENAMEW ofn;
    	memset(&ofn, 0, sizeof(OPENFILENAME));
    	ofn.lpstrInitialDir = curdir;
    	ofn.lStructSize = sizeof(ofn);
    	ofn.lpstrFile = filepath;
    	*(ofn.lpstrFile) = 0;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,514
    Quote Originally Posted by Benzina
    It isnt my code.
    Of course. Whose code is it?

    Quote Originally Posted by Benzina
    But I dont know why it allocates in the Heap?
    Probably because it must outlive the scope of the function. I'm not familiar with this API, but in modern C++ we are unlikely to do things this way: we would be making use of RAII instead.
    Last edited by laserlight; 01-23-2020 at 11:29 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2019
    Posts
    7
    Its an old Russian Crypter based on RC4.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MAX memory allocation from c runtime heap
    By sandylovesgnr in forum C Programming
    Replies: 2
    Last Post: 10-23-2008, 11:02 PM
  2. memory allocation from stack and heap ?
    By Bargi in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2008, 12:37 AM
  3. stack vs heap memory allocation
    By gongchengshi in forum C Programming
    Replies: 9
    Last Post: 11-18-2007, 12:17 PM
  4. about heap allocation
    By sawer in forum C++ Programming
    Replies: 5
    Last Post: 04-15-2006, 11:09 AM
  5. Determining Heap Allocation
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-13-2002, 05:48 PM

Tags for this Thread