2011-09-28 113 views
0

我試圖用C++創建Bingo板,我總是發現細分故障。 Valgrind的沒有告訴我的遠不止這些:BingoBoard分割錯誤

==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) 

這裏是我的代碼:

#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; 

} 
+0

[注意...](http://www.google.com/search?q=bingo&tbm=pts) –

+1

您可能將所有'<='指定爲<<'。否則,你會有很多錯誤的錯誤。 – Mysticial

回答

1

的問題是:

delete bingo; 

其中bingo沒有指向從獲得的記憶使用new但指向字符串文字。

而且格式說明或參數的printf是不正確:

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

%s預計參數作爲一個char *,但你的參數類型是整數。請將格式說明符更改爲%c%d或更改參數。