0
\$\begingroup\$

i am building a game with 100 levels and these levels are creatable by me and by the player with the level editor. For example there are some spikes and all their details are in a array of spike_struct.. (i hope you mean how i organize it) I just want to save the data to a file so

ofstream ofs("data.dat", ios::binary);
ofs.write((char*)&data, sizeof(data));

data is a struct containing

struct datastruct
{
    int spikes[100];
    // number of spikes in 100 levels
    int blocks[100];
    // number of blocks in 100 levels
    spike_struct spike[100][1000];
    //Data of spikes (1000 is the max of spikes in a level and 100 is the max number of levels
    block_struct block[100][1000];
    //same as above
}

This causes an error: SIGSEGV, Segmentation Fault. I got this in debug mode.

How can i avoid this?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I'm not sure but I would say that the SegFault is caused by the fact that you're not declaring your 2D array right at the start.

int spikes[100];

// Should be
int spikes[100][1000];

And the same for your blocks array.

For the moment, with:

spike_struct spike[100][1000];

You're trying to access at: spike[100][1000] that don't exists yet

\$\endgroup\$
3
  • \$\begingroup\$ I solved with spike_struct* spike = new spike_struct [1000]; \$\endgroup\$
    – manudicri
    Jul 26, 2017 at 10:26
  • \$\begingroup\$ @Manu12x Then accept the answer! And upvote it! \$\endgroup\$
    – Engineer
    Jul 26, 2017 at 10:29
  • 1
    \$\begingroup\$ spike_struct spike[100][1000]; is a valid multi-dimensional array declaration. I also don't understand why the spikes array should be made two dimensional since it's simply an integer that stores the amount of spikes in each level (one out of 100 levels). \$\endgroup\$
    – user35344
    Jul 26, 2017 at 10:34

Not the answer you're looking for? Browse other questions tagged .