dsynkd
dsynkd

Reputation: 2145

Access Violation Error

Here's the code:

void SendRemoteData(string Data){
    char charout[1000];
    memset(charout,0,sizeof(charout));
    memcpy(charout,Data.c_str(),Data.size());
    send(tempclient, charout, sizeof(charout),0);
}

When I try to use this function twice, I get the error. What should I do?

Upvotes: 0

Views: 291

Answers (3)

dsynkd
dsynkd

Reputation: 2145

The problem occured because you can't use a string there in memcpy, but you're using Data.c_str(), which is wrong and gives me an error.

A cast should solve the problem:

memcpy(charout,(char *)Data.c_str(),Data.size());

Upvotes: 0

Coder02
Coder02

Reputation: 260

Find out in wich line the access violation happens - there are some positions that might be a problem. As example: you assume that Data is always smaller than 1000 characters - otherwise you cause a buffer overrun. Also, you always send 1000 bytes - is that really your intention?

Upvotes: 2

Dialecticus
Dialecticus

Reputation: 16769

Try this instead:

void SendRemoteData(string Data){
    send(tempclient, Data.c_str(), Data.size(),0);
}

Upvotes: 1

Related Questions