小编Ada*_*eld的帖子

为什么编译器不会警告超出范围的静态数组索引?

最近,我的一位同事通过写出堆栈上的静态数组(他在不增加数组大小的情况下添加了一个元素)来严重受伤.编译器不应该捕获这种错误吗?以下代码使用gcc完全编译,即使使用-Wall -Wextra选项,但它显然是错误的:

int main(void)
{
  int a[10];
  a[13] = 3;  // oops, overwrote the return address
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我很肯定这是不确定的行为,虽然我现在找不到C99标准的摘录.但是在最简单的情况下,数组的大小称为编译时,并且索引在编译时是已知的,编译器是否应该至少发出警告?

c arrays warnings

20
推荐指数
4
解决办法
7904
查看次数

使用{*this}初始化类

团队成员建议使用这样的初始化器:

return Demo{ *this };
Run Code Online (Sandbox Code Playgroud)

比以下更好:

return Demo(*this);
Run Code Online (Sandbox Code Playgroud)

假设一个像这样的简单类:

class Demo {
public:
    int value1;
    Demo(){}
    Demo(Demo& demo) {
        this->value1 = demo.value1;
    }
    Demo Clone() {
        return Demo{ *this };
    }
};
Run Code Online (Sandbox Code Playgroud)

我承认以前没有看过{ *this }语法,也找不到足够好的解释,以便我理解这两个选项的不同之处.是否有性能优势,语法选择或更多内容?

c++ c++11

19
推荐指数
2
解决办法
432
查看次数

类型为const char*的参数与"LPCWSTR"类型的参数不兼容

我试图在Visual Studio 2012中使用C创建一个简单的Message Box,但是我收到以下错误消息

argument of type const char* is incompatible with parameter of type "LPCWSTR"

err LNK2019:unresolved external symbol_main referenced in function_tmainCRTStartup
Run Code Online (Sandbox Code Playgroud)

这是源代码

#include<Windows.h>

int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow)
{

    MessageBox(0,"Hello","Title",0);

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

请帮忙

感谢致敬

c

17
推荐指数
3
解决办法
4万
查看次数

通过引用函数传递新构造的对象是否合法?

具体来说,是以下合法的C++?

class A{};

void foo(A*);
void bar(const A&);

int main(void)
{
    foo(&A());  // 1
    bar(A());  // 2
}

它似乎工作正常,但这并不意味着它必然是合法的.是吗?

编辑 - 更改A&const A&

c++

16
推荐指数
3
解决办法
7992
查看次数

如何使用依赖库分发Mac OS X?

我有一个程序(特别是我的SO DevDays倒计时应用挑战的入口),它依赖于几个动态库,即libSDL,libSDL_ttf等.我/opt/local/lib通过MacPorts 安装了这些库,许多人不会安装这些库(有些人可能已经安装了这些库,但是没有安装它们).

如何分发我的程序,以便没有安装这些库的人可以开箱即用?显然我将不得不分发各种.dylib文件,但这样做是不够的.动态加载程序仍然会查找安装在我安装的位置的库.有没有办法告诉动态加载器查看可执行文件的当前目录,比如Windows对DLL的作用?人们不应该修改任何环境变量(例如DYLD_LIBRARY_PATH),因为我再次希望这可以开箱即用.

macos software-distribution dynamic-linking dyld

15
推荐指数
2
解决办法
7831
查看次数

有没有办法在没有连接到调试器的情况下捕获iPhone上的NSLog输出?

我正在记录一堆数据NSLog().当我的iPhone没有连接到我的开发机器并在调试器下运行时,有没有办法捕获日志数据?

例如,我可以将其重定向到文件,然后在稍后的时间点通过Xcode读取日志文件吗?我需要这样做,以便在WiFi很差的情况下测试我的应用程序,这需要我远离我的办公桌.

iphone cocoa-touch nslog

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

你如何在Python中子类化文件类型?

我正在尝试使用filePython 子类化内置类来为stdin和添加一些额外的功能stdout.这是我到目前为止的代码:

class TeeWithTimestamp(file):
    """
    Class used to tee the output of a stream (such as stdout or stderr) into
    another stream, and to add a timestamp to each message printed.
    """

    def __init__(self, file1, file2):
        """Initializes the TeeWithTimestamp"""
        self.file1 = file1
        self.file2 = file2
        self.at_start_of_line = True

    def write(self, text):
        """Writes text to both files, prefixed with a timestamp"""

        if len(text):
            # Add timestamp if at the start of a line; also add …
Run Code Online (Sandbox Code Playgroud)

python file subclass

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

C++ STL Vector Iterator访问Object的成员

我想我已经正确地声明了一个带有对象的Vector.但是,在使用Iterator进行循环时,我不知道如何访问它的成员.

在我的代码中,行--- >> cout <<""<<*Iter;

如何打印成员的内容?喜欢*Iter.m_PackLine ???

不确定我是否使用了正确的术语,但感谢您的帮助!谢谢

class CFileInfo
{
  public:
      std::string m_PackLine;
      std::string m_FileDateTime;
      int m_NumDownloads;
};

void main()
{
  CFileInfo packInfo;

  vector<CFileInfo, CFileInfo&> unsortedFiles;
  vector<CFileInfo, CFileInfo&>::iterator Iter;

  packInfo.m_PackLine = "Sample Line 1";
  packInfo.m_FileDateTime = "06/22/2008 04:34";
  packInfo.m_NumDownloads = 0;
  unsortedFiles.push_back(packInfo);

  packInfo.m_PackLine = "Sample Line 2";
  packInfo.m_FileDateTime = "12/05/2007 14:54";
  packInfo.m_NumDownloads = 1;
  unsortedFiles.push_back(packInfo);

 for (Iter = unsortedFiles.begin(); Iter != unsortedFiles.end(); Iter++ )
 {
    cout << " " << *Iter; // !!! THIS IS WHERE I GET STUMPED …
Run Code Online (Sandbox Code Playgroud)

c++

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

按位NOT(〜)的奇怪行为

我该如何解释以下行为?

#include<iostream>

using namespace std;

int main(){

       unsigned char a = 8;

       cerr << "a: " << (int)a << '\n';

       unsigned char b = (~a) >> 6;

       cerr << "b: " << (int)b << '\n';

       unsigned char c = (~a);
       c = c >> 6;

       cerr << "c: " << (int)c << '\n';

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

输出:

a: 8
b: 255
c: 3
Run Code Online (Sandbox Code Playgroud)

经过进一步的测试,似乎(~a)变成了int而不是unsigned char.这就是为什么它1会被转移的原因.

这是怎么回事?

编辑:我的编译器只是标准的gcc 4.1.2

c c++ types bit-manipulation

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

C++合成的移动构造函数如何受volatile和virtual成员的影响?

看下面的代码:

struct node
{

  node();
  //node(const node&);    //#1
  //node(node&&);         //#2

  virtual                 //#3
  ~node ();

  node*
  volatile                //#4
  next;

};

int main()
{

  node m(node());         //#5
  node n=node();          //#6
}
Run Code Online (Sandbox Code Playgroud)

使用gcc-4.6.1编译时会产生以下错误:

g++ -g --std=c++0x   -c -o node.o node.cc
node.cc: In constructor node::node(node&&):
node.cc:3:8: error: expression node::next has side-effects
node.cc: In function int main():
node.cc:18:14: note: synthesized method node::node(node&&) first required here
Run Code Online (Sandbox Code Playgroud)

据我所知,编译器无法在第6行创建默认移动或复制构造函数,如果我取消注释第1行或第2行它编译正常,这很清楚.代码在没有c ++ 0x选项的情况下编译正常,因此错误与默认移动构造函数有关.

但是,节点类中的内容会阻止创建默认移动构造函数吗?如果我评论任何第3行或第4行(即使析构函数非虚拟或使数据成员非易失性)它再次编译,那么这两者的组合是否使它不能编译?

另一个难题,第5行不会导致编译错误,与第6行有什么不同?它是否特定于gcc?还是gcc-4.6.1?

c++ g++ move-constructor c++11

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