The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.
float* t = (float*)calloc(100, sizeof(float));
free(t);
t = (float*)calloc(100, sizeof(float));
free(t);
works fine.float** v;
v = Get_2D_FloatArray(10, 10);
Free_2D_FloatArray(v);
v = Get_2D_FloatArray(10, 10);
Free_2D_FloatArray(v);
int** Get_2D_Int-Array(int dim1, int dim2) {
int** v;
v = (int**)calloc(dim2, sizeof(int*));
v[0] = (int*)calloc(dim1*dim2, sizeof(int));
int i=0;
for (i=1; i<dim1; i++) {
v[i] = v[i-1] + dim2;
}
return v;
}
void Free_2D_int_Array(int** v) {
free(v[0]);
free(v);
v[0] = NULL;
v = NULL;
}
void Free_2D_int_Array(int** v) {
free(v[0]); // OK
free(v); // you free v here
v[0] = NULL; // now you are dereferencing v after freeing it
v = NULL;
}
int** Get_2D_Int-Array(int dim1, int dim2) {
int** v;
v = (int**)calloc(dim2, sizeof(int*));
if (v == NULL) {
return NULL;
}
v[0] = (int*)calloc(dim1*dim2, sizeof(int));
if (v[0] == NULL) {
return NULL;
}
int i=0;
for (i=1; i<dim1; i++) {
v[i] = v[i-1] + dim2;
}
return v;
}
void Free_2D_int_Array(int** v) {
free(v[0]);
free(v);
v[0] = NULL;
v = NULL;
}
#include <stdlib.h>
int** Get_2D_Int_Array(int dim1, int dim2) {
int** v;
v = (int**)calloc(dim2, sizeof(int*));
v[0] = (int*)calloc(dim1*dim2, sizeof(int));
int i=0;
for (i=1; i<dim1; i++) {
v[i] = v[i-1] + dim2;
}
return v;
}
void Free_2D_int_Array(int** v) {
free(v[0]);
v[0] = NULL;
free(v);
v = NULL;
}
int main()
{
int** v;
v = Get_2D_Int_Array(10, 10);
Free_2D_int_Array(v);
v = Get_2D_Int_Array(10, 10);
Free_2D_int_Array(v);
}
v = Get_2D_Int_Array(1000, 10);
In Get_2D_Int_Array, dim1 is 1000 and dim2 is 10. Here you are allocating a region suitable for 10 pointers. v = (int**)calloc(dim2, sizeof(int*));
But then here you are setting 1000 pointers: for (i=1; i<dim1; i++)
v[i] = v[i-1] + dim2;
which goes beyond the allocated region.
v = Get_2D_Int_Array(10, 10);
Free_2D_int_Array(v);
v = Get_2D_Int_Array(10, 1000);
Free_2D_int_Array(v);
I did notice one other interesting syntactical issue. In Free_2D_int_Array, you set v to NULL; but v is an auto variable in Free_2D_int_Array's stack. So, when you return to main, the v there should not be affected.int** Get_2D_Int-Array(int dim1, int dim2) {
int** v;
v = (int**)calloc(dim1, sizeof(int*));
if (v == NULL) {
return NULL;
}
v[0] = (int*)calloc(dim1*dim2, sizeof(int));
if (v[0] == NULL) {
return NULL;
}
int i=0;
for (i=1; i<dim2; i++) {
v[i] = v[i-1] + dim2;
}
return v;
}
void Free_2D_int_Array(int*** v) {
free(*v[0]);
free(*v);
*v[0] = NULL;
*v = NULL;
}
int** Free_2D_int_Array(int** v) {
free(v[0]);
v[0] = NULL;
free(v);
v = NULL;
return v;
}
// main():
...
v = Free_2D_int_Array(v); // after return from function, v will be NULL
}
As mentioned earlier, I like to think of a 2d array as a matrix having nRows and nCols (replacing dim1 and dim2, respectively).If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
How to programmatically differentiate between C and Java | 10 | 204 | |
Problem to ASCII | 1 | 194 | |
Arduino EDI - Programming language | 3 | 103 | |
Volatile formula's, speeding up calculations and match first and last name in separate columns to return value from a third. | 6 | 80 |
Join the community of 500,000 technology professionals and ask your questions.