小编Dov*_*iin的帖子

为什么我的反汇编C++代码使用指令指针和偏移来获取字符串文字?

我有一个我已经拆解的C++程序,看起来程序集正在使用指令指针获取字符串文字.例如:

leaq    0x15468(%rip), %rsi ## literal pool for: "special"
Run Code Online (Sandbox Code Playgroud)

leaq    0x15457(%rip), %rsi ## literal pool for: "ordinary"
Run Code Online (Sandbox Code Playgroud)

为什么编译器使用指令指针获取字符串文字?这似乎会导致任何人类程序员的头痛,尽管对编译器来说可能并不那么难.

不过,我的问题是为什么?有没有基于机器或历史的原因或编译器编写者是否决定%rip任意使用?

c++ assembly x86-64

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

如何在SpriteKit游戏中获得键盘输入?

我是SpriteKit编程的初学者,一直在试图弄清楚如何处理来自键盘的输入.

到目前为止我发现的是你应该继承NSResponder并像这样实现它:

@interface AppDelegate : NSResponder <NSApplicationDelegate>
-(void)keyUp:(NSEvent *)theEvent;
-(void)keyDown:(NSEvent *)theEvent;
@end

@implementation AppDelegate
-(void)keyUp:(NSEvent *)theEvent
{
   NSLog(@"Key Released");
}
-(void)keyDown:(NSEvent *)theEvent
{
  NSLog(@"Key Pressed");
}
@end
Run Code Online (Sandbox Code Playgroud)

显然,在界面和实现中还有一些方法/属性,AppDelegate但我没有把它们放在那里以保持相关的问题.

接下来,我将开始使用键代码来检测正在按下哪些键,但是甚至不会调用keyUpkeyDown方法.我不知道为什么.

任何帮助?

更新: 感谢您的回答!我发现,你必须执行keyUp,并keyDown直接在您的场景类,因为他们不会得到从AppDelegate中调用.再次感谢您的帮助!

macos objective-c sprite-kit

7
推荐指数
2
解决办法
5010
查看次数

"-ftrapv"和"-fwrapv":哪种效率更高?

来自GNU的网站:

-ftrapv
This option generates traps for signed overflow on addition, subtraction, multiplication operations. 
-fwrapv
This option instructs the compiler to assume that signed arithmetic overflow of addition, subtraction and multiplication wraps around using twos-complement representation. This flag enables some optimizations and disables others.
Run Code Online (Sandbox Code Playgroud)

我有两个问题.1)哪些选项更适合性能?2)当-ftrapv定义说它产生"陷阱" 时,它意味着什么?这是否意味着例外?(我猜不是,但值得问.)

资料来源:https://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Code-Gen-Options.html

c++ gcc gnu g++ integer-overflow

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

Haskell的"do"关键字有什么作用?

我是一名C++/Java程序员,我正在努力学习Haskell(以及一般的函数式编程),而且我一直在努力学习它.我试过的一件事是这样的:

isEven :: Int -> Bool
isEven x =
    if mod x 2 == 0 then True
    else False

isOdd :: Int -> Bool
isOdd x =
    not (isEven x)

main =
    print (isEven 2)
    print (isOdd 2)
Run Code Online (Sandbox Code Playgroud)

但是在编译期间这个错误失败了:

ghc --make doubler.hs -o Main
[1 of 1] Compiling Main             ( doubler.hs, doubler.o )

doubler.hs:11:5: error:
    • Couldn't match expected type ‘(a0 -> IO ()) -> Bool -> t’
              with actual type ‘IO ()’
    • The function ‘print’ is applied to …
Run Code Online (Sandbox Code Playgroud)

io syntax haskell program-entry-point keyword

5
推荐指数
2
解决办法
923
查看次数

捕获/处理 AngelScript 函数中抛出的异常

我正在使用 AngelScript,我似乎无法理解的一件事是如何捕获从 C++ 抛出但从 AngelScript 调用的异常。这是我到目前为止所得到的:

// test.as

void main()
{
    print("Calling throwSomething...");

    throwSomething();

    print("Call finished");
}
Run Code Online (Sandbox Code Playgroud)

void print(string)void throwSomething()是注册到引擎的两个函数,来源如下。根据AngelScript 文档

向脚本引擎注册的应用程序函数和类方法允许抛出 C++ 异常。虚拟机将自动捕获任何 C++ 异常、中止脚本执行并将控制权返回给应用程序。

以下是提供的用于处理异常的示例代码:

asIScriptContext *ctx = engine->CreateContext();
ctx->Prepare(engine->GetModule("test")->GetFunctionByName("func"));
int r = ctx->Execute();
if( r == asEXECUTION_EXCEPTION )
{
  string err = ctx->GetExceptionString();
  if( err == "Caught an exception from the application" )
  {
    // An application function threw an exception while being invoked from the script
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

我几乎逐字地将这段代码复制到我的编辑器中并尝试运行它。不幸的是,即使我将调用包装Execute …

c++ exception angelscript

5
推荐指数
0
解决办法
163
查看次数

为什么这个程序会等待10秒而不是倒计时?

我正在尝试一些c ++ 11代码,我尝试编写一个从10开始倒数​​的程序,在输出之间休眠.这是我到目前为止所拥有的:

#include <iostream>
using namespace std;

#include <chrono>
#include <thread>

void Sleep(int x)
{
        std::this_thread::sleep_for(std::chrono::duration<int>(x));
}

int main()
{
    for (int x=10; x>0; x--) {
            cout << x << "...";
            Sleep(1);
    }

    cout << " FIRE!!\n";
}
Run Code Online (Sandbox Code Playgroud)

问题是,这段代码等待10秒然后打印所有输出,而不是从10开始倒计时.这是怎么回事?我该如何解决?

(顺便说一下,我在运行Linux Mint 17和MacOSX 10.9的计算机上试过这个,两次都得到了相同的结果)

c++ c++11 c++-chrono

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

如何将多维C数组传递给函数?

我正在学习大学课程中的C和指针,我认为除了多维数组和指针之间的相似性之外,我对这个概念有很好的把握.

我认为,因为所有数组(甚至是多维数组)都存储在连续的内存中,你可以安全地将其转换为a int*(假设给定的数组是int[].)但是,我的教授说定义中的星数取决于数字数组中的维度.所以,一个int[]会变成一个int*,一个int[][]会成为一个int**,等等.

所以我写了一个小程序来测试这个:

void foo(int** ptr)
{    
}

void bar(int* ptr)
{    
}

int main()
{
    int arr[3][4];

    foo(arr);
    bar(arr);
}
Run Code Online (Sandbox Code Playgroud)

令我惊讶的是,编译器在两个函数调用上发出警告.

main.c:22:9: warning: incompatible pointer types passing 'int [3][4]' to parameter of type
      'int **' [-Wincompatible-pointer-types]
    foo(arr);
        ^~~
main.c:8:16: note: passing argument to parameter 'ptr' here
void foo(int** ptr)
         ^
main.c:23:9: warning: incompatible pointer types passing 'int [3][4]' to parameter of type
      'int *' [-Wincompatible-pointer-types] …
Run Code Online (Sandbox Code Playgroud)

c arrays pointers multidimensional-array

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

为什么Clang会警告未使用的指针和未使用的原语,而不是未使用的对象?

在此代码段中...

sf::Time obj;
sf::Time* ptr;
int i;
int* p2;
Run Code Online (Sandbox Code Playgroud)

第一行不会产生警告,但其他三行都会产生警告.怎么会?

(顺便说一句,这是一个完整的方法.变量没有做任何事.)

c++ clang

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

为什么这个stringstream的使用在linux上不起作用,但在Mac上运行?

我有这个代码将一些转换t为整数:

template <class T>
int toInt(const T& t)
{
  int i = -1;
  (std::stringstream() << t) >> i;

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

这在我的Mac上工作正常,但每当我尝试在我学校的linux机器上使用它时,它都无法编译.我必须改用这样的东西:

template <class T>
int toInt(const T& t)
{
  int i = -1;
  std::stringstream ss;
  ss << t;
  ss >> i;

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

哪个工作正常.

为什么是这样?

c++

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