LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-01-2011, 03:13 PM   #1
alireza976
LQ Newbie
 
Registered: May 2011
Posts: 13

Rep: Reputation: Disabled
Problem with allocation and initializing in dynamic 2dimentional array.


hello

I want to fetch all of the values from a table of ''mysql'' database and put them in a dynamic 2dimentional array, in this code.

Quote:
#include <stdio.h>
#include <mysql/mysql.h>
#include <mysql/errmsg.h>
#include <stdlib.h>
#include <string.h>


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.

char** array;
array=(char**)malloc(num_rows*sizeof(char));
while(j<(num_rows*num_fields))
{
array[j]=(char*)malloc(60*sizeof(char));
j++;
}

while((row = mysql_fetch_row(result)))
{
for(i = 0; i < num_fields; i++)
{
strcpy(array[u],row[i]);
//sprintf(array[u],"%s",row[i]);
printf("[%s] ",array[u]);
u++;
}
}
printf("\n\nFetched ok\n\n");
printf("\n\n%d\n\n",u);
mysql_free_result(result);
}
mysql_close(conn);
}
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.
 
Old 07-01-2011, 06:30 PM   #2
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 237

Rep: Reputation: 22
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.
 
Old 07-02-2011, 12:43 PM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Fix this:
Code:
char** array;
array=(char**)malloc(num_rows*sizeof(char));
Should be:
Code:
char** array;
array=(char**)malloc(num_rows*sizeof(char*));   // you need to allocate pointers, not chars
...
or since you are using C-99
Code:
char** array=(char**)malloc(num_rows*sizeof(char*));
...
Feel free to sprinkle some white-space between operators to make your code easier to read. Also, post code using CODE tags, not QUOTE tags.

P.S. Don't use strcpy(); use strncpy() instead, so that you can specify the size of the destination. The size should be N-1 bytes, not N!
 
Old 07-03-2011, 03:25 AM   #4
alireza976
LQ Newbie
 
Registered: May 2011
Posts: 13

Original Poster
Rep: Reputation: Disabled
ok

Thanks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] dynamic memory allocation in assembly hda7 Linux - Software 9 10-28-2009 08:25 AM
Question about page allocation when program initializing and running? valpa Linux - Kernel 2 08-18-2008 11:26 PM
Dynamic Memory Allocation jvff Programming 1 09-05-2005 05:01 AM
pointers and dynamic memory allocation deveraux83 Programming 2 01-24-2004 10:35 AM
Dynamic Memory Allocation query dhanakom Programming 2 07-21-2003 02:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration