Advertisement

char * HELP

Started by December 04, 2000 05:20 PM
1 comment, last by ziplux 24 years, 1 month ago
I''ve been trying to figure this out for a while, and have gotten nowhere. When I run this code:
  
char *t = NULL;
char q[10];

ZeroMemory(q, sizeof(q));

int dwSize = 1;
strcpy(q, "THISATEST!");
t = (char*)malloc(10);
if(t == NULL)
	MsgBox(g_hwnd, "err");
ZeroMemory(t, sizeof(t));
strcpy(t, q);
SendMessage(ChatBox, WM_SETTEXT, 0, (LPARAM)t);
// This puts the text into the edit control on my window

MsgBox(g_hwnd, "see?");
// MsgBox is a macro I made that calls MessageBox with MB_OK cause I''m lazy

free(t);
  
it gives me an error about an assertion and a Normal Block on the call to free. A picture of the error is here: Thanks in advance! My Geekcode: "GCS d s: a14 C++$ P+(++) L+ E-- W+++$ K- w++(+++) O---- M-- Y-- PGP- t X R- tv+ b++ DI+(+++) D- G e* h!" Decode my geekcode! Geekcode.com
Visit our web site: Asylum Entertainment
My Geekcode: "GCS d s: a14 C++$ P+(++) L+ E-- W+++$ K- w++(+++) O---- M-- Y-- PGP- t XR- tv+ b++ DI+(+++) D- G e* h!"Decode my geekcode!Geekcode.com
Visit our web site:Asylum Entertainment
First off, THISISATEST! is 11 bytes (including the NULL terminator)

sizeof(t) will probably return 4 - the size of a pointer, not the size of the memory allocated.

If the allocation is NULL, the code continues anyway, it should skip the rest
Advertisement
quote: Original post by TwoFlower

First off, THISISATEST! is 11 bytes (including the NULL terminator)

sizeof(t) will probably return 4 - the size of a pointer, not the size of the memory allocated.

If the allocation is NULL, the code continues anyway, it should skip the rest


Ah, I see. I''m mallocing 10 when I should be mallocing 11. Thanks.




My Geekcode: "GCS d s: a14 C++$ P+(++) L+ E-- W+++$ K- w++(+++) O---- M-- Y-- PGP- t X
R- tv+ b++ DI+(+++) D- G e* h!"
Decode my geekcode!
Geekcode.com


Visit our web site:
Asylum Entertainment
My Geekcode: "GCS d s: a14 C++$ P+(++) L+ E-- W+++$ K- w++(+++) O---- M-- Y-- PGP- t XR- tv+ b++ DI+(+++) D- G e* h!"Decode my geekcode!Geekcode.com
Visit our web site:Asylum Entertainment

This topic is closed to new replies.

Advertisement