如何检查“设置”是否已设置在c中
如果我像这样分配一个 C 数组:
int array[ 5 ];
然后,仅设置一个对象:
array[ 0 ] = 7;
如何检查所有其他键( array[1]
、array[2]
、 ...)正在存储一个值? (当然,在本例中,它们不是。)
是否有类似 PHP 的 isset()
的函数?
if ( isset(array[ 1 ]) ) ...
If I allocate a C array like this:
int array[ 5 ];
Then, set only one object:
array[ 0 ] = 7;
How can I check whether all the other keys ( array[1]
, array[2]
, …) are storing a value? (In this case, of course, they aren't.)
Is there a function like PHP's isset()
?
if ( isset(array[ 1 ]) ) ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我喜欢的一种方法是创建 2 个数组,一个是位数组,标记设置了数组的索引,另一个包含实际值。即使在您不需要知道数组中的某个项目是否已“设置”的情况下,它也可能是一种有用的优化。将每个元素 1 位的位数组清零比初始化
size_t
每个元素 8 字节的数组要快得多,特别是当数组在整个数组中保持稀疏(大部分未填充)时寿命。我使用这一技巧的一个实际例子是在子字符串搜索函数中,使用 Boyer-Moore 风格的坏字符跳过表。该表需要256个
size_t
类型的条目,但只需要填写与实际出现在针串中的字符对应的条目。在非常短的搜索情况下,1kb(或 64 位上的 2kb)memset
将主导 CPU 使用率,导致其他实现在是否使用该表时采用启发式方法。但相反,我让跳过表未初始化,并使用 256 位位数组(仅 32 字节提供给memset
)来标记哪些条目正在使用。An approach I like is to make 2 arrays, one a bit-array flagging which indices of the array are set, and the other containing the actual values. Even in cases where you don't need to know whether an item in the array is "set" or not, it can be a useful optimization. Zeroing a 1-bit-per-element bit array is a lot faster than initializing an 8-byte-per-element array of
size_t
, especially if the array will remain sparse (mostly unfilled) for its entire lifetime.One practical example where I used this trick is in a substring search function, using a Boyer-Moore-style bad-character skip table. The table requires 256 entries of type
size_t
, but only the ones corresponding to characters which actually appear in the needle string need to be filled. A 1kb (or 2kb on 64-bit)memset
would dominate cpu usage in the case of very short searches, leading other implementations to throw around heuristics for whether or not to use the table. But instead, I let the skip table go uninitialized, and used a 256-bit bit array (only 32 bytes to feed tomemset
) to flag which entries are in use.C 中没有这样的事情。静态数组的内容始终是“设置”的。但是,您可以填写一些特殊值来假装它未初始化,例如
There isn't things like this in C. A static array's content is always "set". You could, however, fill in some special value to pretend it is uninitialized, e.g.
你不能
这些值都是未定义的(因此是随机的)。
您可以明确地将所有值归零以开始,这样您至少有一个良好的起点。但是使用幻数来检测对象是否已初始化被认为是不好的做法(但初始化变量被认为是好的做法)。
但是,如果您想显式检查它们自创建以来是否已显式设置(不使用幻数),您需要将该信息存储在另一个结构中。
You can't
There values are all undefined (thus random).
You could explicitly zero out all values to start with so you at least have a good starting point. But using magic numbers to detect if an object has been initialized is considered bad practice (but initializing variables is considered good practice).
But if you want to explicitly check if they have been explicitly set (without using magic numbers) since creation you need to store that information in another structure.
在 C 中,所有元素在分配时都会有值(垃圾)。所以你不能真正拥有你所要求的功能。
但是,默认情况下,您可以使用 memset() 用一些标准值(例如 0 或 INT_MIN)填充它,然后编写 isset() 代码。
In C, all the elements will have values (garbage) at the time of allocation. So you cannot really have a function like what you are asking for.
However, you can by default fill it up with some standard values like 0 or INT_MIN using memset() and then write an isset() code.
我不知道 php,但这里发生了两件事之一
,无论哪种情况,都有一个有意义的概念“不” set”作为数组的值。另一方面,内置类型的 ac 数组在每个单元格中始终具有一些值。如果数组未初始化并且是自动的或者是在堆上分配的,那么这些值可能是随机的,但它们存在。
要获得 php 行为:
I don't know php, but one of two things is going on here
in either case there is a meaningful concept of "not set" for the values of the array. On the other hand a c array of built in type has some value in every cell at all times. If the array is uninitialized and is automatic or was allocated on the heap those values may be random, but they exist.
To get the php behavior:
一种解决方案可能是使用单独的标志数组。当您分配其中一个元素时,请在布尔数组中设置标志。
您还可以使用指针。您可以使用空指针来表示尚未分配的数据。我在下面做了一个例子:
您可以创建一个分配内存并设置数据的函数,以及另一个与 PHP 中的 isset 函数类似的函数,通过为您测试 NULL 来实现。
我希望这对你有帮助。
编辑:确保完成后释放内存。另一个函数可用于释放某些元素或整个数组。
我之前使用过 NULL 指针来表示数据尚未创建或需要重新创建。
One solution perhaps is to use a separate array of flags. When you assign one of the elements, set the flag in the boolean array.
You can also use pointers. You can use null pointers to represent data which has not been assigned yet. I made an example below:
You could make a function which allocates the memory and sets the data and another function which works like the isset function in PHP by testing for NULL for you.
I hope that helps you.
Edit: Make sure the memory is deallocated once you have finished. Another function could be used to deallocate certain elements or the entire array.
I've used NULL pointers before to signify data has not been created yet or needs to be recreated.