常量字符串数组
是否可以有一个(固定)数组,将其元素存储在可执行文件的只读段中,而不是存储在堆栈上?我想出了这段代码,但不幸的是,在添加、移动或删除项目时它非常不灵活。如何验证字符串确实存储在只读段中?我尝试了 readelf -a file 但它没有列出字符串。
typedef struct {
int len;
int pos[100];
char data[500];
} FixedStringArray;
const FixedStringArray items = {
4,
{ 9, 14, 19, 24 },
"LongWord1Word2Word3Word4"
} ;
char* GetItem(FixedStringArray *array, int idx, int *len) {
if (idx >= array->len) {
/* Out of range */
*len = -1;
return NULL;
}
if (idx > 0) {
*len = array->pos[idx] - array->pos[idx - 1];
return & array->data[array->pos[idx - 1]];
}
*len = array->pos[idx];
return & array->data[0];
}
void PrintItem(FixedStringArray array, int idx) {
int len;
char *c;
int i = 0;
c = GetItem(&array, idx, &len);
if (len == -1) return;
while (i < len) {
printf("%c", *c);
*c++;
i++;
}
}
我正在考虑一个脚本,它自动为每个数组生成一个结构,并为 pos 和 data 使用正确的长度。内存使用方面有什么问题吗? 或者创建一个结构(如上)来容纳所有字符串会更好吗?
Is it possible to have a (fixed) array which stores its elements in the read-only segment of the executable and not on the stack? I came up with this code but unfortunately it is very unflexible when it comes to adding, moving or deleting items. How do I verify that the strings are indeed stored in the read-only segment? I tried readelf -a file but it doesn't list the strings.
typedef struct {
int len;
int pos[100];
char data[500];
} FixedStringArray;
const FixedStringArray items = {
4,
{ 9, 14, 19, 24 },
"LongWord1Word2Word3Word4"
} ;
char* GetItem(FixedStringArray *array, int idx, int *len) {
if (idx >= array->len) {
/* Out of range */
*len = -1;
return NULL;
}
if (idx > 0) {
*len = array->pos[idx] - array->pos[idx - 1];
return & array->data[array->pos[idx - 1]];
}
*len = array->pos[idx];
return & array->data[0];
}
void PrintItem(FixedStringArray array, int idx) {
int len;
char *c;
int i = 0;
c = GetItem(&array, idx, &len);
if (len == -1) return;
while (i < len) {
printf("%c", *c);
*c++;
i++;
}
}
I am considering a script that automatically generates a struct for each array and uses the correct length for pos and data. Are there any concerns in terms of memory usage?
Or would it be better to create one struct (like above) to fit all strings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定我是否理解你的问题,但你的意思是:
这声明了一个指向常量字符的指针常量数组。
好的,为了避免 strlen,怎么样:
I'm not sure I understand your question, but do you mean:
This declares a constant array of pointers to constant characters.
OK, to avoid strlen, how about:
您的 C 编译器不能将任何/所有文字字符串粘贴到只读内存中(例如启用字符串池的 VC++)吗?或者您是否明确要求它们以这种方式顺序存储?
Can't your C compiler stick any/all literal strings into read-only memory (e.g. VC++ with string pooling enabled)? Or do you explicitly require them to be stored sequentially in that way?
这个问题有些相关:
字符串文字
如前所述,ROM/RAM 中字符串文字的存储取决于平台/实现,您不应该对此做出任何预测。另外,使用脚本来读取和创建适当大小的数组并存储它们是非常不可取的。您最好选择动态内存。
This question is somewhat relevant:
String Literals
As pointed out, the storage of string literals in ROM/RAM is platform/implementation dependent, you should not make any predictions for that. Also using a script to read and create array of appropriate sizes and store them is quite undesirable. You should better go for dynamic memory.