Core dump after using memcpy

The core dump happens on the first step of "delete [] game.board[i];" (line 65)... This only happens when memcpy() (line 77) is used. What I am trying to do is copy the contents of the c-string const to a dynamic 2d array. The point is that there will be multiple levels of potentially different sizes put in as the game.board. I had initially tried game.board[0] = "contents of line 0" ... game.board[23] = "contents of line 23", but that gave a compiler warning. Strangely this works perfect using MinGW under windows, but I need this to also work with linux.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    const char* level1[23] =
    {
    "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    "X                                                               X",
    "X  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     X",
    "X  X K                          X            D               X  X",
    "X  XXXXXXXXXXXXXXXXXXXXXX  XXXXXXXXX  XXXXXXXXXXXXXXXXXXXXX  X  X",
    "X  X  X        X                  X                   XXXXX  X  X",
    "X  X   XXXXX   X  XXXXXXXXXXXXXX  XXXXXXXX  XXXXXXXXXXX      X  X",
    "X  X        X  X        D      X  X      X   X        X  XXXXX  X",
    "X  X  XXXX  X  X  XXXXXXXXXX   X     X          X  XXXX      X  X",
    "X        X        X   X  E X  XXXXXXXXX  XXXXXXXX  X  XXXXX  X  X",
    "X  XXXXXXXXXXXXXXXX   D    X  XXK      X        X  X  X     KX  X",
    "X  X     X     X KX   XXXXXX  XXXXXX  XXXXXXXX  X  X  X  XXXXXXDX",
    "X  X  X  X  X  X  X           XXX  X            X  X  X  D   X  X",
    "X  X  X  X  X  X  XXXXXXXXXXXXXXX  XXXXXXXXXXXXXX  X  XXXXX  X  X",
    "X  X  X  X  X  X  X   X               X           KX  X      X  X",
    "X  X  X  X  X  X  X   X  XXXXXXXXXXX  X  XXXXXXXXXXX  X  XXXXX  X",
    "X  X  X  X  X  X  X   X  X     X  KX  X  X            X      X  X",
    "X     X  X  X  X  X      X  X     XX  X  X     XXXXXXXXXXXXX X  X",
    "X  X  X  X  X  X  X   X  X  XXXXXXXX  X  X      X     X      X  X",
    "X  X  X     X     X   X  X         X  X  X   X     X         X  X",
    "X  X  XXXXXXXXXXXXX   X  XXXXXXXX  X  X  X  XXXXXXXXXXXXXXXXXX  X",
    "XS X                                                            X",
    "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    };
    
    // Player structure
    struct playerinfo
    {
        int x;      // X position
        int y;      // Y position
        int keys;   // Number of keys the player has
    };
    
    // Current game info structure
    struct gameinfo
    {
        char** board;       // The maze array
        int maxx;           // Max size of the maze array X coordinate
        int maxy;           // Max size of the maze array X coordinate
        int level;          // The level of the maze
        playerinfo player;        // Link to player data struct
    };
    
    // Initializes the dynamic array that stores the maze
    char** initboard(gameinfo game)
    {
        char** matrix = new char*[game.maxx];
    
        for (int i=0; i<game.maxx; i++)
            matrix[i] = new char[game.maxy];
    
        return matrix;
    }
    
    void delboard(gameinfo game)
    {
        for (int i=0;i<game.maxx;i++)
            delete [] game.board[i];
        delete [] game.board;
    }
    
    int main()
    {
        gameinfo game;
    
        game.maxx=65;
        game.maxy=23;
    
        game.board = initboard(game);
        memcpy(game.board,level1,sizeof(level1));
    
        game.board = initboard(game);
        delboard(game);
    }
Last edited on
level1 is an array of 23 pointers. game.board is an array of 65 pointers. memcpy() deals with blocks of memory. If you memcpy() an array of pointers onto another, you're overwriting the pointers in the destination. When it's time to free the board, game.board then starts with 23 pointers to memory that was never dynamically allocated.
Also when you will try to modify your board later, you will modify string literals, which is illegal too.
Either allocate and copy string manually, use 1D representation of 2D array, or use some sort of C++ container which manages memory automatically

but that gave a compiler warning
Let me guess, "assigning string literal to the non-const pointer"?

Strangely this works perfect using MinGW under windows
Blind luck. It can change on next recompiling.
Registered users can post here. Sign in or register to post.