Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C++
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.


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.
Posted

1 solution

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:

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


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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900