Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C programming questions from a java programmer

So I'm in the need to translate a C library to pure java, so far its looking good, but I'm stuck here.

Can someone explain to me what does the following pointer is for?

double *DTimeSigBuf[MAX_TIME_CHANNELS];

Ok I know it is a double type pointer named DTimeSigBuf, but whats that in the brackets? also MAX_TIME_CHANNELS is defined in the h file as:

 #define MAX_TIME_CHANNELS 2

then in the code this constant value changes, like its pointing somewhere else, but I dont know what does exactly means. is it equivalent to say:

double *DTimeSigBuf = MAX_TIME_CHANNELS;

if I recall well there was something similar in assembler, like: mov [BX], CL called Indirect addressing mode register, does this have anything to do with this? I know I might be completly lost! because as the title says, I'm a java programmer.

And the other question, what is the effect of doing this:

DTimeSigBuf[chanNum]            = (double*)malloc(block_size_samples*sizeof(double));

Where block_size_samples is int and chanNum is an for iterator variable?

Please help! I sware I've been googling the whole time.

Thanks folks :)

like image 318
RicardoE Avatar asked Dec 04 '22 04:12

RicardoE


2 Answers

It is an array of pointers to double. MAX_TIME_CHANNELS is size of the array.

The effect of the statement with malloc is allocation of a block of memory large enough for block_size_samples double values; address of the block of memory is then assigned to chanNum element of the DTimeSigBuf array.

like image 68
piokuc Avatar answered Dec 17 '22 08:12

piokuc


DTimeSigBuf is an array of pointers to type double. This could be thought of as an array of arrays of type double.

double *DTimeSigBuf[MAX_TIME_CHANNELS];

could be thought of as

double DTimeSigBuf[MAX_TIME_CHANNELS][]

The line

DTimeSigBuf[chanNum] = (double*)malloc(block_size_samples*sizeof(double));

is allocating memory for block_size_samples number of variables of type double to be placed in the array pointed at by DTimeSigBuf[chanNum].

For example:

If block_size_samples is 4 and chanNum is 1, you could think of it this way:

DTimeSigBuf[1] = new double[4];
like image 41
ken Avatar answered Dec 17 '22 08:12

ken