Pointer Problem

Started by
1 comment, last by NoNseNse 23 years, 9 months ago
I was experimenting with DPlay, and decided to write a small chat-program to learn it properly. DPlay proved not to be the problem, the problem is this programming with Windows-dialogs, cause I didn''t do that before (Doing a 3d-engine with D3D doesn''t require much windows-programming...) So here''s my problem: in the last line if this procdedure I get a debug error, saying ''Damage!!''. I can continue my app, but I don''t think that it''s too healthy for the system. So what did I wrong with this damn char*? THX char *text = NULL; DWORD size = SendDlgItemMessage( hDlg, IDC_EDIT1, WM_GETTEXTLENGTH, 0, 0 ); text = (char*)malloc(size); GetDlgItemText( hDlg, IDC_EDIT1, text, size+1); //SendChat(PlayerList[0].ID, text); SetDlgItemText( hDlg, IDC_EDIT2, text); if(text) free(text);
Advertisement
> text = (char*)malloc(size);
Firstly, don''t cast malloc. It''s unnecessary, and can hide other errors. But this is the problem line. You''re not allocating space for the null zero, as size contains only the number of characters in the string, not the amount of memory the string requires. You want size + 1 here.



> GetDlgItemText( hDlg, IDC_EDIT1, text, size+1);
Here you''re telling the function to use copy more characters than you''ve allocated memory for. This function expects a number of characters, not an amount of memory.
Casting malloc is proper style, and doesn''t cause errors.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt

This topic is closed to new replies.

Advertisement