开发者

BingoBoard Segmentation Fault

I keep getting a segmentation fault with this Bingo Board I'm trying to create in C++. Valgrind didn't tell me much more than this:

==2734== Memcheck, a memory error detector
==2734== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==2734== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==2734== Command: ./Bingo
==2734== 
==2734== Invalid read of size 1
==2734==    at 0x5D0A436: vfprintf (vfprintf.c:1620)
==2734==    by 0x5DBF8ED: __printf_chk (printf_chk.c:37)
==2734==    by 0x400FC0: generate(int, int) (in /home/holland/Code/cpp/Bingo-build-desktop-Desktop_Qt_4_7_4_for_GCC__Qt_SDK__Release/Bingo)
==2734==    by 0x400B69: main (in /home/holland/Code/cpp/Bingo-build-desktop-Desktop_Qt_4_7_4_for_GCC__Qt_SDK__Release/Bingo)
==2734==  Address 0x62 is not stack'd, malloc'd or (recently) free'd
==2734== 
==2734== 
==2734== Process terminating with default action of signal 11 (SIGSEGV)
==2734==  Access not within mapped region at address 0x62
==2734==    at 0x5D0A436: vfprintf (vfprintf.c:1620)
==2734==    by 0x5DBF8ED: __printf_chk (printf_chk.c:37)
==2734==    by 0x400FC0: generate(int, int) (in /home/holland/Code/cpp/Bingo-build-desktop-Desktop_Qt_4_7_开发者_开发技巧4_for_GCC__Qt_SDK__Release/Bingo)
==2734==    by 0x400B69: main (in /home/holland/Code/cpp/Bingo-build-desktop-Desktop_Qt_4_7_4_for_GCC__Qt_SDK__Release/Bingo)
==2734==  If you believe this happened as a result of a stack
==2734==  overflow in your program's main thread (unlikely but
==2734==  possible), you can try to increase the size of the
==2734==  main thread stack using the --main-stacksize= flag.
==2734==  The main thread stack size used in this run was 8388608.
 ==2734== 
==2734== HEAP SUMMARY:
==2734==     in use at exit: 86,091 bytes in 276 blocks
==2734==   total heap usage: 2,819 allocs, 2,543 frees, 246,187 bytes allocated
==2734== 
==2734== LEAK SUMMARY:
==2734==    definitely lost: 0 bytes in 0 blocks
==2734==    indirectly lost: 0 bytes in 0 blocks
==2734==      possibly lost: 0 bytes in 0 blocks
==2734==    still reachable: 86,091 bytes in 276 blocks
==2734==         suppressed: 0 bytes in 0 blocks
==2734== Rerun with --leak-check=full to see details of leaked memory
==2734== 
==2734== For counts of detected and suppressed errors, rerun with: -v
==2734== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)

Here's my code:

#include <vector>
#include <iostream>
#include <algorithm>
#include "board.h"

#define MIN 0
#define MAX 15

using std::vector;
using std::random_shuffle;
using std::cout;
using std::endl;

void generate(int numRows, int numColumns) {

    std::vector<int> b;
    std::vector<int> i;
    std::vector<int> n;
    std::vector<int> g;
    std::vector<int> o;

    char * bingo = "bingo";

    for(int index = MIN; index <= 15; index++)
    {
        b.push_back(index);
        i.push_back(index + 15);
        n.push_back(index + 30);
        g.push_back(index + 45);
        o.push_back(index + 60);
    }

    std::random_shuffle(b.begin(), b.end());
    std::random_shuffle(i.begin(), i.end());
    std::random_shuffle(n.begin(), n.end());
    std::random_shuffle(g.begin(), g.end());
    std::random_shuffle(o.begin(), o.end());

    for (int i = 0; i <= 5; i++)
    {
        printf(" %s ", bingo[i]);
    }

    for (int j = 0; j <= 15; j++)
    {
        printf(" %d %d %d %d %d \n", b.back(), i.back(), n.back(), g.back(), o.back());
        //b.pop_back();
        //i.pop_back();
        //n.pop_back();
        //g.pop_back();
        //o.pop_back();
    }

    delete bingo;

}


The problem is with:

delete bingo;

where bingo is not pointing to memory obtained from use of new but is pointing to a string literal.

Also the format specifier or the argument to printf is incorrect:

printf(" %s ", bingo[i]);

%s expects argument to be a char * but your argument type is integer. Either change the format specifier to say %c or %d OR change the argument.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜