我一直认为GCC会将static const变量放到ELF或此类文件的.rodata段(或.text优化段).但似乎并非如此.
我目前gcc (GCC) 4.7.0 20120505 (prerelease)在使用GNU/Linux的笔记本电脑上使用.它确实将一个静态常量变量放在.bss段中:
/*
* this is a.c, and in its generated asm file a.s, the following line gives:
* .comm a,4,4
* which would place variable a in .bss but not .rodata(or .text)
*/
static const int a;
int main()
{
int *p = (int*)&a;
*p = 0; /* since a is in .data, write access to that region */
/* won't trigger an exception …Run Code Online (Sandbox Code Playgroud) 我有一个话题,我很困惑,我需要详细说明.它是使用const版本和非const版本重载的运算符.
// non-const
double &operator[](int idx) {
if (idx < length && idx >= 0) {
return data[idx];
}
throw BoundsError();
}
Run Code Online (Sandbox Code Playgroud)
我理解这个函数的一部分,取一个索引并检查它的逻辑,返回类中数组数据的索引.还有一个具有相同主体的函数,但函数调用为
const double &operator[](int idx) const
Run Code Online (Sandbox Code Playgroud)
为什么我们需要两个版本?
此示例问题也可能有助于详细说明.下面的每个实例使用哪个版本?
Array a(3);
a[0] = 2.0;
a[1] = 3.3;
a[2] = a[0] + a[1];
Run Code Online (Sandbox Code Playgroud)
我的假设是只调用const版本,a[2]因为我们不想冒险修改a[0]或a[1].
谢谢你的帮助.
我有接受const引用作为参数的函数.它不应该改变参数,但它确实(变量"_isVertex").怎么解决这个问题?这是代码:
#include <vector>
#include <iostream>
using namespace std;
class Element
{
public:
bool isVertex() const
{ return _isVertex; };
private:
bool _isVertex = true;
};
class ElementContainer : public vector <Element>
{
public:
void push(const Element &t)
{
// here everything is fine
cerr << t.isVertex() << ' ';
push_back(t);
// and here _isVertex is false, should be true!
cerr << t.isVertex() << '\n';
}
};
int main()
{
ElementContainer vertex;
vertex.push({});
vertex.push(vertex[0]);
}
Run Code Online (Sandbox Code Playgroud) 这与其他一些问题有关,例如:这个,以及我的一些其他问题.
在这个问题和其他问题中,我们看到我们可以在一个很好的步骤中声明和初始化字符串数组,例如:
const char* const list[] = {"zip", "zam", "bam"}; //from other question
Run Code Online (Sandbox Code Playgroud)
这可以在没有麻烦的函数的实现中完成,或者在任何范围之外的.cpp文件的主体中完成.
我想要做的是将这样的数组作为我正在使用的类的成员,如下所示:
class DataProvider : public SomethingElse
{
const char* const mStringData[] = {"Name1", "Name2", "Name3", ... "NameX"};
public:
DataProvider();
~DataProvider();
char* GetData()
{
int index = GetCurrentIndex(); //work out the index based on some other data
return mStringData[index]; //error checking and what have you omitted
}
};
Run Code Online (Sandbox Code Playgroud)
但是,编译器抱怨并且我似乎无法找出原因.是否可以在类定义的一个步骤中声明和初始化这样的数组?有更好的替代方案吗?
我确信这是一个非常业余的错误,但一如既往,我非常感谢你的帮助和建议.
干杯,
XAN
我需要声明一个指向函数的指针数组,如下所示:
extern void function1(void);
extern void function2(void);
...
void (*MESSAGE_HANDLERS[])(void) = {
function1,
function2,
...
};
Run Code Online (Sandbox Code Playgroud)
但是,我希望将数组声明为常量 - 数组中的数据和指向数据的指针.不幸的是,我不记得在哪里放置const关键字.
我假设在这种情况下,实际指针MESSAGE_HANDLERS已经是常量,因为它被声明为数组.另一方面,如果声明如图所示,那么数组中的函数指针是否可以在运行时更改?
如果我将一个字符串文字分配给a char*,即使使用了许多迂腐选项(-Wall -W -pedantic -std=c99),GCC和Clang都不会抱怨:
char *foo = "bar";
Run Code Online (Sandbox Code Playgroud)
而他们(当然)不要抱怨,如果我分配const char*到char*.
这是否意味着字符串文字被认为是char*类型?他们不应该const char*吗?如果它们被修改,它就不是定义的行为!
和(一个不相关的问题)命令行参数(即:):argv它被认为是一个字符串文字数组?
当编译器抱怨这个时,我有点意外:
public class UsefulClass
{
public const String RatingName = @"Ratings\rating";
}
public class OtherClass
{
public void SomeFunc()
{
UsefulClass useful = new UsefulClass();
String rating = useful.RatingName;
}
}
Run Code Online (Sandbox Code Playgroud)
编译器说,"无法使用实例引用访问静态成员;而是使用类型名称对其进行限定"
这不是问题,String rating = UsefulClass.RatingName;工作正常.我只是好奇这背后的想法是什么?我有一个带有公共常量的公共类的实例,为什么我不能以这种方式获取数据?
请考虑以下代码:
typedef struct Person* PersonRef;
struct Person {
int age;
};
const PersonRef person = NULL;
void changePerson(PersonRef newPerson) {
person = newPerson;
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,编译器抱怨只读值不可分配.但是const关键字不应该使指针成为常量.有任何想法吗?
在lua中有一个const关键字吗?还是其他类似的东西?因为我想将变量定义为const并防止更改变量值.提前致谢.
考虑以下计划:
int main()
{
int array[9];
const int (*p2)[9] = &array;
}
Run Code Online (Sandbox Code Playgroud)
它在C++中编译得很好(请参见此处的实时演示),但在C中编译失败.默认情况下,GCC会发出以下警告.(见此处的现场演示).
prog.c: In function 'main':
prog.c:4:26: warning: initialization from incompatible pointer type [enabled by default]
const int (*p2)[9] = &array;
Run Code Online (Sandbox Code Playgroud)
但如果我使用-pedantic-errors选项:
gcc -Os -s -Wall -std=c11 -pedantic-errors -o constptr constptr.c
Run Code Online (Sandbox Code Playgroud)
它给了我以下编译器错误
constptr.c:4:26: error: pointers to arrays with different qualifiers are incompatible in ISO C [-Wpedantic]
Run Code Online (Sandbox Code Playgroud)
为什么它在C中编译失败而在C++中没有?C&C++标准对此有何看法?
如果我在数组声明语句中使用const限定符,它也可以在C中编译.那么,上面的程序中发生了什么?