关于LocalAlloc的问题(回答给高分!!)

jgf_student 2004-11-25 09:04:41
在VC6.0的MFC appwizard环境中,调用LocalAlloc()函数时,运行时总通不过,调试时报错如下:
"0x10218253"指令引用的"0x00000000"内存.该内存不能为"read".
本人百思不得其解,请高手指教!
...全文
360 点赞 收藏 12
写回复
12 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
caicaiWu 2004-11-30
lpBuf1 = (LPBYTE)LocalAlloc(LPTR, nSize);
回复
FlyYang 2004-11-30
GolbalAlloc 和 LocalAlloc 都不提倡使用
他们的速度都比较慢,而其功能较少
你看看MSDN吧
微软建议用heap functions
回复
kilnut 2004-11-27

没用过
我都用GloablAlloc和GlobalReAlloc
回复
qrlvls 2004-11-26

Platform SDK: Memory Management
Memory Management Functions

The following functions are used in memory management.

Function Description
CopyMemory Copies a block of memory from one location to another.
CreateMemoryResourceNotification Creates a memory resource notification object.
FillMemory Fills a block of memory with a specified value.
GetLargePageMinimum Retrieves the minimum size of a large page.
GetWriteWatch Retrieves the addresses of the pages that have been written to in a region of virtual memory.
GlobalMemoryStatus Obtains information about the system's current usage of both physical and virtual memory.
GlobalMemoryStatusEx Obtains information about the system's current usage of both physical and virtual memory.
IsBadCodePtr Determines whether the calling process has read access to the memory at the specified address.
IsBadReadPtr Verifies that the calling process has read access to the specified range of memory.
IsBadStringPtr Verifies that the calling process has read access to a range of memory pointed to by a string pointer.
IsBadWritePtr Verifies that the calling process has write access to the specified range of memory.
MoveMemory Moves a block of memory from one location to another.
QueryMemoryResourceNotification Retrieves the state of the specified memory resource object.
ResetWriteWatch Resets the write-tracking state for a region of virtual memory.
SecureZeroMemory Fills a block of memory with zeros.
ZeroMemory Fills a block of memory with zeros.


The following are the AWE functions.

Function Description
AllocateUserPhysicalPages Allocates physical memory pages to be mapped and unmapped within any AWE region of the process.
FreeUserPhysicalPages Frees physical memory pages previously allocated with AllocateUserPhysicalPages.
MapUserPhysicalPages Maps previously allocated physical memory pages at the specified address within an AWE region.
MapUserPhysicalPagesScatter Maps previously allocated physical memory pages at the specified address within an AWE region.

The following are the global and local functions. These functions are slower than other memory management functions and do not provide as many features. Therefore, new applications should use the heap functions. However, the global functions are still used with DDE and the clipboard functions.

Function Description
GlobalAlloc Allocates the specified number of bytes from the heap.
GlobalDiscard Discards the specified global memory block.
GlobalFlags Returns information about the specified global memory object.
GlobalFree Frees the specified global memory object.
GlobalHandle Retrieves the handle associated with the specified pointer to a global memory block.
GlobalLock Locks a global memory object and returns a pointer to the first byte of the object's memory block.
GlobalReAlloc Changes the size or attributes of a specified global memory object.
GlobalSize Retrieves the current size of the specified global memory object.
GlobalUnlock Decrements the lock count associated with a memory object.
LocalAlloc Allocates the specified number of bytes from the heap.
LocalDiscard Discards the specified local memory object.
LocalFlags Returns information about the specified local memory object.
LocalFree Frees the specified local memory object.
LocalHandle Retrieves the handle associated with the specified pointer to a local memory object.
LocalLock Locks a local memory object and returns a pointer to the first byte of the object's memory block.
LocalReAlloc Changes the size or the attributes of a specified local memory object.
LocalSize Returns the current size of the specified local memory object.
LocalUnlock Decrements the lock count associated with a memory object.


The following are the heap functions.

Function Description
GetProcessHeap Obtains a handle to the heap of the calling process.
GetProcessHeaps Obtains handles to all of the heaps that are valid for the calling process.
HeapAlloc Allocates a block of memory from a heap.
HeapCompact Attempts to compact a specified heap.
HeapCreate Creates a heap object.
HeapDestroy Destroys the specified heap object.
HeapFree Frees a memory block allocated from a heap.
HeapLock Attempts to acquire the lock associated with a specified heap.
HeapQueryInformation Retrieves information about the specified heap.
HeapReAlloc Reallocates a block of memory from a heap.
HeapSetInformation Sets heap information for the specified heap.
HeapSize Retrieves the size of a memory block allocated from a heap.
HeapUnlock Releases ownership of the lock associated with a specified heap.
HeapValidate Attempts to validate a specified heap.
HeapWalk Enumerates the memory blocks in a specified heap.

The following are the virtual memory functions.

Function Description
VirtualAlloc Reserves or commits a region of pages in the virtual address space of the calling process.
VirtualAllocEx Reserves or commits a region of pages in the virtual address space of the specified process.
VirtualFree Releases or decommits a region of pages within the virtual address space of the calling process.
VirtualFreeEx Releases or decommits a region of memory within the virtual address space of a specified process.
VirtualLock Locks the specified region of the process's virtual address space into physical memory.
VirtualProtect Changes the access protection on a region of committed pages in the virtual address space of the calling process.
VirtualProtectEx Changes the access protection on a region of committed pages in the virtual address space of the calling process.
VirtualQuery Provides information about a range of pages in the virtual address space of the calling process.
VirtualQueryEx Provides information about a range of pages in the virtual address space of the calling process.
VirtualUnlock Unlocks a specified range of pages in the virtual address space of a process.


Platform SDK Release: February 2003 What did you think of this topic?
Order a Platform SDK CD


回复
qrlvls 2004-11-26
建议仔细阅读MSDN中有关内存分配的内容,对各种内存分配有详述
回复
roger_ding 2004-11-26
Note The local functions are slower than other memory management functions and do not provide as many features. Therefore, new applications should use the heap functions.

所以用HeapAlloc替换LocalAlloc试试
回复
kingzai 2004-11-26
用GolbalAlloc替换LocalAlloc试一下呢?
回复
jgf_student 2004-11-26
代码如下,请各位高手不吝指教,本人不胜感激!!!!
//声明变量
int nSize;
LPBYTE lpBuf1, lpBuf2, lpBufOut;
HFILE hFile1, hFile2, hFileOut;
LPBITMAPINFO lpInfo;
HDC hDC;
int i;

//打开文件(bmp格式的图片)
hFile1 = _lopen("..//BMP//1.bmp",OF_READ);
hFile2 = _lopen("..//BMP//2.bmp",OF_READ);
hFileOut =_lcreat("..//BMP//out.bmp",0); //添加一个新文件

nSize = GetFileSize((HANDLE) hFile1, NULL); //读取hFile1(bmp图片)文件的大小
lpBuf1 = LocalAlloc(LPTR, nSize); //申请nSize大小的文件,就是在此处出错,申请失败,不知是何原因?
lpBuf2 = LocalAlloc(LPTR, nSize);
lpBufOut = LocalAlloc(LPTR, nSize);

_lread(hFile1, lpBuf1, nSize);
_lread(hFile2, lpBuf2, nSize);

_lclose(hFile1);
_lclose(hFile2);

memcpy(lpBufOut, lpBuf1, nSize);
回复
hundlom 2004-11-25
"0x10218253"指令引用的"0x00000000"内存错误可能是引用错误,检查指针变量和其他变量是否初始化
回复
kingzai 2004-11-25
replace LocalAlloc with GolbalAlloc
回复
pleasehelpme 2004-11-25
gz
回复
qrlvls 2004-11-25
那一部分的代码是怎样的
回复
相关推荐
发帖
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2004-11-25 09:04
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……