小编nik*_*ter的帖子

动态内存调整字符数组大小

我正在尝试调整字符数组的大小,我遵循了: 在运行时调整字符 [] 的大小

然后:

我做过这样的事情:

// this crashes in runtime:

const long SIZE_X = 1048576;

char* Buffsz = new char(sizeof(char));

for(int i = 0; i < (SIZE_X - 2); i++)
{
    Buffsz[i] = 'a';
    if(realloc(Buffsz, sizeof(char) * i) == NULL) // autoallocate memory
        cout << "Failled to reallocate memory!" << endl;
}
Run Code Online (Sandbox Code Playgroud)

但如果我这样做:

// this works without problems.
const long SIZE_X = 1048576;

char* ABuffsz = new char[SIZE_X];

for(int i = 0; i < (SIZE_X - 2); i++)
{ …
Run Code Online (Sandbox Code Playgroud)

c++ dynamic-memory-allocation c++14

2
推荐指数
1
解决办法
9461
查看次数

正确转换为指向返回函数的函数的函数指针

我正在翻转源代码,我发现了一个看起来像这样的函数:

考虑一下:

int examplefn(int x) { return x * 4; }

int (*rtx())(int)
{
    return examplefn;
}
Run Code Online (Sandbox Code Playgroud)

那么,我需要做一个指针函数rtx()来做一个钩子,然后我做了这样的事情:

int (*fncptr())(int) = (int(*())(int))0xC0FFEE; 
/* 0xC0FFEE it's a sample of the memory address of the function...*/
Run Code Online (Sandbox Code Playgroud)

但我的编译器没有编译它,然后我尝试过:

typedef int(*fnc_t())(int);

// Clearer example pointing to rtx

fnc_t* TRY_2 = (fnc_t*)&rtx;

// then has successfully compiled, ex test...

 int main()
 {
    std::cout << TRY_2()(4) << std::endl; // output: 16 ok.
 }
Run Code Online (Sandbox Code Playgroud)

好吧,我要说明一点,¿我怎样才能在不使用的情况下进行正确的铸造typedef

我搜索了整个互联网,我没有找到任何东西......

c++ function-pointers visual-c++ c++14

1
推荐指数
2
解决办法
69
查看次数

intel 汇编 TEST PF 标志操作

我正在使用 进行手动操作TEST (Parity flag operation),问题是我无法获得正确的结果,请考虑以下因素:

ax = 256 = 0000 0001 0000 0000

所以如果我这样做:

test ah, 0x44

标志PF操作应该是:

0000 0000 = 0000 0001 & 0100 0100

PF = 0000 0000 XNOR 0000 0000

PF = 1111 1111

我遵循了英特尔参考资料,根据以下内容:

在此输入图像描述

问题是我做错了什么?

assembly flags operation parity

1
推荐指数
1
解决办法
868
查看次数

有什么方法可以将基本类型存储在变量或成员指针中?

考虑以下代码

#include <type_traits>
#include <iostream>

class A {};

class B : public A {};

class C : public A {};


void parseType(A* base)
{ 
   std::cout << typeid(std::remove_pointer<decltype(base)>::type).name() << "\n";
}


int main(int argc, char** argv)
{
    A* a = new B;

    A* b = new C;

    parseType(a); // outputs A

    parseType(b); // outputs A

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以base从函数中获取存储在参数中的类型parseType()?从字面上看,始终是base类型。

c++ type-deduction

1
推荐指数
1
解决办法
73
查看次数