在x86程序esp
集中,堆栈指针ebp
是堆指针吗?为什么我们有两个数据结构而不是一个?
什么esi
/ edi
代表什么?
目前在弄清楚为什么这个函数不能通过函数完全初始化main中的指针时遇到一些麻烦.这是我正在尝试做的一个例子.
#include <iostream>
void stuff(int * p)
{
p = new int;
}
int main()
{
int * p;
stuff(p);
*p = 1;
std::cout << *p << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
基本上我试图让它成为一个函数使整数指针访问堆.在那之后,我用间接运算符和中提琴打印出来,我有一个输出.什么不起作用是p不是初始化.我究竟如何使用函数初始化指向堆的指针?
我有一个关于内存管理和全局变量与堆的问题,以及如何决定是否使用从堆分配空间而不是全局变量的变量。
我了解使用堆分配的变量会new
在程序的整个生命周期内持续使用,而全局变量也会在程序的生命周期内持续使用。
应该使用堆变量而不是全局变量吗?
以下面这两种方法为例,就代码速度和内存管理而言,这是更合适的,为什么该方法更合适:
#include <iostream>
int x = 5;
int main(int argc, char** argv)
{
// do stuff with the variable x
return 0;
}
Run Code Online (Sandbox Code Playgroud)
与
#include <iostream>
int main(int argc, char** argv)
{
int x = new int;
*x = 5;
// do stuff with the variable pointed to by x
delete x;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 为什么我的for循环增量不?整数“ i”不会以某种方式增加,这会导致要打印出数组的第一个位置。
我执行以下操作:
int* values = new int[10] {};
int& half(int* arr[]){
//Here, the "i" doesn't increment
for(int i = 0; i < 10; i++){
return *arr[i];
}
}
int main(){
int& a = half(&values);
std::cout << a << std::endl;
delete[] values;
return 0;
}
Run Code Online (Sandbox Code Playgroud) void* heap = malloc(100);
char *c = heap;
strcpy(c, "Terence");
printf("heap = %s\n", heap);
free(heap);
heap = malloc(100);
printf("heap = %s\n", heap);
Run Code Online (Sandbox Code Playgroud)
输出是:
heap = Terence
heap =
Run Code Online (Sandbox Code Playgroud)
这就是我的期望,但现在我有一个更复杂的代码,结构与上面类似,但输出如下:
heap = "Terence"
heap = " ren "
Run Code Online (Sandbox Code Playgroud)
类似的东西.
堆似乎还没有被清理干净?
有办法解决吗?
这是我的代码,我不明白为什么我得到运行时检查失败#2 - 变量'tempSign'周围的堆栈已损坏.我相信错误来自尝试在char*tempSign [MAX]中交换2个值.有人可以解释为什么我得到这个错误,并帮助我解决这个问题谢谢.
void constructSet(ZodiacSign *& z,int size)
{
/*ZodiacSign is a char *
This is how z was created from the previous function and
passed by reference
ZodiacSign * z;
z=new char* [num];
for (int i=0;i<num;i++)
{
z[i]=new char [MAXSTR];
} */
ZodiacSign tempSign [MAX]={"aquarius","pisces","aries","taurus","gemini","cancer","leo",
"vergo","libra","scorpio","sagittarius","capricorn"};
for (int i=0; i<size;i++)
{
int x=12;
int num=(rand()%x);
char * ptr=tempSign[num];
strcpy(z[i],ptr);
swap(num,x,tempSign);
x--;
}
}
void swap(int num,int x,ZodiacSign tempSign [MAX])
{
ZodiacSign temp;
temp=tempSign[num];
tempSign[num]=tempSign[x-1];
tempSign[x]=temp;
}
Run Code Online (Sandbox Code Playgroud) 我昨天进行了一些性能测试,看看堆栈和堆分配在实践中有多大差异.人们对这种测试的期望是堆分配略慢或与堆栈分配相同.然而,我惊讶地发现了相反的情况.我无法解释为什么,以及它在逻辑上是如何可能的,但堆分配总是稍微快一点(我正在编译优化OFF).
这是一个示例输出:
ticks (stack): 42698
ticks (stack): 43977
ticks (stack): 44024
ticks (stack): 44070
ticks (stack): 45038
ticks (heap): 42588
ticks (heap): 43525
ticks (heap): 43633
ticks (heap): 43681
ticks (heap): 43071
Run Code Online (Sandbox Code Playgroud)
这是一个很小的差异但它非常一致,它在100%的时间内重现了堆分配.
谁能解释为什么我会得到这些奇怪的结果?
这是我运行的代码:
#include <vector>
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
struct JJ
{
int c[50];
JJ(int i) { c[5] = 3; c[29] = 4; c[30] = i; c[49] = c[5]; }
};
void fill_direct_stack()
{
vector<JJ> vec;
for (int i=0; i<1000; ++i)
vec.push_back(i);
} …
Run Code Online (Sandbox Code Playgroud) 我需要class
在一个线程应用程序中使用它,所以在堆栈上实例化它显然是一个问题,有没有办法强制new
在实例化类时使用?
我已经将构造函数private
和new
运算符公之于众
public:
void *operator new(size_t);
private:
SomeClass(void);
SomeClass(SomeType value);
.
.
.
Run Code Online (Sandbox Code Playgroud)
但正如我预期的那样使用
SomeClass *heapInstance = new SomeClass(value);
Run Code Online (Sandbox Code Playgroud)
编译器告诉我构造函数是私有的.
问题:
注意:我已经使用class
了这个地方,现在我需要修复我的代码,即我需要编译器来防止编译所以我不需要手动搜索每个事件,我不是很擅长c ++我只是不得不使用它,因为我需要做一个GUI跨平台应用程序,并选择Qt
了几个与此无关的原因.
我试图从文件到向量获得大约3百万个字符串.我像这样分配了它
vector<string> *slownikds = new vector<string>;
Run Code Online (Sandbox Code Playgroud)
当我尝试从这样的文件中推回时:
string *line;
while (getline(slownik, *line))
{
*slownikds->push_back(*line);
}
Run Code Online (Sandbox Code Playgroud)
它不起作用.如何从文件"slownik"中推回字符串?
1)为什么Go的标准库堆没有MaxHeap的实现,而且看起来只支持MinHeap?
2)在上面的实现中,哪个function
负责Heapify()
?Heapify()
将普通数组转换为O(n)
.
代码中的错误
int main()
{
void *ptr = 0;
int overrun = 1;
ptr = malloc(overrun);
while(overrun++)
{
if(!ptr)
while(1) Sleep(500);
*((char*)ptr + (overrun+1)) = 'a';
printf("\n%d\n",overrun);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)从visual studio 2010的项目菜单确保构建为"Release"和"x64"(机器为x64)
启用FULL PAGE HEAP
gflags /p /enable test.exe /full
Run Code Online (Sandbox Code Playgroud)制作windbg默认调试器
E:\installed\Debugging Tools for Windows (x64)>windbg -I
Run Code Online (Sandbox Code Playgroud)在cmd
没有调试器的情况下将代码作为单独的exe运行
输出:
2
3
4
5
6
7
8
9
10
11
12
13
14
Run Code Online (Sandbox Code Playgroud)
之后看到windbg抓住腐败.而且我认为整页堆可以立即捕获损坏.
有关为什么整页堆糟透了的任何评论?
我在一些c#代码中写了一行代码:
new int[] myStore;
Run Code Online (Sandbox Code Playgroud)
我不知道这意味着什么,但我如何将其转换为有效的C++?