[英]warning:array elements are more than 100
I have a piece of code declaring an array.我有一段代码声明一个数组。
#define MAX_ELEMENT 150
Struct_arr arr_elem[MAX_ELEMENT];
Here Struct_arr
is a structure.这里Struct_arr
是一个结构。 On compilation i am getting the warning for this later line as:在编译时,我收到后一行的警告:
warning:array elements are more than 100
could someone please help me about this?有人可以帮我解决这个问题吗?
regards, vivG问候, vivG
That's almost certainly the result of a #warning
directive in the code somewhere.这几乎可以肯定是代码中某处的#warning
指令的结果。 C++ itself does not limit you to 99 array elements, this will be an artificial limit introduced for some reason. C++ 本身并不限制您使用 99 个数组元素,这将是出于某种原因引入的人为限制。
My advice would be to search for that message and see if there's any comments close by which explain why that's a problem.我的建议是搜索该消息,看看附近是否有任何评论可以解释为什么会出现问题。 Something like:就像是:
fgrep 'array has more than' *.c *.cpp *.h *.hpp
in as UNIX-like operating system would be a good starting point.在类 UNIX 操作系统中将是一个很好的起点。
The warnings says that you're trying to allocate a big array on the stack.警告说您正在尝试在堆栈上分配一个大数组。 On typical linux system, the stack has 8Mb, so it can be quite easy to reach that limit (causing a segfault).在典型的 linux 系统上,堆栈有 8Mb,因此很容易达到该限制(导致段错误)。 This is why you have this warning.这就是您收到此警告的原因。
As you tagged your question as C++, please notice that it's not very common to use C-style array in C++.当您将问题标记为 C++ 时,请注意在 C++ 中使用 C 样式数组并不常见。 You would rather use an你宁愿使用
std::vector<Struct_arr> arr_elem(150);
This allocates a vector of 150 elements, but it can be dynamicaly resized, so if you allocated such a big array "just in case", then a std::vector is even more the way to go.这分配了一个包含 150 个元素的向量,但它可以动态调整大小,因此如果您“以防万一”分配了这么大的数组,那么 std::vector 甚至更适合 go。
Last thing, just use plain old constants instead of macros, there is really no benefit from using them in this particular situation.最后一件事,只使用普通的旧常量而不是宏,在这种特殊情况下使用它们确实没有任何好处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.