Failed attempt at using xalloc

Hello,

I would like to know the difference between the 2 following versions of what I thought was the same piece of code.

// 1 ==================================================
// in the lib file

/*** BeginHeader table_matrice*/
extern const unsigned char table_matrice[];
/*** EndHeader */

const unsigned char table_matrice[]=
{
	0xFE,0x01,
	0x03,0x02,
	0x0D,0x02,
	0x31,0x02,
	0xC1,0x02,
	0x01,0x03,
	0xFE,0x01
};

// accessed using : var = table_matrice[i];
// ====================================================

and the other version

// 2 ==================================================
// in main program, as global var
unsigned char* pt_table_matrice;
unsigned char* pt_table_matrice_h;


// in lib file
/*** BeginHeader init_matrice */
void init_matrice(void);
/*** EndHeader */

void init_matrice(void)
{
	long mat1

    mat1 = 14;

	pt_table_matrice = xalloc(mat1*sizeof(char));

	pt_table_matrice[	0]=	0xFE	;	pt_table_matrice[	1]=	0x01	;
	pt_table_matrice[	2]=	0x03	;	pt_table_matrice[	3]=	0x02	;
	pt_table_matrice[	4]=	0x0D	;	pt_table_matrice[	5]=	0x02	;
	pt_table_matrice[	6]=	0x31	;	pt_table_matrice[	7]=	0x02	;
	pt_table_matrice[	8]=	0xC1	;	pt_table_matrice[	9]=	0x02	;
	pt_table_matrice[	10]=	0x01	;	pt_table_matrice[	11]=	0x03	;
	pt_table_matrice[	12]=	0xFE	;	pt_table_matrice[	13]=	0x01	;
}
// accessed using : var = pt_table_matrice[i];
//====================================================

I imagine that it doesn’t, since it is no giving me the same result … but I can’t see the difference between the 2, except that the 2nd version stores the values in xmem, which is what I was looking for.

Anyone got any idea ?

Thanks

Hi,

The first thing to note is that xalloc does not return a normal popinter to the block. It returns a long containing the 20bit physical address of the allocated block so the second code sample is incorrect. (if you are using DC10.64 you could use a far pointer but I don’t know what version you are using).

You need to have a look at the xmem samples and the documentation to see how to use xmem with xalloc and associated functions.

If you are using DC10.xx there are easier ways to achieve this with far pointers.

Regards,
Peter

Thanks for the quick reply.

Aw I assumed that xalloc was returning the pointer like a regular malloc. I’ll check the Dynamic C doc first next time ^^

Yes I’m using 10.64, so I checked the far pointers, and yea it seems that it is exactly what I was looking for.

Just a question on that.

If I use the #define USE_FAR_STRING_LIB, all the strcpy() will become _f_strcpy(). But if I use a strcpy with near pointers, do I need to specify n or will it make the difference by itself ?

Thanks again

the far versions of the string functions will work with near and far pointers.

In the bacnet stack I use far data in lots of places and it seems to work fairly well.

Regards,
Peter