Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 28th, 2006, 10:35 AM
drazbliz@yahoo.com
Guest
 
Posts: n/a
Default Need urgent help.

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.

  #2  
Old March 28th, 2006, 10:45 AM
Ben Pope
Guest
 
Posts: n/a
Default Re: Need urgent help.

drazbliz@yahoo.com wrote:[color=blue]
> 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?[/color]

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
--
I'm not just a number. To many, I'm known as a string...
  #3  
Old March 28th, 2006, 03:15 PM
Daniel T.
Guest
 
Posts: n/a
Default Re: Need urgent help.

In article <1143541635.951736.314680@e56g2000cwe.googlegroups .com>,
"drazbliz@yahoo.com" <drazbliz@yahoo.com> wrote:
[color=blue]
> 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?[/color]

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...


--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
  #4  
Old March 28th, 2006, 03:45 PM
Phlip
Guest
 
Posts: n/a
Default Re: Need urgent help.

drazbliz wrote:
[color=blue]
> 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);[/color]

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?

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


  #5  
Old May 1st, 2006, 09:48 PM
Newbie
 
Join Date: May 2006
Posts: 1
Default

Quote:
Originally Posted by drazbliz@yahoo.com
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?
_tcscat_s (and most if not all the secure runtime functions) uses the # of characters as the buffer size, not the # of bytes. You're probably compiling using the Unicode character set, so a TCHAR is 2 bytes, not 1.

The debug version of these functions also write to the entire buffer area, so by telling it there's 20 characters (40 bytes), the _tcscat_s function ends up overwriting memory beyond the allocated 10 characters (20 bytes), which leaves the heap damaged.


Try:

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

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles