Need urgent help.

D

drazbliz

Hello,

I am running the following code segment in visual studio 2005.

size_t cchSize = 10*sizeof(TCHAR);
LPTSTR temp = (LPTSTR)malloc(cchSize);
temp[0] = '\0';
_tcscat_s(temp, cchSize, _T("hello"));
free(temp);

It fails at the statement -

free(temp)

with the error:

Heap block at 00A57F80 modified at 00A57FC0 past requested size of 38.

If i change _tcscat_s to deprecated _tcscat there is no error. Can
anyone throw some light on this?


Thanks,
C++Programmer.
 
B

Ben Pope

Hello,

I am running the following code segment in visual studio 2005.

size_t cchSize = 10*sizeof(TCHAR);
LPTSTR temp = (LPTSTR)malloc(cchSize);
temp[0] = '\0';
_tcscat_s(temp, cchSize, _T("hello"));
free(temp);

It fails at the statement -

free(temp)

with the error:

Heap block at 00A57F80 modified at 00A57FC0 past requested size of 38.

If i change _tcscat_s to deprecated _tcscat there is no error. Can
anyone throw some light on this?

Not really, those functions starting with an underscore do not exist in
C++ and I have no idea what they do. Perhaps you should ask in a
newsgroup where those functions are topical, perhaps one with microsoft
and windows in the name.

Ben Pope
 
D

Daniel T.

Hello,

I am running the following code segment in visual studio 2005.

size_t cchSize = 10*sizeof(TCHAR);
LPTSTR temp = (LPTSTR)malloc(cchSize);
temp[0] = '\0';
_tcscat_s(temp, cchSize, _T("hello"));
free(temp);

It fails at the statement -

free(temp)

with the error:

Heap block at 00A57F80 modified at 00A57FC0 past requested size of 38.

If i change _tcscat_s to deprecated _tcscat there is no error. Can
anyone throw some light on this?

I believe that if you put the above code in a main all by itself, the
error will go away as well. If not, then probably _tcscat_s doesn't do
what you think it does and is writing past the requested size.

I can't help but wonder how big a TCHAR is if 10 * sizeof(TCHAR) ends up
equaling 38...
 
P

Phlip

drazbliz said:
I am running the following code segment in visual studio 2005.

size_t cchSize = 10*sizeof(TCHAR);
LPTSTR temp = (LPTSTR)malloc(cchSize);
temp[0] = '\0';
_tcscat_s(temp, cchSize, _T("hello"));
free(temp);

Why on Earth are you not using a CString, a CStringW, or a _bstr_t?

And why are you using a strcat() derived method when you could use a
strcpy() derived method, or even a _tcsdup()?

And why malloc() in a C++ program?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top