可能不是最有效的方式,但它是否正确且便携?
int are_overlapping(const char *a, const char *b) {
return (a + strlen(a) == b + strlen(b));
}
Run Code Online (Sandbox Code Playgroud)
澄清:我正在寻找的是内存重叠,而不是实际内容.例如:
const char a[] = "string";
const char b[] = "another string";
are_overlapping(a, b); // should return 0
are_overlapping(a, a + 3); // should return 1
Run Code Online (Sandbox Code Playgroud) (使用Visual C++ 2010,在调试中编译并关闭优化)
我有以下非常简单的课程:
class exampleClass
{
public:
exampleClass()
{
cout << "in the default ctor" << endl;
}
private:
exampleClass (const exampleClass& e)
{
cout << "in the copy ctor" << endl;
}
};
Run Code Online (Sandbox Code Playgroud)
当我尝试使用以下main编译它时:
#include <iostream>
using namespace std;
int main()
{
exampleClass e1=exampleClass();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到编译错误:
'exampleClass::exampleClass' : cannot access private
member declared in class 'exampleClass'
Run Code Online (Sandbox Code Playgroud)
当我从copy ctor中删除访问修饰符"private"时,程序仅编译并打印:
in the default ctor
Run Code Online (Sandbox Code Playgroud)
为什么会这样?如果编译器无论如何都不会调用copy ctor,为什么它会烦我?
由于有些人错过了第一行(至少在一些编辑之前),我将重复它:
有没有人曾经使用指针/引用/指向成员(非类型)模板参数?
我不知道任何(理智/真实世界)场景,其中C++特性应该被用作最佳实践.
演示功能(用于指针):
template <int* Pointer> struct SomeStruct {};
int someGlobal = 5;
SomeStruct<&someGlobal> someStruct; // legal c++ code, what's the use?
Run Code Online (Sandbox Code Playgroud)
任何启蒙都将不胜感激!
如所解释的,例如,在这里,存在用于void关键字(更有经验的C/C++程序员可以跳到第四使用)3个主要用途:
1)作为不返回任何内容的函数的返回类型.这将导致代码示例如下:
void foo();
int i = foo();
Run Code Online (Sandbox Code Playgroud)
生成编译器错误.
2)作为函数参数列表中的唯一参数.AFAIK,一个空函数的参数列表与编译器完全相同,因此以下两行在含义上是相同的:( 编辑:它仅在c ++中为真.注释显示c中的差异).
int foo();
int foo(void);
Run Code Online (Sandbox Code Playgroud)
3)void*是一种特殊类型的通用指针 - 它可以指向任何未使用const或volatile关键字声明的变量,转换为/来自任何类型的数据指针,并指向所有非成员函数.另外,它不能被解除引用.我不会举例.
还有第四种用途,我不完全理解:
4)在条件编译中,它通常用在表达式(void)0中,如下所示:
// procedure that actually prints error message
void _assert(char* file, int line, char* test);
#ifdef NDEBUG
#define assert(e) ((void)0)
#else
#define assert(e) \
((e) ? (void)0 : \
__assert(__FILE__, __LINE__, #e))
#endif
Run Code Online (Sandbox Code Playgroud)
我试图通过实验来理解这个表达式的行为.以下所有内容均有效(编译良好):
int foo(); // some function declaration
int (*fooPtr)(); // function pointer
void(foo);
void(fooPtr);
void(0);
(void)0;
void('a');
void("blabla");
exampleClass e; //some …Run Code Online (Sandbox Code Playgroud) (我正在使用VS 2010,但大部分信息至少与VS 2003相关,可能与构建配置菜单\ GUI的组织/布局略有不同)
配置项目构建时,有一个名为"VC++目录"的部分包含6个标签.其中2个是:
此外,如果您转到"C/C++" - >"其他包含目录",您可以指定其他目录,AFAIK(来自MSDN和VS帮助中这些目录的描述)与"包含目录"相同(尽管它们之间可能存在一些搜索顺序).同样,如果您转到"链接器" - >"其他库目录",您可以指定库与项目链接的其他路径(此处描述更精确 - "允许用户覆盖环境库路径",因此这些路径被更快地搜索到).
是否有理由使用一个(路径)而不是另一个?什么是最佳做法?
请在您的答案中说明使用属性页面功能(这为不同项目的配置增加了灵活性,并允许轻松地重用现有项目,但这使我对此处的最佳实践更加困惑).提前致谢.
c c++ visual-studio-2005 visual-studio-2010 visual-studio-2008
在转向更新版本的CC编译器时,会出现在以前工作的模块中的段错误.
从核心文件中我可以了解segfault的起源功能.当我观察到这个功能时,我找不到任何可疑的东西.
第一个主要问题是segfault仅在"发布"(优化打开)中进行编译时才会重现,并且不会在"调试"中重现.此外,段错误不会在g ++上重现.
现在我开始使用打印,并且在将代码中的某些行添加cout/ printf(二进制搜索段错误行/打印指针的值)时出现了更大的问题,段错误没有重现.此外,我在代码中的某一行添加了一个cout来维护段错误,这可能意味着段错在该行之前发生.在该行之后评论行使得段错误消失.
对我来说,这会尖叫内存损坏(特别是堆栈),但我不知道如何在不查看生成的程序集的情况下推进此操作.
有任何想法吗?提前致谢.
我正在开发SunOS_5.10_Studio_12_5.12_64,CC版"Sun C++ 5.12 SunOS_sparc 2011/11/16"
我正在使用一个旧的开源库,其中包含以下(简化)API:
// some class that holds a raw pointer to memory on the heap
// DOES NOT delete it in its destructor
// DOES NOT do a "deep" copy when copied/assigned (i.e., after copying both objects
// will point to the same address)
class Point;
// function used to construct a point and allocate its data on the heap
Point AllocPoint();
// function used to release the memory of the point's data
void DeallocPoint(Point& p);
// Receives a pointer/c-array of …Run Code Online (Sandbox Code Playgroud) 现在,我们有std::array,std::vector和括号的初始化,是C风格数组仍然需要?
在一些内存测试中,我做了以下程序的段错误:
#include <string>
#include <iostream>
using namespace std;
int main()
{
cout << "Beginning Test" << endl;
const int N = 2000000;
string sArray[N];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于我在打印"Beginning Test"之前得到了段错误,我在GDB中运行它并检查了回溯,我得到的唯一的东西是:
程序接收信号SIGSEGV,分段故障.
Main.cxx中main()中的0x00000000004008c5:11
11字符串sArray [N];
(gdb)bt
#0 0x00000000004008c5在Main.cxx:11的main()中
对我来说最奇怪的是,如果我将N设置为1000000(1M)而不是2000000(2M),我就不会得到段错误.
可能是什么问题的任何线索?
我正在使用Linux Red-Hat 2.6.18和g ++(GCC)4.1.2.
谢谢!