Thread: Cutting out the middle man with stream writing

  1. #1
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    139

    Cutting out the middle man with stream writing

    I'm trying to use output streams to output biunary data, with some of that data being private class variables. I'm atrying to code it in a way where I can cut out having to copy the values to local variables before doing so, because it might be more efficient

    This is the only way I know that works:

    Code:
    class A {
    private:
    	int _x;
    public:
    	void setX(int val) { _x = val; }
    	int x() { return _x; }
    };
    
    int main()
    {
    	A a;
    	a.setX(89);
    	int x = a.x(); // trying to see if I can avoid making this copy
    	file.write(reinterpret_cast<const char*>(&x), sizeof(int));
    	return 0;
    }
    I've tried numerous things without success:
    - returning int & doesn't work like this: char * p = reinterpret_cast<char*>(a.x()) because C++ interprets it as a value for some reason
    - returning const int * doesn't work either

    Is the only way to do this to make a wrapper function of some sort, expose the private vars, or copy them to local vars?

  2. #2
    Guest
    Guest
    How about:
    Code:
    const int&  x() const { return _x; }
    Code:
    file.write(reinterpret_cast<const char*>(&a.x()), sizeof(int));
    That would work, no?

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Add a write_x function to A.

  4. #4
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    139
    Quote Originally Posted by Guest View Post
    How about:
    Code:
    const int&  x() const { return _x; }
    Code:
    file.write(reinterpret_cast<const char*>(&a.x()), sizeof(int));
    That would work, no?
    It does! Thanks!

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Chris87 View Post
    It does! Thanks!
    I wonder if that's still making an implicit copy.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're worried about making a copy of an int? If the compiler didn't optimize that, that's just a register transfer which is done in a singe clock cycle--super fast! In fact, it's likely to be more slow to make a reference due to the possibility of a cache miss.

    You shouldn't worry about performance or efficiency unless you can prove it's slow in your program. Binary files have their problems, some of them being non-portable and more complex in getting right.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's a waste of effort worrying about a few nS here or there when accessing memory, when disk seek times are still measured in milliseconds (that's millions of times slower).

    Worry instead about making a clean interface to each class to serialise (and de-serialise) itself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing number in the middle of hollow rectngle
    By Ph0x in forum C Programming
    Replies: 15
    Last Post: 10-05-2014, 05:47 AM
  2. cutting up strings
    By pastitprogram in forum C++ Programming
    Replies: 10
    Last Post: 11-09-2008, 06:18 PM
  3. Mic cutting out
    By cerin in forum Tech Board
    Replies: 13
    Last Post: 11-19-2006, 11:12 PM
  4. cutting a string
    By keeper in forum C++ Programming
    Replies: 2
    Last Post: 05-24-2006, 06:43 PM
  5. cutting lines more when len is > 15
    By xion in forum C Programming
    Replies: 7
    Last Post: 07-24-2003, 07:14 PM

Tags for this Thread