prolem in calling mysql C API in C++ project

M

Manpreet

Hi all,

I am using mysql C API in my C++ project. I am using
mysql-4.1.4-gamma and gcc 3.2.2.

Problem with this code is that when add_data(i) is called second time
SEGMENTATION FAULT occurs at mysql_real_query(). First time it
executes the add_data() function successfully.


My Code is

MYSQL db is declared in main.cpp and database connection is also made
in main.cpp.

//main.cpp
add_carrier();

//carrier.cpp
add_carrier()
{
for(i=0;i<3;i++)
{
add_data(i);
}
}
//country.cpp
extern MYSQL db;
add_data(int i)
{
char sqlquery[500];
sprintf(sqlquery,"Select rateid from RATES where carrierid=%d",i);

//*** CAUSES SEGMENTATION FAULT WHEN add_data() IS CALLED SECOND
TIME
mysql_real_query(&db,sqlquery,strlen(sqlquery)); //SEGMENTATION
FAULT


...
...
...

}

Thanx in advance.

Regards,
Manpreet Dhanjal
 
V

Victor Bazarov

[answering in comp.lang.c++]
Manpreet said:
I am using mysql C API in my C++ project. I am using
mysql-4.1.4-gamma and gcc 3.2.2.

Problem with this code is that when add_data(i) is called second time
SEGMENTATION FAULT occurs at mysql_real_query(). First time it
executes the add_data() function successfully.


My Code is

MYSQL db is declared in main.cpp and database connection is also made
in main.cpp.

//main.cpp
add_carrier();

//carrier.cpp
add_carrier()
{
for(i=0;i<3;i++)
{
add_data(i);
}
}
//country.cpp
extern MYSQL db;
add_data(int i)
{
char sqlquery[500];
sprintf(sqlquery,"Select rateid from RATES where carrierid=%d",i);

//*** CAUSES SEGMENTATION FAULT WHEN add_data() IS CALLED SECOND
TIME
mysql_real_query(&db,sqlquery,strlen(sqlquery)); //SEGMENTATION
FAULT


...
...
...

}


The code you posted is neither C nor C++. If I add all the necessary
elements to make it compilable, it still contains plenty of "..." which
doesn't help.

Segmentation faults are usually due to accessing memory using invalid
pointers. Either some pointer gets stumped over, or you cross some array
boundary, or both. In the presented code there is no problem, so I say
it's somewhere in the code you didn't post.

Run your program under a debugger and examine the cause of the seg fault.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top