file (de)serialization, for array of simple structures

Not needed for IV..

// simple and fast (de)serialization to file given an array of structures

#include <fcntl.h>
#include <iostream>
using namespace std;
size_t const len=15;

struct A{
        int i1;
        char cstr[len];
        //string s4; //doesn't really work
        A(int i=999, string cs="default c-string", string s="default std::string"): i1(i){
                strncpy(cstr, cs.c_str(), len);
size_t const  cnt=2, siz=cnt * sizeof(A);
A arr[cnt], ar2[cnt];

char fname[] = "/tmp/,.dat";
int main() {
        arr[0]=A(1,  "grin", "backbone");
        arr[1]=A(2,  "frown", "try/except/else");

        int fd = open(fname, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
        write(fd, arr, siz);

        int fd2 = open(fname, O_RDONLY);
        read(fd2, ar2, siz);

        for (int idx = 0; idx < cnt; ++idx){
                A * tmp = ar2 + idx;
                cout<<tmp->i1<<" ; "<<tmp->cstr<<" ; "<<endl; //tmp->s4<<endl;

signed-char-array dump in hex digits: printf/cout

C++ code is convoluted. Must cast twice!
static const char tag[] = {'W', 2, 0xFF, 0x80};
// same output from c and c++: 57 02 ff 80
int main(void)
  cout << hex << setfill('0') ;
  for(int i = 0; i< sizeof(tag)/sizeof(char); ++i)
    printf("%02hhx ", tag[i]);
  printf ("\n");

  for(int i = 0; i< sizeof(tag)/sizeof(char); ++i)
    cout << setw(2) << (int)(unsigned char) tag[i] << " ";

Fwd: c++ portability, according to csdoctor

With java, i think we get binary portability in the vast majority of cases. With c++ you usually get good source code portability unless obvious non-portable libraries are used.

———- Forwarded message ———-
From: Qiang Wang
Subject: Re: c++ portability

I think the answer is YES. You have portability problem if you want to move from Windows to UNIX, especially when applications have GUI, hardware driver, network functions. However, for a simple C++
application, you can run on both Windows and UNIX with minimal change after compiling on both systems separately.


On Tue, Aug 24, 2010 at 9:01 AM, Bin TAN (Victor) wrote:
> Hi Friends,
> One potential IV question to verify hands-on c++ experience — Java has a
> big selling point “write once run anywhere”. If you wrote c++ in windows and
> run in unix, did you have portability problems?
> Is there any?

"uninitialized" is either a pointer or a primitive type

See also

1) uninitialized variable of primitive types — contains rubbish
2) uninitialized pointer — very dangerous.

We are treating rubbish as an address! This address may happen to be Inside or Outside this process’s address space.

Read/write on this dereferenced pointer can lead to crashes. See P161 [[understanding and using C pointers]].

There are third-party tools to help identify uninitialized pointers. I think it’s by source code analysis. If function3 receives an uninitialized pointer it would look completely normal to the compiler or runtime.

3) uninitialized class instance? Possible. Every class instance in c++ will have its memory layout well defined, though a field therein may fall into category 1) or 2) above.

4) uninitialized array of pointers could hold wild pointers

reliably convert Any c++ source to C

More than one person asked me “Can you compile a c++ app if you only have a c compiler?”

Some of the foremost experts on c/c++ compiler said on

If you mean “can you convert C++ source to C source, and run the C through a C compiler to get object code”, as a way to run C++ code on a system that has only a C compiler, yes it is possible to implement all of the features of ISO standard C++ by translation to C source code, and except for exception handling this produces object code with efficiency comparable to that of the code generated by a conventional compiler.

For exception handling, it is possible to do an implementation using setjmp/longjmp that is completely conformant, but the code generated will be 5-20% slower than code generated by a true c++ compiler.

Boost Any – barebones cheatsheet

* bite-sized introduction — best is P164 [[beyond c++ standard lib]]. Best 2nd intro is P165.
* Most essential operations on Any are

1) ctors — desposit into safebox
2) any_cast — check out using the “key”

* void pointer — Any is better than void pointers. (It’s good to know that void pointers meet the same basic requirement.)
* shared_ptr — to store pointers in an Any instance, use shared_ptr.
* STL — Any can be used with or without STL containers. First get a firm grip on one of them.

— P164 – without containers
myAny=3.281; // a double
//As shown you can put any object into the variable. To retrieve it, you specify the expected type.
any_cast (myAny);
any_cast (myAny); // would throw exception.

Q: big 3?
Q: can subclass?
Q: name a real use case
Q: this sounds powerful, convenient, too good to be true. What’s the catch? Why not widely used?