Ananya Taneja
Ananya Taneja

Reputation: 11

Crash in wcscpy_s

I am getting crash in wcscpy_s while wcscpy works fine. I have the following struct

struct Test {
...
wchar_t         identity[256 * 2];  
...
};

I am doing member-by-member copy of one struct into another. The below one is crashing

wcscpy_s(t2.identity, sizeof(t2.identity) , t1.identity);

while this one is working fine:

wcscpy(t2.identity, t1.identity);

Upvotes: 1

Views: 621

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409404

If you read the wcscpy_s reference you will see that the middle argument is the number of elements, while you pass the size of the array in bytes.

If you're using Visual C++ then you can use e.g. _countof to get the number of elements.

Upvotes: 5

Related Questions