0

I'm trying to directly cast a stream of data into a structure that actually has a variable number of other structures as members. Here's an example:

    struct player
    {
        double lastTimePlayed;
        double timeJoined;
    };

    struct team
    {
        uint32_t numberOfPlayers;
        player everyone[];
    };

then I call:

    team *myTeam = (cache_team*)get_stream();

This should work like some kind of serialization, I know my stream is structured exactly as represented above, but I have the problem of the numberOfPlayers being a variable.

My stream starts with 4 bytes representing the number of players of the team, then it contains each player (in this case, each player has only lastTimePlayed and timeJoined).

The code posted seems to be working, I still get a warning from the compiler because of the default assignment and copy constructors, but my question is it it's possible to do this some other way, a better way.

BTW, my stream is actually a direct mapping to a file, and my goal is to use the structure as if it was the file itself (that part is working properly).

megamau
  • 33
  • 5

3 Answers3

0

uint32_t is 4 bytes. If it starts with 8 bytes you want a uint64_t.

If you want to get rid of the warning you can make the default copy and assignment private:

struct team {
    // ...
private:
    team(const team &);
    team &operator=(const team &);
};

Since you'd probably want to pass everything by pointer anyways it'll prevent ever doing an accidental copy.

Casting the mapped pointer to the struct is probably the easiest way. The big thing is to just make sure everything is lining up correctly.

scaryrawr
  • 777
  • 4
  • 12
  • you're right uint32_t is 4bytes :) just edited my question. About the question itself: yes, this way I get rid of the warning, and is in fact a way of doing it. I'm sure I'm not going to instantiate or copy the structure anyway. – megamau Jul 10 '13 at 22:30
0

Visual Studio 2012 gives the following:

warning C4200: nonstandard extension used : zero-sized array in struct/union
A structure or union contains an array with zero size.
Level-2 warning when compiling a C++ file and a Level-4 warning when compiling a C file.

This seems to be a legitimate message. I would recommend you to modify your struct to:

struct team
{
        uint32_t numberOfPlayers;
        player everyone[1];
};

Such definition is less elegant, but the result will be basically the same. C++ is not checking the value of indexes. Tons of code are using this.

New development should avoid the "array size violations" where possible. Describing external structures in this way is acceptable.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
0

Both scaryrawr's solution, and yours do the trick, but I was in fact searching for another way.

I in fact did find it. I used an uint32_t everyonePtr instead of the array, then I will convert the uint32_t to a pointer using a reinterpret_cast like this:

player *entries = reinterpret_cast<player*>(&team->everyonePtr);

then my mapping will work as expected, and I think it's easier to understand than the array[1] or even the empty one. Thank you guys.

megamau
  • 33
  • 5