我在一些简单的 C++ 代码上收到“分段错误(核心转储)”错误。当我使用变量结果时,错误会发生。在这种情况下,什么会导致这个问题?我知道有些东西超出了范围,但结果怎么可能呢?数据库中只有 2 个项目。
#include <iostream>
#include <boost/variant.hpp>
#include <mysql/mysql.h>
#include <mysql_connection.h>
using namespace std;
const char *server = "localhost";
const char *user = "XXXX";
const char *password = "XXXX";
const char *database = "XXXX";
int main()
{
MYSQL *conn;
MYSQL_RES *result;
MYSQL_ROW row;
int num_fields = 0;
conn = mysql_init(NULL);
mysql_real_connect(conn, server,user, password, database, 0, NULL, 0);
mysql_query(conn, "SELECT * FROM `users` WHERE `id` = '5'");
result = mysql_store_result(conn);
num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result)))
{
cout << "Spam ";
}
mysql_free_result(result);
mysql_close(conn);
}
错误:
Segmentation fault (core dumped)
Process returned 139 (0x8B) execution time : 0.255 s
Press ENTER to continue.