Download MetaTrader 5

Pointer array as a member behaves strange when passed to a function.

To add comments, please log in or register
minimax2000
103
minimax2000  

Pointer array doesn't work well when it is a member of a class or a struct. It returns bad pointers if it is passed to a function. Why???

class C { public: int c; };

struct S { C* array[2]; } s;

void F()
{
  s.array[0] = new C();
  s.array[1] = new C();
  G(s.array);
}

void G(C* &array[])
{
  Print(array[0].c);
  Print(array[1].c);  // bad pointer  WHY????
}
Sergey Dzyublik
5171
Sergey Dzyublik  
class C 
{
public: 
   int c; 
};

struct S 
{ 
   C* array[]; 
   S () {ArrayResize(array, 2);}
   ~S() 
   {
      for (int i = ArraySize(array) - 1; i >= 0; --i)
         if (POINTER_DYNAMIC == CheckPointer(array[i]))
            delete array[i];
   }
} s;

void F()
{
  s.array[0] = new C();
  s.array[1] = new C();
  G(s.array);
}

void G(C* &array[])
{
  Print(array[0].c);
  Print(array[1].c);
}
minimax2000
103
minimax2000  
ALXIMIKS:

Thank you ALXIMIKS. Your code works.

But I still can't understand what is wrong with my code. Why doesn't fixed size array work?

Alexey Da
Moderator
6629
Alexey Da  
minimax2000:

Thank you ALXIMIKS. Your code works.

But I still can't understand what is wrong with my code. Why doesn't fixed size array work?

I've just checked your code in 1010 build of client terminal. Your code works well.

So I need to know build number of client terminal you are using.

minimax2000
103
minimax2000  
alexvd:

I've just checked your code in 1010 build of client terminal. Your code works well.

So I need to know build number of client terminal you are using.

Thank you alexvd. My question is solved.

My client's build number is 735. I will wait for my broker's update.

To add comments, please log in or register