# passing anonymous arrays from Angelscript to C

This topic is 1188 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi There

I'm trying to pass arrays from angelscript to C - and having a few problems

The problems being it doesn't work at all - doesn't fill the vec array - and probably will eventually crash - I can't really tell what is going - because the C code is compiled at run time and my shabby system has no really debugging at the moment.

here's my C code:

typedef struct
{
int maxElements;
int numElements;
unsigned char  data[1];
}  SArrayBuffer;

typedef struct
{
int       refCount;
_Bool      gcFlag;
void*     objType;
SArrayBuffer*     buffer;
int       elementSize;
int       subTypeId;
} ASarray;

void Init(void)
{
Debug("TCC init called");
//asRegisterGlobalFunction("float TestFunk(float)",TestFunk);
//asRegisterGlobalFunction("vec3 vectest(vec3)",vectest);
//asRegisterGlobalFunction("vec3 vecpointer(const vec3 &in)",vecpointer);
asRegisterGlobalFunction("void vecfill(vec3[]@,int)",vecfill);
}

void vecfill(ASarray *a,int l)
{
vec3* v=(vec3*)a->buffer->data;

for (int a=0;a<l;a++)
{
v[a].x=counter++;
v[a].y=counter++;
v[a].z=counter++;
}
}

here's my angelscript code

array<vec3> TestArray(50);

vecfill(TestArray,2);


I am doing it this way because 1) I want to be able to pass arrays anonymously without having to define a specific class or struct

2) I'm using TCC so the C code is compile at runtime - it's actually called from angelscript:

ExecuteTCC("./NewDir2/Test.c","Init");

I realise this is probably as dodgy as hell - but it's  not a commercial thing, pure R&D - so I just hope it is possible.

thanks!

Edited by shabtronic

##### Share on other sites

What exactly are the problems you're having here?

##### Share on other sites

You forgot to tell us what the actual problems are

I'm guessing you're actually trying to manipulate the CScriptArray object directly through memory operations. If that is the case, your ASArray struct is missing the hidden virtual function table pointer that C++ adds to the class behind the scenes due to the existence of a virtual method, i.e.

typedef struct
{
void*         virtualFunctionTable;
int           refCount;
_Bool         gcFlag;
void*         objType;
SArrayBuffer* buffer;
int           elementSize;
int           subTypeId;
} ASarray;


You'll also need to carefully verify the alignment of the members in the struct. It is likely that it will be correct, but there is no guarantee that TCC will align the members exactly the same way that whatever compiler you use to compile the application. It might be necessary to add some padding between members to get the proper alignment. ?

?

?

Edited by Andreas Jonsson

##### Share on other sites

Thanks Andreas - I thought it might be something like the vptr - but assumed the lack any inheritance would nuke it :) I will try it later on today - I really hope it works - because

having that anonymity saves me a fair bit of typing and fits with my reckless R&D code style :)

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633669
• Total Posts
3013259
×