Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

strncpy_s

27 views
Skip to first unread message

Jan M

unread,
Mar 17, 2009, 8:49:13 AM3/17/09
to
Hi,

Silly question. Why does strncpy_s() fill the unused part of the destination
with 0xFD?

Cheers,

Jan


Alex Blekhman

unread,
Mar 17, 2009, 9:04:20 AM3/17/09
to
"Jan M" wrote:
> Silly question. Why does strncpy_s() fill the unused part of the
> destination with 0xFD?

This is not a silly question. CRT does it on purpose. It helps you
during debugging, so you can easily see what bytes belong to what.
For more special memory values see here:

"Win32 Debug CRT Heap Internals"
http://www.nobugs.org/developer/win32/debug_crt_heap.html

Alex


Ulrich Eckhardt

unread,
Mar 17, 2009, 9:14:49 AM3/17/09
to
Jan M wrote:
> Silly question. Why does strncpy_s() fill the unused part of the
> destination with 0xFD?

I guess it does so in order to diagnose better when you are accessing the
memory there. This gives you a defined value that typically makes no sense,
so if you see it, you know that you did something wrong. It's the same as
with debug builds initialising ints to 0xcccccccc or malloc adding some
buffer range before and after the requested memory to track out-of-range
writes.

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

Jan M

unread,
Mar 17, 2009, 9:36:50 AM3/17/09
to
"Alex Blekhman" <tkfx....@yahoo.com> wrote in message
news:%23qaskDw...@TK2MSFTNGP05.phx.gbl...

Hi Alan,

Is there a way to have the _s versions not try to be so "helpful" when a
buffer has been carefully zeroed for a good reason?

Cheers,

Jan


Alex Blekhman

unread,
Mar 17, 2009, 10:56:34 AM3/17/09
to
"Jan M" wrote:
> Is there a way to have the _s versions not try to be so
> "helpful" when a buffer has been carefully zeroed for a good
> reason?

Yes. You have two choices:

1. Use _CrtSetDebugFillThreshold in ordert to affect debug version
of *_s string functions.

2. Use "unsafe" strncpy, which will pad the rest of the buffere
with zeroes.

HTH
Alex


beginthreadex

unread,
Mar 17, 2009, 11:49:14 AM3/17/09
to
I you know the size of your string to copy and you know that your
buffer is a good size then you can easily ZeroMemory and then use your
favorite memcpy routines. it will just pickup the memory from location
x and plop it into y. be careful with your buffers so you never
overrun and you'll be fine.

On Tue, 17 Mar 2009 13:36:50 -0000, "Jan M" <ja...@jerseymail.co.uk>
wrote:

Jan M

unread,
Mar 17, 2009, 11:50:46 AM3/17/09
to
Hi Alan,

Thanks. _CrtSetDebugFillThreshold() is what I was looking for.

Cheers,

Jan


"Alex Blekhman" <tkfx....@yahoo.com> wrote in message

news:%234beSCx...@TK2MSFTNGP03.phx.gbl...

0 new messages