how to get memory address of a sample input vector?

TristanShepherd's icon

t_int *resampler_perform1chn(t_int *w)
{
    t_resampler *x = (t_resampler *)(w[1]);
    t_float *in = (t_float *)(w[2]);
    t_float *in2 = (t_float *)(w[3]);
    t_float *out = (t_float *)(w[4]);
    int n = (int)(w[5]);

blah blah, declarations etc, getting data from last call from memory

while (--n) {
        temp = *++in;
        input = *++in2;

now at this point how do I find the memory address of *in2

I'm using an static library to which i need to pass the memory address of an arbitrary sample in the input vector of the 2nd input {w[3] here???}?

newer to programming, any help?

is it &(*in) or just in?

$Adam's icon

Probably you need to familiarize yourself with the concept of pointers:

In short: a pointer is a variable that "points" to a specific memory address. When you need to pass the address of an arbitrary sample in the memory, you'll create a pointer "pointing" to that specific sample and pass this pointer to your static library.

In C there are two operators related to pointers: the * operator asks for the "content" of the address represented by a pointer (so for instance if p is a pointer to an integer residing somewhere in the memory, *p will return the actual value of that integer) and the & operator gets the address of a specific variable (for example if v is a variable then &v tells you the address of the variable v in the memory).

Also don't confuse the symbol * when used as an operator and the same symbol when used in a declaration: when you declare a pointer, you'll use the symbol * to tell the compiler that you need a pointer (for example, in the top of your function the t_resampler *x declaration means that x is a pointer to a variable of kind t_resampler).

As you can see, the variable called in2 in your code was declared as t_float *in2. This means that in2 is a pointer pointing to variables of type t_float. After learning how MSP deals with input vectors you'll realize that the initialization

t_float *in2 = (t_float *)(w[3]);

means that in2 is a pointer pointing to the first sample of an input vector (assuming that the third argument you set up in the DSP initializing routine was actually an input vector). In other words, in2 will hold the address of the first sample in the memory. Therefore the address of the n-th sample of the input vector would be in2+n.

This is the value you should pass to your static library.

A few more remarks: since & and * are "inverse" of each other, writing *& or &* just doesn't make sense: &*in2, *&in2 and in2 mean just the same thing (this is not necessarily true for C++ as theoretically you could overload these operators, which happens some times for instance in the STL library, but you won't need to deal with this for now). And as *in2 means the actual value stored in the first sample of the vector, &in2 would mean the address of the pointer pointing to the first sample of the input vector (in other words, &in2 is a double-pointer). Since in2 itself is just a local pointer used by your function, it is more than probable that a third-party static library won't need to deal with this value.

Hope this helps,
$adam