C 中的基本数组用法?
这是你们在 ANSI-C99 中获取数组大小的方法吗?来自高级语言的感觉有点笨拙。
int tests[7];
for (int i=0; i<sizeof(tests)/sizeof(int); i++) {
tests[i] = rand();
}
这种分割也有缺陷。
int r = 10000000;
printf ("r: %i\n", r);
int tests[r];
运行它:
r: 10000000
Segmentation fault
10000000 个段错误,但 1000000 个有效。
我如何从中获取更多信息?我应该检查什么以及如何调试这样的东西? C 数组有限制吗?什么是分段错误?
Is this how you guys get size of an array in ANSI-C99? Seems kind of, um clunky coming from higher language.
int tests[7];
for (int i=0; i<sizeof(tests)/sizeof(int); i++) {
tests[i] = rand();
}
Also this Segmentation faults.
int r = 10000000;
printf ("r: %i\n", r);
int tests[r];
run it:
r: 10000000
Segmentation fault
10000000 seg faults, but 1000000 works.
How do I get more info out of this? What should I be checking and how would I debug something like this? Is there a limit on C arrays? What's a segmentation fault?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 C 中获取数组的大小很容易。这将为您提供数组的大小(以字节为单位)。
但我猜你需要的是元素的数量,在这种情况下它会是:
你可以为此编写一个简单的宏:
例如:
Getting size of an array in C is easy. This will give you the size of array in bytes.
But I guess what you require is number of elements, in that case it would be:
You can write a simple macro for this:
For example:
为什么要计算尺寸?
定义一个包含大小的常量,并在声明数组时使用该常量。当您需要数组的大小时,请引用该常量。
作为一名主要使用 C++ 的程序员,我会说,历史上常量通常被定义为枚举值或#define。在 C 中,这可能是当前的而不是历史的,不过 - 我不知道当前的 C 如何处理“const”。
如果您确实想计算大小,请定义一个宏来执行此操作。甚至可能有一个标准的。
段错误的原因很可能是因为您尝试声明的数组大小约为 40 MB,并且被声明为局部变量。大多数操作系统都限制堆栈的大小。将数组保留在堆上或全局内存中,对于大多数系统来说,一个变量 40 MB 可能就可以了,尽管某些嵌入式系统可能仍然会抱怨。在像Java这样的语言中,所有对象都位于堆上,只有引用保留在堆栈上。这是一个简单而灵活的系统,但通常比在堆栈上存储数据效率低得多(堆分配开销、可避免的堆碎片、间接访问开销......)。
Why calculate the size?
Define a constant containing the size and use that when declaring the array. Reference the constant whenever you want the size of the array.
As a primarily C++ programmer, I'll say that historically the constant was often defined as an enum value or a #define. In C, that may be current rather than historic, though - I don't know how current C handles "const".
If you really want to calculate the size, define a macro to do it. There may even be a standard one.
The reason for the segfault is most likely because the array you're trying to declare is about 40 megabytes worth, and is declared as a local variable. Most operating systems limit the size of the stack. Keep your array on the heap or in global memory, and 40 megabytes for one variable will probably be OK for most systems, though some embedded systems may still cry foul. In a language like Java, all objects are on the heap, and only references are kept on the stack. This is a simple and flexible system, but often much less efficient than storing data on the stack (heap allocation overheads, avoidable heap fragmentation, indirect access overheads...).
C 中的数组不知道它们有多大,所以是的,您必须执行
sizeof array / sizeof array[0]
技巧来获取数组中元素的数量。至于段错误问题,我猜测您尝试分配 10000000 *
sizeof int
字节超出了堆栈大小。经验法则是,如果您需要超过几百个字节,请使用malloc
或calloc
动态分配它,而不是尝试创建一个大的auto 变量:
请注意,在大多数情况下,您可以将
tests
视为数组类型(即,您可以为其添加下标,可以将其传递给任何函数)需要一个数组等),但它不是数组类型;它是指针类型,因此sizeoftests / sizeoftests[0]
技巧不起作用。Arrays in C don't know how big they are, so yes, you have to do the
sizeof array / sizeof array[0]
trick to get the number of elements in an array.As for the segfault issue, I'm guessing that you exceeded your stack size by attempting to allocate 10000000 *
sizeof int
bytes. A rule of thumb is that if you need more than a few hundred bytes, allocate it dynamically usingmalloc
orcalloc
instead of trying to create a largeauto
variable:Note that you can treat
tests
as though it were an array type in most circumstances (i.e., you can subscript it, you can pass it to any function that expects an array, etc.), but it is not an array type; it is a pointer type, so thesizeof tests / sizeof tests[0]
trick won't work.传统上,数组具有静态大小。所以我们可以做,
但不能做
因为我们在编译时知道数组的大小,所以获取数组的大小往往很简单。我们不需要
sizeof
因为我们可以通过查看声明来计算出大小。C++ 提供堆数组,
但由于这涉及指针而不是堆栈数组,因此我们必须将大小存储在手动传递的变量中。
Traditionally, an array has a static size. So we can do
but not
Since we know the size of an array at compile time, getting the size of an array tends to trivial. We don't need
sizeof
because we can figure out the size by looking at our declaration.C++ provides heap arrays, as in
but since this involves pointers instead of stack arrays, we have to store the size in a variable which we pass around manually.
我怀疑这是因为整数溢出。尝试使用 printf 打印该值:
如果它打印负数 - 这就是问题所在。
I suspect that it is because of integer overflow. Try printing the value using a printf:
If it prints a negative number - that is the issue.
堆栈溢出!尝试在堆上而不是堆栈上分配。
Stack Overflow! Try allocating on the heap instead of the stack.