最近看了Windows核心编程的18章堆栈部分,有些疑惑。请教中ing....

时间:2022-12-15 07:53:49
1、它里面讲的堆栈和c++中的所谓的heap和stack到底有什么关系?
2、它里面讲的HeapAlloc()和c++中所谓的new、delete到底有什么关系?

到google上没找到答案。
:)急盼高手解答。
   谢。!!!

29 个解决方案

#1


1.There is no difference between memory allocated from a private heap and that allocated by using the other memory allocation functions.


2.Memory allocated by HeapAlloc is not movable











The heap functions enable a process to create a private heap, a block of one or more pages in the address space of the calling process. The process can then use a separate set of functions to manage the memory in that heap. There is no difference between memory allocated from a private heap and that allocated by using the other memory allocation functions. For a complete list of functions, see the table in Memory Management Functions.

The HeapCreate function creates a private heap object from which the calling process can allocate memory blocks by using the HeapAlloc function. HeapCreate specifies both an initial size and a maximum size for the heap. The initial size determines the number of committed, read/write pages initially allocated for the heap. The maximum size determines the total number of reserved pages. These pages create a contiguous block in the virtual address space of a process into which the heap can grow. Additional pages are automatically committed from this reserved space if requests by HeapAlloc exceed the current size of committed pages, assuming that the physical storage for it is available. Once the pages are committed, they are not decommitted until the process is terminated or until the heap is destroyed by calling the HeapDestroy function. 

The memory of a private heap object is accessible only to the process that created it. If a dynamic-link library (DLL) creates a private heap, it does so in the address space of the process that called the DLL. It is accessible only to that process. 

The HeapAlloc function allocates a specified number of bytes from a private heap and returns a pointer to the allocated block. The pointer identifies the block for the HeapFree function to release or for the HeapSize function to determine the size. 

Memory allocated by HeapAlloc is not movable. Because the system cannot compact a private heap, the heap can become fragmented. 

A possible use for the heap functions is to create a private heap when a process starts up, specifying an initial size sufficient to satisfy the memory requirements of the process. If the call to the HeapCreate function fails, the process can terminate or notify the user of the memory shortage; if it succeeds, however, the process is assured of having the memory it needs. 

Memory requested by HeapCreate may or may not be contiguous. Memory allocated within a heap by HeapAlloc is contiguous. You should not write to or read from memory in a heap except that allocated by HeapAlloc, nor should you assume any relationship between two areas of memory allocated by HeapAlloc.

You should not refer in any way to memory that has been freed by HeapFree. After the memory is freed, any information that may have been in it is gone forever. If you require information, do not free memory containing the information. Function calls that return information about memory (such as HeapSize) may not be used with freed memory, as they may return bogus data.

Windows NT 4.0: External factors may cause accesses to heap memory to generate access violations. One possible cause of an access violation is very limited space in the paging file. Therefore, all accesses to heap memory should be protected with structured exception handling. This is not necessary on later versions of Windows. 

Windows 95/98/Me: The heap managers are designed for memory blocks smaller than four megabytes. If you expect your memory blocks to be larger than one or two megabytes, you can avoid significant performance degradation by using the VirtualAlloc or VirtualAllocEx function instead.

#2


堆是堆,栈是栈
一定要分开来说

#3


Alloc族函数的作用是分配内存和销毁分配的内存
new、delete不但分配内存,而且new对象的时候能够对对象进行构造,delete对象的时候能够销毁对象,你可以在一个类的  构造/析构 函数中添加语句,然后调用
CMyClass *m = new CMyClass;
就知道了

是否执行 构造/析构 是alloc和new,free和delete的重大区别之一。

#4


heap = 堆
stack = 栈(堆栈)
这本书的翻译不好,建议同时看英文电子书

#5


new比较适合小块内存的分配,使用new主要的目的据说是减少内存的浪费
new比较慢,因为new最终要调用heapalloc,heapalloc需要同步(entercriticalsection)

#6


new可以看成两个动作:1。分配内存(相当于alloc族)2。引发构造函数。

new 是个操作符,和什么"+","-","="...有一样的地位.

 

alloc族是个分配内存的函数,供你调用的.

 

new是保留字,不需要头文件支持.

alloc族需要头文件库函数支持.



new 建立的是一个对象,

alloc族分配的是一块内存.

 

new建立的对象你可以把它当成一个普通的对象,用成员函数访问,不要直接访问它的地址空间

alloc族分配的是一块内存区域,就用指针访问好了,而且还可以在里面移动指针.

#7


还有,new在内存分配失败后可以抛出异常,这对系统稳定性很重要

#8


还有,new不用写太多,alloc还要写sizeof,

new 打起来比alloc省了好几下键盘

#9


还有就是new不能在纯c里边用

#10


我是来学习的。看了 JennyVenus所说,收益不小啊。

#11


:)看了各位的高论,还是有些晕。再顶一下

核心编程中说的堆栈和实际我们new出来的内存是一块区域吗?HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(RecvInfo));这样分配的是默认堆栈的区域。new的是那块区域?我感觉他们是不一样的。

#12


默认堆栈是1M,能new出来的内存有多大?有些晕....

#13


再顶一下

#14


怎么都是有星的啊

#15


再顶一下

#16


malloc 和 new 都是在堆中(一般是进程的默认堆)分配的,除非被重载。但是它们分配的内存块和 HeapAlloc 的不一样,因为 C/C++ 需要维护这些分配的内存块。

在栈中分配可以用 _alloca ,分配的内存只在调用函数内有效,不用 free

#17


再补充:

new/delete new[]/delete[] malloc/free 系列平台无关.在任何支持标准C/C++ 的编译器上都可用

HeapAlloc/HeapFree 仅限 WIN32 平台

#18


JennyVenus() :new比较适合小块内存的分配,使用new主要的目的据说是减少内存的浪费
不太同意

#19


再补充:

事实上 new/delete new[]/delete[] malloc/free 在 microsoft c++ 中的实现就是调用 HeapAlloc/HeapFree , 操纵的是 c-runtime heap

#20


多谢各位的解答,似乎有些明白。单还有一些问题。

  1、默认堆栈可以自动增长吗?
  2、如果我在开始一个新的线程前用HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(RecvInfo));分配内存。在线程退出前HeapFree(GetProcessHeap(),0,m_pRecvInfo);这样如果线程被频繁启动,需要频繁切换时会不会有什么问题?还有,这样和线程启动前用new,退出时用delete,进行内存操作有什么区别?

  再盼各位高手解答。
   谢。!!!

#21


再顶一下就散分了  :)

#22



#23


1. 可以
2. 除了代码兼容性上,HeapAlloc 和 new 分配没有什么大的区别,如  realdreamer 所说,new 实际上也是调用 HeapAlloc 进行分配的。

#24


奥。我最近写的程序中在开始一个新的线程前用HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(RecvInfo));分配内存。在线程退出前HeapFree(GetProcessHeap(),0,m_pRecvInfo);我的程序偶尔会莫名其妙的退出。不出任何提示。不知是不是跟这个有关。

:)多谢各位解答。

#25


gz

#26


gz

#27


ding...

#28


ding

#29


ding...

#1


1.There is no difference between memory allocated from a private heap and that allocated by using the other memory allocation functions.


2.Memory allocated by HeapAlloc is not movable











The heap functions enable a process to create a private heap, a block of one or more pages in the address space of the calling process. The process can then use a separate set of functions to manage the memory in that heap. There is no difference between memory allocated from a private heap and that allocated by using the other memory allocation functions. For a complete list of functions, see the table in Memory Management Functions.

The HeapCreate function creates a private heap object from which the calling process can allocate memory blocks by using the HeapAlloc function. HeapCreate specifies both an initial size and a maximum size for the heap. The initial size determines the number of committed, read/write pages initially allocated for the heap. The maximum size determines the total number of reserved pages. These pages create a contiguous block in the virtual address space of a process into which the heap can grow. Additional pages are automatically committed from this reserved space if requests by HeapAlloc exceed the current size of committed pages, assuming that the physical storage for it is available. Once the pages are committed, they are not decommitted until the process is terminated or until the heap is destroyed by calling the HeapDestroy function. 

The memory of a private heap object is accessible only to the process that created it. If a dynamic-link library (DLL) creates a private heap, it does so in the address space of the process that called the DLL. It is accessible only to that process. 

The HeapAlloc function allocates a specified number of bytes from a private heap and returns a pointer to the allocated block. The pointer identifies the block for the HeapFree function to release or for the HeapSize function to determine the size. 

Memory allocated by HeapAlloc is not movable. Because the system cannot compact a private heap, the heap can become fragmented. 

A possible use for the heap functions is to create a private heap when a process starts up, specifying an initial size sufficient to satisfy the memory requirements of the process. If the call to the HeapCreate function fails, the process can terminate or notify the user of the memory shortage; if it succeeds, however, the process is assured of having the memory it needs. 

Memory requested by HeapCreate may or may not be contiguous. Memory allocated within a heap by HeapAlloc is contiguous. You should not write to or read from memory in a heap except that allocated by HeapAlloc, nor should you assume any relationship between two areas of memory allocated by HeapAlloc.

You should not refer in any way to memory that has been freed by HeapFree. After the memory is freed, any information that may have been in it is gone forever. If you require information, do not free memory containing the information. Function calls that return information about memory (such as HeapSize) may not be used with freed memory, as they may return bogus data.

Windows NT 4.0: External factors may cause accesses to heap memory to generate access violations. One possible cause of an access violation is very limited space in the paging file. Therefore, all accesses to heap memory should be protected with structured exception handling. This is not necessary on later versions of Windows. 

Windows 95/98/Me: The heap managers are designed for memory blocks smaller than four megabytes. If you expect your memory blocks to be larger than one or two megabytes, you can avoid significant performance degradation by using the VirtualAlloc or VirtualAllocEx function instead.

#2


堆是堆,栈是栈
一定要分开来说

#3


Alloc族函数的作用是分配内存和销毁分配的内存
new、delete不但分配内存,而且new对象的时候能够对对象进行构造,delete对象的时候能够销毁对象,你可以在一个类的  构造/析构 函数中添加语句,然后调用
CMyClass *m = new CMyClass;
就知道了

是否执行 构造/析构 是alloc和new,free和delete的重大区别之一。

#4


heap = 堆
stack = 栈(堆栈)
这本书的翻译不好,建议同时看英文电子书

#5


new比较适合小块内存的分配,使用new主要的目的据说是减少内存的浪费
new比较慢,因为new最终要调用heapalloc,heapalloc需要同步(entercriticalsection)

#6


new可以看成两个动作:1。分配内存(相当于alloc族)2。引发构造函数。

new 是个操作符,和什么"+","-","="...有一样的地位.

 

alloc族是个分配内存的函数,供你调用的.

 

new是保留字,不需要头文件支持.

alloc族需要头文件库函数支持.



new 建立的是一个对象,

alloc族分配的是一块内存.

 

new建立的对象你可以把它当成一个普通的对象,用成员函数访问,不要直接访问它的地址空间

alloc族分配的是一块内存区域,就用指针访问好了,而且还可以在里面移动指针.

#7


还有,new在内存分配失败后可以抛出异常,这对系统稳定性很重要

#8


还有,new不用写太多,alloc还要写sizeof,

new 打起来比alloc省了好几下键盘

#9


还有就是new不能在纯c里边用

#10


我是来学习的。看了 JennyVenus所说,收益不小啊。

#11


:)看了各位的高论,还是有些晕。再顶一下

核心编程中说的堆栈和实际我们new出来的内存是一块区域吗?HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(RecvInfo));这样分配的是默认堆栈的区域。new的是那块区域?我感觉他们是不一样的。

#12


默认堆栈是1M,能new出来的内存有多大?有些晕....

#13


再顶一下

#14


怎么都是有星的啊

#15


再顶一下

#16


malloc 和 new 都是在堆中(一般是进程的默认堆)分配的,除非被重载。但是它们分配的内存块和 HeapAlloc 的不一样,因为 C/C++ 需要维护这些分配的内存块。

在栈中分配可以用 _alloca ,分配的内存只在调用函数内有效,不用 free

#17


再补充:

new/delete new[]/delete[] malloc/free 系列平台无关.在任何支持标准C/C++ 的编译器上都可用

HeapAlloc/HeapFree 仅限 WIN32 平台

#18


JennyVenus() :new比较适合小块内存的分配,使用new主要的目的据说是减少内存的浪费
不太同意

#19


再补充:

事实上 new/delete new[]/delete[] malloc/free 在 microsoft c++ 中的实现就是调用 HeapAlloc/HeapFree , 操纵的是 c-runtime heap

#20


多谢各位的解答,似乎有些明白。单还有一些问题。

  1、默认堆栈可以自动增长吗?
  2、如果我在开始一个新的线程前用HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(RecvInfo));分配内存。在线程退出前HeapFree(GetProcessHeap(),0,m_pRecvInfo);这样如果线程被频繁启动,需要频繁切换时会不会有什么问题?还有,这样和线程启动前用new,退出时用delete,进行内存操作有什么区别?

  再盼各位高手解答。
   谢。!!!

#21


再顶一下就散分了  :)

#22



#23


1. 可以
2. 除了代码兼容性上,HeapAlloc 和 new 分配没有什么大的区别,如  realdreamer 所说,new 实际上也是调用 HeapAlloc 进行分配的。

#24


奥。我最近写的程序中在开始一个新的线程前用HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(RecvInfo));分配内存。在线程退出前HeapFree(GetProcessHeap(),0,m_pRecvInfo);我的程序偶尔会莫名其妙的退出。不出任何提示。不知是不是跟这个有关。

:)多谢各位解答。

#25


gz

#26


gz

#27


ding...

#28


ding

#29


ding...