在 C 中,对不同的事物重复使用相同的缓冲区名称是一种常见的做法吗?
例如,假设我有一个名为 charjournal_name[25]
的缓冲区,用于存储日志名称。现在假设在代码后面的几行我想将某人的名字存储到缓冲区中。我应该使用 char person_name[25]
还是只重复使用 journal_name[25]
?
问题在于,每个阅读代码的人(几周后我也是如此)都必须明白 journal_name
现在实际上是 person_name
。
但反对的论点是,拥有两个缓冲区会增加空间使用量。所以最好使用一个。
对于这个问题你怎么看?
谢谢,博达·西多。
For example, suppose I have a buffer called char journal_name[25]
which I use to store the journal name. Now suppose a few lines later in the code I want to store someone's name into a buffer. Should I go char person_name[25]
or just reuse the journal_name[25]
?
The trouble is that everyone who reads the code (and me too after a few weeks) has to understand journal_name
is now actually person_name
.
But the counter argument is that having two buffers increases space usage. So better to use one.
What do you think about this problem?
Thanks, Boda Cydo.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您确实不想浪费内存,以 C 方式解决此问题的方法是使用块来确定缓冲区的范围:
编译器将为两者重用相同的内存位置,同时为您提供一个完全清晰的名称。
作为替代方案,将其命名为
name
并将其用于 <.<The way to solve this in the C way, if you really don't want to waste memory, is to use blocks to scope the buffers:
The compiler will reuse the same memory location for both, while giving you a perfectly legible name.
As an alternative, call it
name
and use it for both <.<有些代码在这里确实是有序的。不过,有几点需要注意:
保持标识符与对象分离。可以将其称为
scratchpad
或其他名称。另外,从外观上看,这个字符数组不是动态分配的。这意味着您必须分配足够大的暂存器才能重复使用它们。一种更好的方法可能是让你的函数更短:理想情况下,一个函数应该一次只做一件事。看看你们是否可以分手并仍然面对这个问题。
Some code is really in order here. However a couple of points to note:
Keep the identifier decoupled from your objects. Call it
scratchpad
or anything. Also, by the looks of it, this character array is not dynamically allocated. Which means you have to allocate a large enough scratch-pad to be able to reuse them.An even better approach is to probably make your functions shorter: One function should ideally do one thing at a time. See if you can break up and still face the issue.
作为之前(好的)答案的替代方案,那么
稍后
可以吗?
As an alternative to the previous (good) answers, what about
then later
Would it be OK ?
如果两个缓冲区都是自动的,为什么不使用它呢?
大多数编译器都会通过重用内存来正确处理这个问题。
但你保持可读性。
顺便说一句,即使您的编译器很愚蠢并且内存非常低,您也可以使用 union 但保留不同的名称以提高可读性。使用相同的变量是最糟糕的方式。
If both buffers are automatic why not use this?
Most of compilers will handle this correctly with memory reused.
But you keep readability.
By the way even if your compiler is stupid and you are very low of memory you can use union but keep different names for readability. Usage of the same variable is worst way.
请使用
person_name[25]
。没有人喜欢难以阅读的代码。就内存而言,它对你的程序没有多大作用(如果有的话)。请以可读的方式进行。Please use
person_name[25]
. No body likes hard to read code. Its not going to do much if anything at all for your program in terms of memory. Please, just do it the readable way.由于您在问题中提到的原因,在编写代码时,您应该始终(除非您的内存非常紧张)追求可读性和可维护性。
25 个字符(除非这只是一个示例)不会“倾家荡产”,但如果内存非常宝贵,您可以为
journal_name
动态分配存储空间,然后在需要时释放它在为person_name
动态分配存储空间之前已经完成了它。尽管存在指向数组的指针的“开销”。另一种方法是在数组上使用本地作用域:
尽管即使使用此伪代码,该方法也会变得相当长,并且会受益于重构为不会出现此问题的子例程。
You should always (well unless you're very tight on memory) go for readability and maintainability when writing code for the very reason you mentioned in your question.
25 characters (unless this is just an example) isn't going to "break the bank", but if memory is at a premium you could dynamically allocate the storage for
journal_name
and then free it when you've finished with it before dynamically allocating the storage forperson_name
. Though there is the "overhead" of the pointer to the array.Another way would be to use local scoping on the arrays:
Though even with this pseudo code the method is getting quite long and would benefit from refactoring into subroutines which wouldn't have this problem.
如果你担心内存,并且我怀疑 25 字节会是一个问题,但是你可以只使用
malloc
和free
然后你就只剩下额外的 4-8 个字节用于指针的字节。但是,正如其他人提到的,可读性很重要,您可能想要分解您的函数,以便在实际上给出更多有关其用途的指示的函数中使用两个缓冲区。
更新:
现在,我有一个名为
buffer
的缓冲区,例如,我将使用它来读取文件,然后我将使用传递的函数指针,解析结果,以便函数读取文件并适当处理它,这样缓冲区就不会被填充,然后我必须记住它还不应该被覆盖。因此,当从套接字或文件读取时,重用缓冲区可能很有用,但您希望本地化该缓冲区的使用,否则可能会出现竞争条件。
If you are worried about memory, and I doubt 25 bytes will be an issue, but then you can just use
malloc
andfree
and then you just have an extra 4-8 bytes being used for the pointer.But, as others mentioned, readability is important, and you may want to decompose your function so that the two buffers are used in functions that actually give more indication as to their uses.
UPDATE:
Now, I have had a buffer called
buffer
that I would use for reading from a file, for example, and then I would use a function pointer that was passed, to parse the results so that the function reads the file, and handles it appropriately, so that the buffer isn't filled in and then I have to remember that it shouldn't be overwritten yet.So, yet, reusing a buffer can be useful, when reading from sockets or files, but you want to localize the usage of this buffer otherwise you may have race conditions.