有没有办法让缓冲函数不浪费那么多内存?
像 GetWindowText()、GetModuleFileName()、SHGetFolderPath() 这样的缓冲区函数让我很生气,因为你几乎总是在缓冲区中浪费这么多内存。有没有办法让他们不浪费那么多内存?
buffer functions like GetWindowText(), GetModuleFileName(), SHGetFolderPath() make me angry because you almost always waste so much memory in the buffer. Is there a way to have them not waste so much memory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“让我生气”?你是六岁的孩子吗? :-)
我不完全确定你的抱怨是什么。通过指定您自己的最大长度,您可以很容易地限制从 GetWindowText 和 GetModuleFileName 返回的数量(以丢失信息为代价)。
但是,如果你想获得整个东西,你就需要空间。没有办法解决这个问题。
SHGetFolderPath
不太方便,但毕竟只有MAXPATH
个字符。我很难相信 Windows 下的人会关心存储这些数据项所需的非常小的分配。您确实知道您拥有绝对的地址空间,是吗?如果您真的担心的话,您可以共享内存,将其重复用于不止一件事(当然不是同时进行)。
"make me angry"? Are you a six-year-old? :-)
I'm not entirely certain what your complaint is here. You can restrict how much is returned from
GetWindowText
andGetModuleFileName
quite easily (at the cost of losing information) by specifying your own maximum length.But, if you want to be able to get the whole thing, you need space for it. There's no way around that.
SHGetFolderPath
is not so accomodating but it's onlyMAXPATH
characters after all.I find it hard to believe that someone under Windows is concerned about the very small allocation needed for storing these data items. You do know that you have absolute bucketloads of address space, yes? And you can share the memory, reusing it for more than one thing (though not concurrently of course) if you're really worried.
通常,您将使用已在堆栈上分配的缓冲区来调用这些函数。在堆栈上分配足够的空间实际上没有任何成本,因为从函数返回时它就不再被使用。
如果您尝试执行仅分配所需内存量的操作(例如通过不断增加的缓冲区大小重复调用
GetWindowText()
),您将浪费时间而不是内存。时间(以及能源)的成本要高得多。Typically you'll call those functions with a buffer that has been allocated on the stack. There's virtually no cost to allocating enough space on the stack, because it becomes unused the moment you return from your function.
If you try to do something that allocates only as much memory as is required (such as calling
GetWindowText()
repeatedly with an increasing buffer size), you'll waste time instead of memory. Time (and therefore energy) is much more costly.针对不同的功能有不同的解决方案。例如,可以在调用 GetWindowText() 之前调用 GetWindowTextLength()。使用 GetModuleFileName(),您可以传递一个小缓冲区,并查看文件名是否合适。还有其他函数,如果您为缓冲区传递 null,它们将在您下次调用该函数时返回您需要为缓冲区分配的确切大小。
不过,一般来说,你是在小题大做。如今内存非常便宜,如果文件名(例如)导致您耗尽内存,我会感到非常惊讶。只需分配一个 32k 缓冲区(最大 NTFS 路径长度)即可完成。
There are different solutions for different functions. For example, a call to GetWindowText() can be preceded by a call to GetWindowTextLength(). With GetModuleFileName(), you can pass a small buffer, and see if the filename fits. There are other functions where if you pass a null for your buffer, they will return the exact size you need to allocate for your buffer the next time you cal the function.
In general, though, you're making a big deal out of a small problem. Memory is very cheap these days, and I'd be very surprised if a file name, for example, is causing you to exhaust memory. Just allocate a 32k buffer (maximum NTFS path length) and be done with it.