为什么MySQL在其C API中抛出堆栈溢出异常? [英] Why is MySQL throwing a stack overflow exception in its C API?

查看:85
本文介绍了为什么MySQL在其C API中抛出堆栈溢出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚这一点。我复制了这个代码几乎是行(我进行了一些调整),将图像作为BLOB数据类型存储在MySQL数据表中。



I can''t seem to figure this out. I copied this code almost line for line (I made a few adjustments) to store an image as a BLOB data type in a MySQL data table.

void Mainframe::User::setImage(std::string ID, std::string dbProperty, std::string imagePath)
	{
		MYSQL *conn; 

		int len, size;
		char data[1000*1024];
		char chunk[2*1000*1024+1];
		char query[1024*5000];

		FILE *fp;

		conn = mysql_init(NULL);
		mysql_real_connect(conn, "localhost", "root", "password", "testdb", 0, NULL, 0);

		fp = fopen(imagePath.c_str(), "rb");
		size = fread(data, 1, 1024*1000, fp);

		mysql_real_escape_string(conn, chunk, data, size);

		std::string updateStr = ("update users set " + dbProperty + "='%s' where id=" + ID + ";"); 
		len = _snprintf(query, sizeof(updateStr.c_str()) + sizeof(chunk),updateStr.c_str(), chunk);
		
		mysql_real_query(conn, query, len);

		fclose(fp);
		mysql_close(conn);
	}





然而,每次运行时,我的应用程序立即崩溃,我得到:





Yet, every time it runs, I the app immediately crashes and I get:

Unhandled exception at 0x00ee2867 in MSQLClientTest.exe: 0xC00000FD: Stack overflow.





任何想法可能会出错?在我看到有人在网上提出建议后,我尝试将我的char更改为char *,但这只会导致更多涉及强制转换异常的问题。所以任何帮助将不胜感激。在此先感谢。



Any ideas what might be going wrong? I tried changing my "char"s to "char*"s after I saw someone suggest it online, but it only lead to more problems involving cast exceptions and whatnot. So any help would be greatly appreciated. Thanks in advance.

推荐答案

是的,那些char数组是巨大的。您需要在堆上分配数组,作为静态对象或增加堆栈的大小。在大多数机器上,默认情况下堆栈通常不超过1或2兆字节。



只需声明即可使它们静止:



Yeah, those char arrays are enormous. You either need to allocate the arrays on the heap, as static objects or increase the size of the stack. On most machines, the stack is not usually more than 1 or 2 megabytes by default.

You can make them static simply by declaration:

static char data[1000*1024];
static char chunk[2*1000*1024+1];
static char query[1024*5000];





要在堆上分配它们,您需要稍微重写一下代码。希望你知道如何做到这一点。



要增加堆栈大小,可以为主可执行项目执行此操作。对于每个版本的Visual Studio,详细信息都很简单但略有不同。



To allocate them on the heap, you will need to rewrite the code a little. Hopefully you know how to do that.

To increase the stack size, you do this for the main executable project. Details are straightforward but slightly different for each version of Visual Studio.


这篇关于为什么MySQL在其C API中抛出堆栈溢出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆