在C++ 03中,表达式是rvalue或lvalue.
在C++ 11中,表达式可以是:
两类已成为五大类.
C和C++中未定义,未指定和实现定义的行为有什么区别?
c c++ undefined-behavior unspecified-behavior implementation-defined-behavior
以下代码在第2行接收seg错误:
char *str = "string";
str[0] = 'z'; // could be also written as *str = 'z'
printf("%s\n", str);
Run Code Online (Sandbox Code Playgroud)
虽然这非常有效:
char str[] = "string";
str[0] = 'z';
printf("%s\n", str);
Run Code Online (Sandbox Code Playgroud)
经过MSVC和GCC测试.
我正在使用 Microsoft Visual C++ 将以下程序编译为 C++20 程序:
#include <iostream>
#include <tuple>
int main()
{
auto t1 = std::make_tuple("one", "two", "three");
auto t2 = std::make_tuple("one", "two", "three");
std::cout << "(t1 == t2) is " << std::boolalpha << (t1 == t2) << "\n";
std::cout << "(t1 != t2) is " << std::boolalpha << (t1 != t2) << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我看到以下输出:
(t1 == t2) is false
(t1 != t2) is true
Run Code Online (Sandbox Code Playgroud)
元组是相同的,那么为什么它的比较结果是错误的呢?我该如何解决?
下面的代码安全吗?编写类似于此的代码可能很诱人:
#include <map>
const std::map<const char*, int> m = {
{"text1", 1},
{"text2", 2}
};
int main () {
volatile const auto a = m.at("text1");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该映射仅用于字符串文字.
我认为它是完全合法的并且似乎正在工作,但是我从未看到保证在两个不同的地方使用的文字指针是相同的.我无法让编译器为具有相同内容的文字生成两个单独的指针,所以我开始怀疑这个假设是多么坚定.
我只对具有相同内容的文字是否可以有不同的指针感兴趣.或者更正式的,上面的代码可以除外吗?
我知道有一种编写代码的方法可以确保它有效,我认为上面的方法很危险,因为编译器可以决定为文字分配两个不同的存储,特别是如果它们放在不同的翻译单元中.我对吗?
编译器(MS Visual C++ 2010)如何在不同的cpp源文件中组合相同的字符串文字?例如,如果我分别在src1.cpp和src2.cpp中有字符串文字"hello world \n".编译的exe文件在constant/readonly部分中可能只有1个"hello world"字符串文字.这个任务是由链接器完成的吗?
我希望实现的是我得到了一些用汇编编写的模块供C++模块使用.这些汇编模块包含许多长字符串文字定义.我知道字符串文字与C++源代码中的其他字符串文字相同.如果我将我的程序集生成的obj代码与编译器生成的obj代码链接,那么这些字符串文字是否会被链接器合并以删除冗余字符串,就像所有模块都在C++中一样?
I'd like to ask if is it portable to rely on string literal address across translation units? I.e:
A given file foo.c has a reference to a string literal "I'm a literal!", is it correct and portable to rely that in other given file, bar.c in instance, that the same string literal "I'm a literal!" will have the same memory address? Considering that each file will be translated to a individual .o file.
For better illustration, follows an …
首先,考虑这个例子:
#include <iostream>
using namespace std;
int main()
{
cout << ("123" == "123");
}
Run Code Online (Sandbox Code Playgroud)
我期望什么:由于“123”是一个const char*,我期望对这些字符串的地址(如这些答案之一所述)进行比较。
...因为
!=和==只会比较这些字符串的基地址。不是字符串本身的内容。
但输出仍然是1. 好吧,我们实际上不知道如何比较两个纯右值对象的地址(或者至少我不明白它是如何完成的)。因此,让我们将这些字符串声明为变量,看看会发生什么:
#include <iostream>
using namespace std;
int main()
{
const char* a = "1230";
const char* b = "1230";
cout << (a == b);
}
Run Code Online (Sandbox Code Playgroud)
输出仍然是1. 那么const char*琴弦不会衰减吗?或者编译器设法进行一些优化并仅为一个字符串分配内存?好吧,让我们尝试避免它们:
#include <iostream>
using namespace std;
int main()
{
const char* a = "1230";
const char* b = "1231";
b …Run Code Online (Sandbox Code Playgroud) c++ ×7
c ×3
visual-c++ ×2
c++-faq ×1
c++11 ×1
c-strings ×1
expression ×1
implementation-defined-behavior ×1
linker ×1
pointers ×1
pooling ×1
storage ×1
string ×1
tuples ×1