Array of pointers and dinamically generated.

Hi, I'm looking for dinamically declaration of a array of pointers. With some investigation, I now that pointers are a 16 bit variables.

sizeof(int*);

I make a simple example code. I can't restore the value of the variable.

/*

Code for testing using an array of pointers.

The array is generated by malloc.

*/

int* f; // The pointer of the array of pointer

int var1 = 124;  // Variables for testing
int var2 = 634;
int var3 = 234;
int var4 = 958;
int var5 = 858;


void setup() {
  
  Serial.begin(9600); // Serial comunication for debuging
  
  f = (int *) malloc(5 * sizeof(int*));  // malloc for generating the new memory reservation for the list of 5 items


  f[0] = (uint16_t)&var1;  // filling the list of pointers from var's pointers
  f[1] = (uint16_t)&var2;
  f[2] = (uint16_t)&var3;
  f[3] = (uint16_t)&var4;
  f[4] = (uint16_t)&var5;

  Serial.print("*Var1: ");        // Printing vars pointers
  Serial.print((uint16_t)&var1);
  Serial.print("\t*Var2: ");
  Serial.print((uint16_t)&var2);
  Serial.print("\t*Var3: ");
  Serial.print((uint16_t)&var3);
  Serial.print("\t*Var4: ");
  Serial.print((uint16_t)&var4);
  Serial.print("\t*Var5: ");
  Serial.println((uint16_t)&var5);
  
  Serial.print("f[0]: ");          // Printing the array contents
  Serial.print(f[0]);              // Value: 608, memory direction
  Serial.print("\tf[1]: ");
  Serial.print(f[1]);
  Serial.print("\tf[2]: ");
  Serial.print(f[2]);
  Serial.print("\tf[3]: ");
  Serial.print(f[3]);
  Serial.print("\tf[4u: ");
  Serial.println(f[4]);

  Serial.print ("Var1: ");   
  Serial.println (var1);         // Still being 124

  Serial.print("f[0]: ");          // Printing the array contents of the original variables...
//  Serial.print(*(f[0]));  // Not compile: error: invalid type argument of unary ‘*’
//  Serial.print(*f[0]);  // Not compile: error: invalid type argument of unary ‘*’
//  Serial.print(((f)[0]));  // Compile, but value is 608
//  Serial.print(*(&f[0]));  // Compile, but value is 608
 
}

void loop() {
}

I can't understand so much how pointers is working.

Can any body help me? A little help to start understanding what is happen?

f should be a pointer to the start of an array of five (or whatever) pointers to ints. Thus, it needs to look like this:

int** f; // dynamic -- use new and delete or malloc and free
int* g[5]; // static -- prefered if you know that it'll be five since memory allocation is evil(tm)

int var1 = 12;
int var2 = 41;
int var3 = 913;
int var4 = 153;
int var5 = 1491;

f = new int*[5]; // C++ way
//f = (int**) malloc(5 * sizeof(int*)); // C way

for (int i=0; i<5; i++) {
  Serial.println(*f[i]);
  f[i]++;
}

Serial.println(var1);
Serial.println(var2);
Serial.println(var3);
Serial.println(var4);
Serial.println(var5);


delete f[]; // C++ way
//free(f); // C way I think

WizenedEE:
f should be a pointer to the start of an array of five (or whatever) pointers to ints. Thus, it needs to look like this:

Wowww, thanks, this was my problem.

I fix it, but, the "new" form doesn't work. This is the error message:

undefined reference to `operator new[](unsigned int)'

This actually compiles on my computer:

#include <iostream>
using namespace std;


int** f; // dynamic -- use new and delete or malloc and free
int* g[5]; // static -- prefered if you know that it'll be five since memory allocation is evil(tm)

int var1 = 12;
int var2 = 41;
int var3 = 913;
int var4 = 153;
int var5 = 1491;

int main() {
    f = new int*[5]; // C++ way
    //f = (int**) malloc(5 * sizeof(int*)); // C way

    f[0] = &var1;
    f[1] = &var2;
    f[2] = &var3;
    f[3] = &var4;
    f[4] = &var5;

    for (int i=0; i<5; i++) {
        cout << *f[i] << endl;
        (*f[i])++;
    }

    cout << var1 << endl;
    cout << var2 << endl;
    cout << var3 << endl;
    cout << var4 << endl;
    cout << var5 << endl;

    delete[] f; // C++ way
    //free(f); // C way I think
}

For the arduino, you'll need to define another version of operator new (the arduino libs only define one) (this compiles but I'm not sure it works properly):

void * operator new[](size_t size) {
    return malloc(size);
}

void operator delete[](void * ptr) {
    free(ptr);
}

WizenedEE:

I don't understand the difference between new in your code and malloc. It seems like a hardcoding.

Now I'm using the malloc option of your firs post. It's working fine.

Thanks.

Why define f as (effectively) an array of pointers to int? It seems much more likely that an array of ints (not pointers to ints) is what OP was looking for.

PaulS:
Why define f as (effectively) an array of pointers to int? It seems much more likely that an array of ints (not pointers to ints) is what OP was looking for.

My english is not very good, as you can see...

I can't understand very well your message...

I'll send it to a friend for help.

Thanks.