我有一个反向排序的堆。我正在尝试构建最大堆:
我有代码:
int main(int argc, char *argv[])
{
int heapArray[] = {0, 1, 2, 3, 4, 5, 6 , 7, 8 ,9 ,10 , 11, 12, 13 ,14 ,15};
int n = sizeof(heapArray)/sizeof(int);
printTree(heapArray, n);
buildHeap(heapArray, n);
printTree(heapArray, n);
}
void buildHeap(int array[], int n)
{
printf("buildHeap\n");
int i = (n-1)/2;
while(i > 0) heapify(array, n, i--);
}
void heapify(int array[], int n, int i)
{
printf("heapify [%i] = %i\n", i, array[i]);
int childLeft = 0, childRight = 0;
int …Run Code Online (Sandbox Code Playgroud) 需要知道是否会在堆栈中或堆上创建这样的3d矩阵,如果它在堆栈上如何新建它并正确初始化默认值(memset)
class Matrix {
protected:
int n[9000][420]; // is stack or heap if VVV is pointer?
};
void main()
{
Matrix* t = new Matrix(); // created on heap
}
Run Code Online (Sandbox Code Playgroud) 使用c:
char ptr[n];
free(ptr);
Run Code Online (Sandbox Code Playgroud)
在我看来:当"char ptr [n];" 使用,分配内存,ptr指向它,free(ptr)应该工作.程序失败了,为什么?(n == 5例如)任何深入分析?
为什么下面的代码直接显示Mb占用的堆:
int Mb = 0;
while ( malloc(1<<20)) ++Mb;
printf("Allocated %d Mb total\n", Mb);
Run Code Online (Sandbox Code Playgroud)
是什么意思1<<20?
Free()知道要释放多少字节的内存但可以删除[]做同样的事情?如果我们从堆栈而不是堆分配,它们是否可以使用free()和delete []完美地工作?最后一个问题:我们需要在结尾分配NULL吗?
#include <stdio.h>
#include <stdlib.h>
char * malloc2()
{
char * m = (char *)malloc(100);
//so malloc(10000000) cannot cause stack-overflow?
//cast from void * to char *
return m;
}
char * malloc3()
{
static char m[100];
//can [1000000] cause stack overflow?
return m;
}
char * newX()
{
char * output = new char[100];
return output;
}
int main(){
char * p = malloc2();
//sizeof(p) gives 8 because this is on 64 bit OS/CPU
free(p);
//free() knows the …Run Code Online (Sandbox Code Playgroud) int main()
{
char *p = new char[100];
strcpy(p, "Test");
cout << "Before heap corruption: " << p << endl;
p[150] = '\0';
cout << "after heap corruption: " << p;
delete p;
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在上面提到的代码中,我在一个不是我的内存位置写'\ 0',即使这样也没有抛出异常.如果使用注释delete p运行上面的代码,则不会抛出任何异常.但是如果它被取消注释,则抛出附加的异常.因此,删除可验证内存所有权.那么,我是否可以知道eaxctly删除是如何工作的以及为什么在写出内存块时有很多验证

我反对我的指针向量问题...我知道问题可能是什么:
当我创建一个指向向量的指针时,指针会保留堆上向量的大小.所以这基本上意味着,指针现在指向向量的内存而没有任何内部......当我现在调整大小或推回向量时,指针现在仍然指向向量的整个内存或只有内存刚开始分配?
我也想知道,如果你有一些技巧可以解决这个问题(如果我认为是真的)."vector.reserve(n)"是一种实现此目的的方法吗?或者有什么我可以做的事情来覆盖指针内存地址,它被初始化后的向量?
free在堆上分配内存后,这两种调用变量之间是否存在差异:
// variant 1
int* p1 = (int*) malloc(sizeof(int)*4);
free(p1);
//variant 2
int* p2 = (int*) malloc(sizeof(int)*4);
free(*p2);
*p2 = NULL;
Run Code Online (Sandbox Code Playgroud) 我对内存泄漏非常谨慎,所以我认为我已经验证了这一点.在下面的例子中会出现内存泄漏吗?我的直觉是肯定的.
class Handler // Class definition
{public:
~Handler();
int* ptrToInts;
};
Handler::~Handler() // Class destructor
{
delete[] ptrToInts;
}
Handler handler; // Global object
void aFunction()
{
handler.ptrToInts = new int[20];
}
int main()
{
bool quit = false;
while(!quit)
{
aFunction();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在heapeach时间,ptrToInts会在单独的内存中创建20个单独的新int?
另外,另一个问题是,如果不是析构函数,是否会释放动态分配的内存?看到类的生命周期是程序的持续时间,它会清除所有"新"内存吗?
编辑:谢谢你的回答.我问这个的原因是因为我试图绕过调用new并删除WadProc每次基本上调用Raw Input,这就是MSDN告诉你这样做的方式.似乎非常低效.
我创建了一个无法通过GC收集的内存分配库.(https://github.com/10sa/Unmanaged-Memory)
该库分配的堆区域基本上是通过使用WinAPI GetProcessHeap()函数获得的.您还可以创建堆区域并将其分配给它.但是,用于创建堆区域的函数是HeapCreate函数.
问题是,
1.此内存区域(GetProcessHeap())是否由GC管理?
2.如果使用HeapCreate函数创建新的堆区域,GC可以收集生成的堆区域吗?
3.如果上述所有问题都成立,如何在不使用Global Heap的情况下在C#中创建内存区域?