i want to understand how dynamic memory allocation done for result of sqlite3_get_table with malloc ?
-
@Qt-embedded-developer
This is absolutely basic C! If youmalloc(), what function do you have to call to free the allocated memory?@JonB i have to call free() to deallocate memory.
void free(void *ptr);
i have to free recordset memory inside below part else condition
if (ret != SQLITE_OK) { printf("SQL error: %s\n", err_messg); sprintf(RET, "%d", ret); // InsertErrorLog // if (strcmp(strErrorLogDescription, "") != 0) { // InsertErrorlog("", 0, 0, err_messg, strErrorLogDescription); // } success = 0; } else { resultSet_Table.rows = nrow; resultSet_Table.cols = ncol; resultSet_Table.recordset = recordset; success = 1; } -
@JonB i have to call free() to deallocate memory.
void free(void *ptr);
i have to free recordset memory inside below part else condition
if (ret != SQLITE_OK) { printf("SQL error: %s\n", err_messg); sprintf(RET, "%d", ret); // InsertErrorLog // if (strcmp(strErrorLogDescription, "") != 0) { // InsertErrorlog("", 0, 0, err_messg, strErrorLogDescription); // } success = 0; } else { resultSet_Table.rows = nrow; resultSet_Table.cols = ncol; resultSet_Table.recordset = recordset; success = 1; }@Qt-embedded-developer
So write the code to callfree()s corresponding to where you did yourmalloc()s. What else is there to ask/say? -
@Qt-embedded-developer
So write the code to callfree()s corresponding to where you did yourmalloc()s. What else is there to ask/say?i have allocated memory like below so to where and how to write free() for this all allocated memory.
recordset = (char ***) malloc(nrow * sizeof (char **)); for (count = ncol; count < ((nrow + 1) * ncol); count++) { printf("Enter in LOOP\n"); recordset[i] = (char **) malloc(ncol * sizeof (char *)); for (j = 0; j < ncol; j++) { printf("%s ", result[count]); recordset[i][j] = (char *) malloc((strlen(result[count]) + 1)); strcpy(recordset[i][j], result[count]); if (j != (ncol - 1)) count++; } i++; printf("\n"); }
in else part if i wrote code like below. is it right ?
```
free(recordset);
for (count = ncol; count < ((nrow + 1) * ncol); count++) {printf("Enter in LOOP\n"); free(recordset[i]); for (j = 0; j < ncol; j++) { free(recordset[i][j]); } i++; printf("\n"); } -
i have allocated memory like below so to where and how to write free() for this all allocated memory.
recordset = (char ***) malloc(nrow * sizeof (char **)); for (count = ncol; count < ((nrow + 1) * ncol); count++) { printf("Enter in LOOP\n"); recordset[i] = (char **) malloc(ncol * sizeof (char *)); for (j = 0; j < ncol; j++) { printf("%s ", result[count]); recordset[i][j] = (char *) malloc((strlen(result[count]) + 1)); strcpy(recordset[i][j], result[count]); if (j != (ncol - 1)) count++; } i++; printf("\n"); }
in else part if i wrote code like below. is it right ?
```
free(recordset);
for (count = ncol; count < ((nrow + 1) * ncol); count++) {printf("Enter in LOOP\n"); free(recordset[i]); for (j = 0; j < ncol; j++) { free(recordset[i][j]); } i++; printf("\n"); }@Qt-embedded-developer
No, that is not right.You are freeing the items in the same order as you allocated them. Once you have done
free(recordset)you must not then dofree(recordset[i]), and once you have donefree(recordset[i])you must not then dofree(recordset[i][j]).Please stop and think about it/why. This is basic C.
free()things in the opposite order to how you built your structure withmalloc(). Do notfree()something if you then go into it to access/free()something else. -
@Qt-embedded-developer
No, that is not right.You are freeing the items in the same order as you allocated them. Once you have done
free(recordset)you must not then dofree(recordset[i]), and once you have donefree(recordset[i])you must not then dofree(recordset[i][j]).Please stop and think about it/why. This is basic C.
free()things in the opposite order to how you built your structure withmalloc(). Do notfree()something if you then go into it to access/free()something else.@JonB means i need to write code like below:
int i = 0; for (count = ncol; count < ((nrow + 1) * ncol); count++) { printf("Enter in LOOP\n"); for (j = 0; j < ncol; j++) { free(recordset[i][j]); } free(recordset[i]); i++; printf("\n"); } free(recordset);And can you elaborate below sentence:
Do not free() something if you then go into it to access/free() something else. -
@JonB means i need to write code like below:
int i = 0; for (count = ncol; count < ((nrow + 1) * ncol); count++) { printf("Enter in LOOP\n"); for (j = 0; j < ncol; j++) { free(recordset[i][j]); } free(recordset[i]); i++; printf("\n"); } free(recordset);And can you elaborate below sentence:
Do not free() something if you then go into it to access/free() something else.@Qt-embedded-developer
In principle you at least now have thefree()s in the right order.I don't know why you have an
iand acount, whether you go through the right items, and why you use(nrow + 1)(the+ 1). But that's a coding issue for you to sort out. I have answered your questions, over to you to implement now.Do not free() something if you then go into it to access/free() something else.
Exactly what I wrote; If you go e.g.
free(recordset); free(recordset[i]); // or anything to `recordset[i]`think about what state
recordset[i]might be in after you havefree()drecordset. -
@Qt-embedded-developer
In principle you at least now have thefree()s in the right order.I don't know why you have an
iand acount, whether you go through the right items, and why you use(nrow + 1)(the+ 1). But that's a coding issue for you to sort out. I have answered your questions, over to you to implement now.Do not free() something if you then go into it to access/free() something else.
Exactly what I wrote; If you go e.g.
free(recordset); free(recordset[i]); // or anything to `recordset[i]`think about what state
recordset[i]might be in after you havefree()drecordset.@JonB said in i want to understand how dynamic memory allocation done for result of sqlite3_get_table with malloc ?:
over to you to implement now
Hi
here below i allocated memory for string element in array index
recordset[i][j] = (char *) malloc((strlen(result[count]) + 1)); -
Yeah good C exercise :)
int rows=2, cols=3; const char* R1[]={"r1 c1","r1 c2","r1,c3"}; const char* R2[]={"r2 c1","r2 c2","r2,c3"}; const char** data[]={ R1,R2}; char*** recordset; // array of pointers (rows) to array of pointers (cols) recordset=(char***) malloc(sizeof(char***)*rows); for (int r = 0; r < rows; ++r) { recordset[r]=(char**) malloc(sizeof(char**)*cols); for (int c = 0; c < cols; ++c) { recordset[r][c]=(char*) malloc(sizeof(char)*(strlen(data[r][c])+1)); strcpy(recordset[r][c],data[r][c]); } } for(int r=0 ; r<rows; r++) std::cout<<recordset[r][0]<<", "<<recordset[r][1]<<", "<<recordset[r][2]<<", "<<std::endl; // delete for (int r = 0; r < rows; ++r) { for (int c = 0; c < cols; ++c) { free(recordset[r][c]); } free(recordset[r]); } free(recordset); -
Yeah good C exercise :)
int rows=2, cols=3; const char* R1[]={"r1 c1","r1 c2","r1,c3"}; const char* R2[]={"r2 c1","r2 c2","r2,c3"}; const char** data[]={ R1,R2}; char*** recordset; // array of pointers (rows) to array of pointers (cols) recordset=(char***) malloc(sizeof(char***)*rows); for (int r = 0; r < rows; ++r) { recordset[r]=(char**) malloc(sizeof(char**)*cols); for (int c = 0; c < cols; ++c) { recordset[r][c]=(char*) malloc(sizeof(char)*(strlen(data[r][c])+1)); strcpy(recordset[r][c],data[r][c]); } } for(int r=0 ; r<rows; r++) std::cout<<recordset[r][0]<<", "<<recordset[r][1]<<", "<<recordset[r][2]<<", "<<std::endl; // delete for (int r = 0; r < rows; ++r) { for (int c = 0; c < cols; ++c) { free(recordset[r][c]); } free(recordset[r]); } free(recordset); -
@mpergand said in i want to understand how dynamic memory allocation done for result of sqlite3_get_table with malloc ?:
recordset[r][c]=(char*) malloc(sizeof(char*)); strcpy(recordset[r][c],data[r][c]);Ummm, really...? ;-)