4

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 :)

2
  • 2
    Doesn't Java also have brackets? They serve a similar purpose in C. Commented Mar 20, 2012 at 20:03
  • 5
    the questions are basic enough that instead of asking individual questions that you should look at a tutorial first - especially looking at arrays Commented Mar 20, 2012 at 20:03

6 Answers 6

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

By the way #define MAX_TIME_CHANNELS 2, will cause that the precompiler will replace with 2 every ocurrece of MAX_TIME_CHANNELS, so the declaration will be compiles as double *DTimeSigBuf[2];
3

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];

1 Comment

Nit: it's an array of pointer to double, not a pointer to an array of double.
3

Its an array pointer of type double. MAX_CHANNEL_TIMES is a constant and also the array size

Comments

3

If you have C code like:

#define MAX_TIME_CHANNELS 2 
double *DTimeSigBuf[MAX_TIME_CHANNELS]; 

In Java it looks like:

final static int MAX_TIME_CHANNELS = 2;
double DTimeSigBuf[][] = new double[MAX_TIME_CHANNELS][]; 

And this in C:

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

is allocating space for the y dimension.

In Java it is:

DTimeSigBuf[chanNum] = new double[block_size_samples];

Comments

2

DTimeSigBuf is an array of pointers to doubles.

The allocation is allocating an array of doubles. That is, the pointer that's returned is a pointer to the first double in an array of block_size_samples doubles.

Comments

1

As previously answered, the first sections are declaring an array of pointers to doubles. Since declaration does not necessarily allocate memory in C, the third line is allocating space for a new row of doubles.

Breaking it down:

DTimeSigBuf[chanNum] // chanNum is the position in the array
= // equals
(double*) // memory address to a double
malloc(  // get some memory from the system
block_size_samples*sizeof(double)); // number of samples times memory needed for one double

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.