c++ - Passing the Number of Elements in an Array to Function? -
i writing dll passes char array function. define char array 22 elements here:
unsigned char data[22] = { 0x00, 0x0a, 0x00, 0x09, 0x70, 0x00, 0x72, 0x00, 0x6f, 0x00, 0x74, 0x00, 0x68, 0x00, 0x65, 0x00, 0x67, 0x00, 0x75, 0x00, 0x79, 0x00 };
now, try pass array function declared as:
bool senddata(unsigned char* sdata, unsigned long ssize);
with these arguments:
senddata(data, 22);
this code compiles, crashes program when function called. taking closer while debugging, can see there's access violation in function senddata
. looking further, see values of data
, sdata
@ run-time:
data
points 22 byte char array correct values (obviously)sdata
points char array null-terminated first byte, containing 1 value (0)
it clear me compiler not know allocate 22 bytes sdata
, because not specify length it. question is:
how specify length of
sdata
argument passed won't terminate early?
if i'm wrong issue, please correct me , explain further. in advance!
edit:
i understand \0
(the first byte , many more in data
) null-terminator , prematurely end array. asking how avoid this. understanding sdata
never given specific length , therefore stops on \0
, may wrong.
i asked supply senddata function:
bool senddata(unsigned char* sdata, unsigned long ssize) { try { send(sdata, ssize); return true; } catch (...) { return false; } }
send
calling function module, isn't relevant issue, error occurs beforehand when sdata
argument passed senddata
.
no allocation of sdata going happen, points array. displays empty in debugger because displays char* string, , strings end when there '/0', first byte. not mean sdata not have correct data. write sdata[0]. sdata[1], etc, in debugger see correct values.
Comments
Post a Comment