Minimally Verifiable Source Code - I think.I am trying to use dynamic allocation of a pointer array in a function to create a double double pointer array. When I use the code outside a struct reference it works. When I try to reference it through a pointer, it doesn't. What am I doing wrong?
This code works in GCC 4.9.2
gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.9/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.9.2-10ubuntu13' --with-bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.9 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix
int main(int argc, char **argv)
{
double ** array;
double row[6] = { 1.2, 2.3, 3.0, 4, 5, 6 };
int i;
int j;
array = (double **) malloc( 50* sizeof(double * ) );
for ( i = 0; i < 50; i++)
{
array[i] = (double *) malloc( 6 * sizeof(double ) );
}
for (j =0; j < 50; j++)
for ( i = 0; i < 6; i++)
{
array[j][i] = row[i];
}
for (j =0; j < 50; j++)
{
for (i = 0; i < 6; i++ )
{
printf("%f ", ( array[0][i]) );
}
printf("\n ");
}
exit(0); // Use exit() to exit a program, do not use 'return' from main()
}
But this code doesn't:
double ** create_array( unsigned int length, unsigned int row)
{
double ** array;
int i;
array = (double **) malloc( length * sizeof(double * ) );
for ( i = 0; i < length; i++)
{
array[i] = (double *) malloc( row * sizeof(double ) );
}
return array;
}
I placed the double double pointer array inside a struct
typedef struct
{
// the data area
double ** array;
// the number of rows in the entire entry
unsigned int length;
// the size of the double entity of array size
unsigned int rowsize;
// the current starting point
int start;
// the current ending point
int end;
// the amount of the last write action
// assumes the number of write in order
unsigned int last_write[100];
// the number of writes
unsigned int write_count;
} DRingBuffer;
And then access through following function calls like this:
int DRingBuffer_printrow(DRingBuffer *buffer, unsigned int row )
When I call the function and then try to access the members it seg faults.
printf("%f ", (buffer->array[3][2]));
This is a lot of code but someone asked for it.
DRingBuffer *DRingBuffer_create(unsigned int length, unsigned int row )
{
int i =0;
DRingBuffer *buffer = malloc( sizeof(DRingBuffer) );
buffer->length = length;
buffer->rowsize = row;
buffer->start = 0;
buffer->end = 0;
buffer->array = create_array( length, row);
if ( buffer->array <= 0 )
{
printf("ERROR: allocating arrays");
exit( -1);
}
return buffer;
}