Download MetaTrader 5

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

To add comments, please log in or register
Publish your articles and improve your rating!
minimax2000
93
minimax2000 2014.11.05 13:24 

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
4818
Sergey Dzyublik 2014.11.05 14:58  
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
93
minimax2000 2014.11.05 15:32  
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
6608
Alexey Da 2014.11.06 09:20  
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
93
minimax2000 2014.11.06 13:56  
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