Need Help Memcpy Access Violation

All helbreath developer topics and releases
Post Reply
Hypnotoad
Member
Posts: 100
Joined: Wed Apr 28, 2004 8:16 pm

Post by Hypnotoad »

I am trying to use memcpy to copy from one array to another except its making an access violation. Here is a snipped of the code...

CPP / C++ / C Code:
char cTotalCharNames[111], cCharacterName[11], cTotalChar;

for (i = 0; i <= cTotalChar; i++) {
&nbsp; &nbsp; memset(cCharacterName, 0, sizeof(cCharacterName));
&nbsp; &nbsp; memcpy(cCharacterName, cTotalCharNames[i*11], 10);
&nbsp; &nbsp; wsprintf(G_cTxt, "Character (%s) added", cNewCharName);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
}

well the array cTotalCharNames is [111] in size and i want to write all the possible character names into it using memcpy moving 11 places after each name for example the first name would go into spots spaces 1 to 11, then the 2nd name would go into 11 to 22 etc.. Except when i am running this code it makes an access violation. If i change "memcpy(cCharacterName, &cTotalCharNames[i*11], 10);" It stops the access violation but the name isnt copied. what am i doing wrong...


edit:

i need to add a few things which i forgot to add. If no "&" or "(char *)" is added before cTotalCharNames[i*11], i get the error:

error C2664: 'memcpy' : cannot convert parameter 2 from 'char' to 'const void *'

cTotalChar is initially 0, and reads a file and returns a number does not exceed 8 - 4 characters for abaddon 4 characters for apocalypse. The access violation occurs on the first attempt to go through "for (i = 0; i <= cTotalChar; i++) {." Breakpoints tell me that as soon as the line " memcpy(cCharacterName, cTotalCharNames[i*11], 10);" is processed it crashes.

There is code to write from cTotalCharNames[i*11] and to write to cTotalCharNames[i*11]. The above code in this case is from, below is the example of to:

CPP / C++ / C Code:

Code: Select all

memcpy&#40;cTotalCharNames&#91;cTotalChar*11&#93;, cNewCharacterName, 10&#41;;
<img src='http://www.playah.no-ip.com/images/Hypn ... imated.gif' border='0' alt='user posted image' />
KLKS
Loyal fan
Posts: 218
Joined: Sun Feb 22, 2004 2:32 pm

Post by KLKS »

u need to use &. but the loop has a problem,

memcpy(cCharacterName, cTotalCharNames[i*11], 10);

as your loop increases, data in cCharacterName gets overided with the newest cTotalCharNames. is that what you want to achieve ?
Hypnotoad
Member
Posts: 100
Joined: Wed Apr 28, 2004 8:16 pm

Post by Hypnotoad »

KLKS wrote: u need to use &. but the loop has a problem,

memcpy(cCharacterName, cTotalCharNames[i*11], 10);

as your loop increases, data in cCharacterName gets overided with the newest cTotalCharNames. is that what you want to achieve ?
the goal of this function is to write a packet to send to the MainServer detailing the data...heres a packet quick analysis

packet size max 750
00 to 10 is the account name
10 to 14 is character unique timer id to match processes between main/world
14 to 18 is message id (MSGID_RESPONSE_CHARACTERLOG)
18 to 20 is message type (DEF_LOGRESMSGTYPE_NEWCHARACTERCREATED)
20 to 30 is new character name
30 to 31 is total characters in account
----start loop here do this for each character----
31 to 41 character name extracted from cTotalCharNames
41 to 42 check if character file is valid (0/1)
42 to 44 appr1
44 to 46 appr2
46 to 48 appr3
48 to 50 appr4
50 to 52 sex
52 to 54 skin
54 to 56 ilevel
56 to 60 exp
60 to 62 str
62 to 64 vit
64 to 66 dex
66 to 68 int
68 to 70 mag
70 to 72 char
72 to 76 apprcolor
76 to 78 month
78 to 80 day
80 to 82 hour
82 to 84 minute
84 to 86 year
86 to 96 mapname
-------finish one loop - 65 in size-----
send message to main log server------> (cTotalChar*65)+32
perfect except for access violation
<img src='http://www.playah.no-ip.com/images/Hypn ... imated.gif' border='0' alt='user posted image' />
Aryes
Member
Posts: 114
Joined: Tue Aug 31, 2004 10:14 pm
Location: Brazil

Post by Aryes »

Code: Select all

char cTotalCharNames&#91;111&#93;, cCharacterName&#91;11&#93;, cTotalChar;
 
 for &#40;i = 0; i <= cTotalChar; i++&#41; &#123;
    memset&#40;cCharacterName, 0, sizeof&#40;cCharacterName&#41;&#41;;
    memcpy&#40;cCharacterName, &#40;char*&#41;cTotalCharNames+i*11, 10&#41;;
    wsprintf&#40;G_cTxt, "Character &#40;%s&#41; added", cNewCharName&#41;;          
&#125;
Try it

n00d :lol:
====<span style='color:red'><br>Aryes</span><br>====<br><br>HB United: www.hbuonline.net:<br><br><img src="http://hbtop50.com/button.php?u=hbkhispano" alt="Helbreath Top 50 - Helbreath Silver" border="0" /><br><img src="http://hbtop50.com/button.php?u=dcom" alt="Helbreath Top 50 - Helbreath Silver" border="0" /><br><img src="http://hbtop50.com/button.php?u=tinchocba" alt="Helbreath Top 50 - Helbreath Silver" border="0" /><br><img src="http://hbtop50.com/button.php?u=rafha_bernn" alt="Helbreath Top 50 - Helbreath Silver" border="0" /><br><img src="http://hbtop50.com/button.php?u=HB-Tere" alt="Helbreath Top 50 - Helbreath Silver" border="0" /><br><img src="http://hbtop50.com/button.php?u=Kiruku" alt="Helbreath Top 50 - Helbreath Silver" border="0" />
snoopy81
Loyal fan
Posts: 338
Joined: Mon Jul 12, 2004 7:13 pm

Post by snoopy81 »

memcpy(cCharacterName, cTotalCharNames[i*11], 10);
cCharacterName is *char (a pointer)
cTotalCharNames is *char (a pointer)
cTotalCharNames[i*11] is char and not *char (not a pointer)
So Aryes got it... (Pointer + offset casted to pointer)



_\_ _<br> / , \__/ . \ Admin of Equilibrium Project<br> II\ \___ . O<br> III \_/ \ _ / <a href='http://www.equiprojet.com' target='_blank'>http://www.equiprojet.com</a><br> II I¯I
Hypnotoad
Member
Posts: 100
Joined: Wed Apr 28, 2004 8:16 pm

Post by Hypnotoad »

:-D well it works now should make things easier on me for the rest of this,.

I are a proud noodle.
<img src='http://www.playah.no-ip.com/images/Hypn ... imated.gif' border='0' alt='user posted image' />
snoopy81
Loyal fan
Posts: 338
Joined: Mon Jul 12, 2004 7:13 pm

Post by snoopy81 »

I don't know if theure is a real professional C++ coder here, with a real C++ developping formation...
Because as many people, by all learning by myself, I lack some piece of knowledge, and sometimes, it's a bit.... difficult with wouldbe easy things...
_\_ _<br> / , \__/ . \ Admin of Equilibrium Project<br> II\ \___ . O<br> III \_/ \ _ / <a href='http://www.equiprojet.com' target='_blank'>http://www.equiprojet.com</a><br> II I¯I
Post Reply