gpt4 book ai didi

c++ - Xcode c++ 和 VS2010 c++ 有什么区别

转载 作者:行者123 更新时间:2023-11-28 07:40:52 27 4
gpt4 key购买 nike

我对 C++ 很陌生,我有一些 java 经验。

目前我正在使用 xcode 编写一个小型 c++ 项目。我刚刚使用了标准库。然后我在 VS2010 中创建一个项目。我在项目的源文件夹下“添加现有项目(我的 xcode 项目中的源文件)”。之后发现工程可以编译,但是在VS2010中运行不正常。

听说c++代码具有可移植性,我不明白为什么在xcode上运行的代码在VS2010中会出现问题。

在我的源代码中,我写了类似这样的东西:

嗯:

  class a{
public :
int p ;
vector<Token*> v;
a();
int b();
void c();
}

a.cpp:

   a::a(){ //constructor of a
p = 0;
v.push_back(new Token("a",1));
}
int a::b(){
......
//breakpoint to view p, v
c();
}
void a::c(){
.......
// when I set breakpoint here, in xcode, the debugger stops here.
// in VS2010, it said the debugger did not hit this breakpoint.
}

在运行时,xcode 调试器可以正确显示 vector 和 p,但在 VS2010 中,调试器无法正确显示 p 和 vector。如果我展开 vector 的 View , vector 的大小在 VS2010 中变得非常大,但 xcode 中的 vector 大小是正确的。

有什么方法可以将 Xcode C++ 项目导入 VS2010?或者有什么方法可以解决上述问题?

最佳答案

C++ 标准定义了一个“抽象机器”,它在出现格式良好的 C++ 程序时会产生某些行为。编译器不需要重现抽象机的所有行为,只需要重现它的“可观察行为”。

C++11 标准,§1.9/8:

The least requirements on a conforming implementation are:

  • Access to volatile objects are evaluated strictly according to the rules of the abstract machine.
  • At program termination, all data written into files shall be identical to one of the possible results thatexecution of the program according to the abstract semantics would have produced.
  • The input and output dynamics of interactive devices shall take place in such a fashion that prompting output is actually delivered before a program waits for input. What constitutes an interactive device is implementation-defined.

These collectively are referred to as the observable behavior of the program. [Note: More stringent correspondences between abstract and actual semantics may be defined by each implementation. — end note ]

请注意,“我的函数将始终在运行时被调用”不是这些要求之一。只要可观察到的行为与标准允许的相匹配,就允许(实际上,鼓励)编译器进行非常积极的优化。允许编译器假定您编写了一个不会导致未定义行为的格式良好的 C++ 程序。假设它这样做了,如果它注意到一个函数没有可观察的行为并且会返回一个一致的值,它就可以完全省略调用,在编译时计算值等。

VS的输出是否坏掉很大程度上取决于

  • 优化是否被禁用(许多优化是默认打开的,但大多数编译器——包括 VC++——如果被问到会禁用非必要的优化);
  • a::c() 是否真的在调试器之外调用过(如果从未使用过,编译器甚至可能不会将其包含在二进制文件中);
  • 它是否会引起任何可观察到的副作用;
  • 在某些情况下,a::b() 的内容,甚至是调用者的内容;
  • 在可能调用 a::b()a::c() 之前,程序的行为是否未定义——特别是,无论您是通过悬空指针或引用等调用 a::b() 等(一旦程序运行偏离轨道,所有赌注都将关闭);

还有一些其他的东西。

不过,我要说的是:如果 VS 允许您查看 vp 的内容,并且它们具有垃圾值,那么问题更有可能是代码而不是调试器或编译器。如果它不知道如何显示,它可以很容易地说“我不能显示这个”(因为它喜欢处理 .net 对象 :P )。它认为自己可以、尝试过但错了的事实很能说明问题。

关于c++ - Xcode c++ 和 VS2010 c++ 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15880917/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com