Problem with allocation and initializing in dynamic 2dimentional array.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
int main(int argc, char **argv)
{
MYSQL *conn;
MYSQL_RES *result;
MYSQL_ROW row;
char myquery[300]="select * from pageinfo;",
int i,
x,
j=0,
num_fields,
num_rows=0,
u=0;
if((conn=mysql_init(NULL))==NULL)
{
printf("\nFailed to initate MySQL connection");
// exit(1);
}
if (!mysql_real_connect(conn,"127.0.0.1","root","1","aaa",0,"/var/run/mysqld/mysqld.sock",0))
{
printf( "Failed to connect to MySQL: Error: %s\n", mysql_error(conn));
// exit(1);
}
printf("\nLogged on to database sucessfully\n\n");
printf("\n\n%s\n\n", myquery);
if (mysql_query(conn,myquery))
{
printf("\n\nQuery error.\n\n");
}
else
{ //Query OK.
result = mysql_store_result(conn); //unsigned int num_fields.
num_rows = mysql_num_rows(result); //Returns the number of rows in the result set.
num_fields = mysql_num_fields(result); //Returns the number of columns in a result set.
Reading information process does work correctly but when program get the second row, there is a ''segmentation fault'' error and it causes the program to stop.
The trouble can be either memory allocation or entering the informaition to array.
Could anybody help me to solve this problem?
Thanks
Last edited by alireza976; 07-01-2011 at 03:17 PM.
To use an array of records you should specify a structure that defines that record (mybe a whole row if records of a row don't have same length) and make thereof an array.
And then it is best to trop that struct and make a class for that what you want to do, because variable arrays are name space depending (heap).
Allocate a memory as a whole block not only parts of it, because arrays can only access a continuous memory block (''segmentation fault'') and calling malloc a second time could separate that memory blocks from the other.
Variable allocation:
1)Current allocation is the allocation before + additional needed memory.
2)Copy contens of the old allocation to the new allocation + new record.
3)Free old allocation.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.