dll and array - page 2

 

Ok jjc, no sweat. Was just in the I/F btwn MT/C that was of interest.

My finer points of C KBase needs reboot anyway and have been thinking about sourcing a C compiler to play with. Borland of course has one as does MS but not want MS bloatware. There are many others anyway to pick from in all flavours like cmd line and rad...

Alternately, Python has a lot going for it (well, kinda floats my boat anyway:) and has loads current functionality that would allow some great playtime.

I just want to revisit the simple delights of actually computing for the sake of computing and not the day in, day out never ending nightmare that is MT.

Actually, bout 10yrs ago started to mess with asm + win. Wow... small AND fast or what?

Think must firstly sort out financial mess that is my life at the mo!

Cheers jjc and thanks no hard feelings or whatever :O)

 
fbj:

Actually, bout 10yrs ago started to mess with asm + win. Wow... small AND fast or what?

That takes me back... It must be over 15 years since I wrote any asm to speak of. DOS days, rather than Windows. Oh, the joys of "XOR AX, AX" rather than "MOV AX, 0" - if my memory is correct.


[...] the day in, day out never ending nightmare that is MT.

You should try something like NinjaTrader. For me personally, speed of development in its C# environment is slower than in MQ4, and any performance gains are too trivial to speak of. And then there's the fact that Ninja falls over all the time, and is a nightmare from a replication and disaster-recovery point of view. As I said to a broker recently who was suggesting that we move from MT4 to Ninja, I've had few happier days than the one when we turned Ninja off after about 18 months of continuous problems. MT4 is a walk in the park.


[...] sourcing a C compiler to play with

I can't remember if VS Express lets you create DLLs. But there's a well-known and trivial workaround which lets you use an evaluation version of Visual Studio Professional for an unlimited period. No need for hacks, or downloads from dodgy sites etc. I'm not actually going to disclose it, but just ask yourself "What's the simplest workaround which could possibly exist?"

 

I'm not too sure how I came across as patronising but I want to apologize if I offended you. It wasn't my intention.

.

When I first read this thread, I specified that I hadn't tried this for myself yet. I've got a lot more experience in C, which would be the DLL, than I do in MQL. Since I started off thinking that MQL was passing an address of a pointer to an array (which is what its syntax suggested), in C it would be done using a unary (**) declaration in the function prototype. By chatting with you, I realized that MQL was actually just passing the address of the array and I concluded by conceiding my position saying you were right and I was wrong from my first assumption.

.

To put it simply, MQL passes the address of the start of the array. So in this post, TestArray1 is the correct one to use. Beware that in C, passing the address (& prefix) of an array as MQL does in its syntax, you would be required to use a unary declaration in the function prototype. This is what lead to the confusion.

.

As for some education.. here are a few examples:

int a;

//integer variable.. address accessed by &a, value access by a

int *a;

//pointer to an integer value.. address accessed by a, value access by *a -- Note: needs to be allocated using malloc() calloc() or realloc() before storing or using a value

int a[10];

//an array of 10 integers, this is actually a pointer just like above but it is statically allocated at run time since the size is known.. address accessed by a, value access by a[0]

int **a;

//pointer to a pointer to an integer value.. just like a 2-dimensional array.. Note: you can dynamically allocate like the single pointer above but this time 2 dimensions

//address of the start of the 2d array is accessed by a or a[0], address of the start of the first element is accessed by a[1] or a+sizeof(int *), first value of the whole thing accessed by a[0][0]

//a[elem1][elem2]: a is the start of the whole thing, elem1 will all be pointers to the second dimension, elem2 will all be values

//note that you can also get the address of the 1st element by doing this: &(a[1])

int a[10][10];

//an array of 100 integers (10x10), this is actually a pointer just like above as well but since they are statically declare, size is known at runtime and is readily useable.

//the rest is all the same as above, even how they are accessed

.

Let's say you want to send "int *a" to a function when it's already allocated with 10 integers initialized to 0's. You would simply pass the pointer to the function like so:

.

#define NUM_ELEM 10 //number of elements we want in the array

.

int main(){

int i;

int *a = NULL;

if((a = malloc(sizeof(int) * NUM_ELEM) == NULL){return 0;} //allocate 40 bytes because we want 10 ints of 4 bytes each

function(a); //send to the function which will print by accessing the values

if(a){free(a);}

return 0;

}

.

void function(int *a){ //pointer to ints

int i;

for(i=0;i<NUM_ELEM;i++){printf("a[%i] = %i\n", i, a[i]);}

}

.

Now, let's say you want to send "int *a" to a function and allocate 10 integers there and fill it with 0's. You would need to pass the address of that pointer so you can change the value it is pointing at (otherwise you will only be sending its value and wont know this address inside the function like the example above).

.

#define NUM_ELEM 10 //number of elements we want in the array

.

int main(){

int i;

int *a = NULL;

function(&a); //send the address of the pointer so we can use that address and change where it's pointing at inside the function

for(i=0;i<NUM_ELEM;i++){printf("a[%i] = %i\n", i, a[i]);}

if(a){free(a);}

return 0;

}

.

void function(int **a){ //address to a pointer to ints -- Note: this is NOT a 2-dimensional array in this case

int i;

if((*a = malloc(sizeof(int) * NUM_ELEM) == NULL){return;} //remember a is the addres of the pointer, by using *a we are changing where it's pointing from inside a function

for(i=0;i<NUM_ELEM;i++){(*a)[i] = 0;} //for optimal performance, use memset().. I did it this way for educational purposes.

}

.

It's incredible just how many things you can do with pointers. This is why they are so popular.

.

Let me know if more questions arise from this.

.

Jon