views:

185

answers:

7

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 /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.

+2  A: 

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?

Chris Hafey
Actually no, you can't do ANYTHING in C or C++. Specifically, you can't rewire the hardware, and that's what would be required to create an infinitely long buffer that doesn't overlap valid memory.
Ben Voigt
+1  A: 

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.

epatel
+1  A: 

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.

zvrba
A: 

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.

Potatoswatter
A: 

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.

Ben Voigt
A: 

Maybe you can seek to where you're trying to go.

Ken Bloom
A: 

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

Crazy Chenz