Is there a way to have a bit bucket pointer? (C/C++)
Is there a way to have a bit bucket pointer?
A lot of IO (specifically input related) system calls return data to a buffer of a specific size. Is there a trick or way to make a sorta bit bucket pointer, so I can accept any amount of data that will be thrown away. Doing something like "char tmp[INT_MAX]" is crazy. The behavior I am looking for is something like /开发者_JAVA百科dev/null, only in a pointer world.
Not to hopeful on this.... just curious.
Thanks, Chenz
UPDATE: Perhaps mmap-ing /dev/null. Forgot about that when I asked the question.
Which IO interface are you using? If it is FILE based methods (fopen, fread, etc) you can just keep reading into the same buffer and ignore its contents. You can do anything in C/C++ so what you want is certainly possible, but it isn't totally clear what you are trying to do. Maybe post some code?
I'd imagine just a temp array on the stack being OK to read dummy data to. Trying some elaborate method will probably invalidate CPU caches and bring the performance down.
If it's a file, you can skip data by seeking. mmaping /dev/null WILL allocate and consume memory when the memory area is written to. In any case, it's not really clear what you're trying to do.
A lot of IO (specifically input related) system calls return data to a buffer of a specific size.
Usually that size is either small or user-specified.
If it is a predefined small size, allocate the buffer on the stack and ignore the results.
If you specify the size, see if a size specification of zero would cause the buffer to be ignored. If so, do it. If not, allocate some small buffer and (if necessary) repeatedly call the function to flush all the unwanted data through it.
Probably, also a good idea to write the author of the function and ask that a NULL buffer pointer be allowed.
You can mmap a single allocated page to a bunch of consecutive virtual addresses. But if you increment the pointer enough times it'll eventually fall off the end. There's no getting around that, any pointer you provide will end up pointing to something important if incremented enough.
You're in better shape if the API you're calling accepts an iterator instead of a pointer, because you can make incrementing the iterator a no-op.
Maybe you can seek to where you're trying to go.
The only way I've come up with is to write some sort of system call that would switch all of the virtual memory in a program to another psuedo program virtual memory space. But the psuedo memory space would be write only and never stored. Then you'd switch the virtual memory space back when you're done writing out to the "NULL pointer."
Example:
main()
{
int fd = open("file", O_RDONLY);
null_vm();
read(fd, NULL, UINT_MAX);
unnull_vm();
close(fd);
return;
}
Thanks, Chenz
精彩评论