BingoBoard 分段错误
我尝试用 C++ 创建的宾果板不断出现分段错误。 Valgrind 没有告诉我更多的事情:
==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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于:
其中
bingo
不是指向通过使用new
获得的内存,而是指向字符串文字。此外,格式说明符或 printf 的参数不正确:
%s
期望参数为char *
但您的参数类型是整数。将格式说明符更改为%c
或%d
或者更改参数。The problem is with:
where
bingo
is not pointing to memory obtained from use ofnew
but is pointing to a string literal.Also the format specifier or the argument to printf is incorrect:
%s
expects argument to be achar *
but your argument type is integer. Either change the format specifier to say%c
or%d
OR change the argument.