473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

new wchar_t[5]

Being relatively new to c++, I've recently discovered a few things
about the behavior of new with respect to wchar_t, ZeroMemory and
delete. I can't seem to find documentation describing the following
three behaviors:

1. int len = 5; wchar_t *pwch = new wchar_t[len];

Subsequent inspection of pwch always show that it is allocated space
for 12 WCHARs. (The amount of overallocation varies depending on the
value of len).

2. By default pwch always begins with a string of five 0xcdcd's,
followed by two 0xfdfd's, then four 0xabab's and finally the string is
always terminated by a 0xfeee.

3. If I somehow overwrite any of the 0xfdfd's, 0xabab's or the 0xfeee,
subsequent calls to delete fail and my application terminates
abnormally.

After spending an hour or so exploring, it has become clear that VC
likes to overallocate storage for wchar_t, uses a 0xfdfd, 0xabab,
0xfeee sequence to identify the overallocated bytes so delete can
correctly identify and free the bytes following the null string
terminator.

As a newb, I wanted to use ZeroMemory to zero-out the entire 12 WCHARs
rather than just the WCHAR at position pwch + 4. When that failed, I
read the documentation on new wchar_t and delete, and hunted around
usenet for about 2 hours before attempting to reinvent the wheel and
solve the problem on my own (losing another 2 hours).

Where does one find documentation on the allocation of specific types?

Why are these strings over-allocated? Is that an optimization?

Any replies are much appreciated.
Nov 16 '05 #1
2 3488
"Isak Dinesen" <no****@hotmail.com> wrote in message
news:19**************************@posting.google.c om...
Being relatively new to c++, I've recently discovered a few things
about the behavior of new with respect to wchar_t, ZeroMemory and
delete. I can't seem to find documentation describing the following
three behaviors:

1. int len = 5; wchar_t *pwch = new wchar_t[len];

Subsequent inspection of pwch always show that it is allocated space
for 12 WCHARs. (The amount of overallocation varies depending on the
value of len).
You asked for 5 wchar_t's. You got them. The fact that they live in a
"bigger neighborhood" than what you expected is beside the point. :-)
2. By default pwch always begins with a string of five 0xcdcd's,
followed by two 0xfdfd's, then four 0xabab's and finally the string is
always terminated by a 0xfeee.
In the presence of a debugger, compilers like to instrument your program in
ways that make it easier for them to diagnose user errors. For example, if
you make a common error and write

pwch[5] = blah;

at runtime VS may be abe to tell you that you overran a buffer.
3. If I somehow overwrite any of the 0xfdfd's, 0xabab's or the 0xfeee,
subsequent calls to delete fail and my application terminates
abnormally.
Yes. This shouldn't surprise you. :-) Writing to memory that you didn't
allocate can do just about anything. Virus writers use this technique to
take control of an application or a set of them. Do not do this, ever!

When C was hot, I heard it said that "C sets policy, it does not enforce
practice". In other words, the language gives a developer enough rope to
hang himself. C++ tries to "improve" on that state of affairs in some ways,
but at the end of the day, if you try you can still hurt yourself. Badly.
Some say, for example, that an application developer should never manipulate
a "naked" pointer but rather use a "smart" pointer type. That's a subject
for another day.
Where does one find documentation on the allocation of specific types?


At some level, you shouldn't be concerened about implementation. If you ask
for an array of N elements of type T you get them in sequential storage
locations. In fact, the behavior you report may be different in a release
build than in a debug build depending on options selected. The behavior can
(and does) change across compiler versions.

To satisfy your curiosity, however, you might want to search the MSDN-CD or
at http://msdn.microsoft.com for the article "Compiler Security Checks In
Depth" by Brandon Bray (who sometimes posts here) if you want to learn more
about VS code generation in debug builds, etc.

Regards,
Will
Nov 16 '05 #2
William gave a terrific answer (and not just because he mentioned my
article). I just wanted to fill in one other thing.

Isak Dinesen wrote:
Why are these strings over-allocated? Is that an optimization?


As William said, it shouldn't matter to you as a user. Nevertheless, it is
nice to know. The reason is that the smallest allocation that malloc or new
can return on x86 is 16-bytes. You'll find that all memory is dealt out in
16-byte chunks (unless you change the memory manager).

The main reason for doing this is to help align memory. When accessing
unaligned memory (i.e., accessing a double on an 4-byte alignment rather
than 8-byte alignment) the x86 stalls. Also, if the allocation chunks are
too small, the memory manager has more to manage. It tends to lead to
greater memory fragmentation which over time looks like memory leaks. What
the memory manager does now is empiracally the best thing in general for
most programs.

Hope that helps. Cheerio!

--
Brandon Bray Visual C++ Compiler
This posting is provided AS IS with no warranties, and confers no rights.
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Julius Mong | last post by:
Hi all, I'm doing this: // Test char code wchar_t lookup = {0x8364, 0x5543, 0x3432, 0xabcd, 0xef01}; for (int x=0; x<5; x++) { wchar_t * string = (wchar_t*) malloc(sizeof(wchar_t)); string =...
1
by: Mark Fancy | last post by:
I have a project that I'm trying to get to compile. I need to have the /Tc:wchar_t compile switch in order to use some libraries. I have included the following header files: #include <mapix.h>...
23
by: Steven T. Hatton | last post by:
This is one of the first obstacles I encountered when getting started with C++. I found that everybody had their own idea of what a string is. There was std::string, QString, xercesc::XMLString,...
1
by: jjf | last post by:
Do Standard C's wide characters and wide strings require absolutely that each character be stored in a single wchar_t, or can characters be "multi-wchar_t" in the same way that they can be...
12
by: Jens Theisen | last post by:
Hello, does anyone know which layer is responsible for defining the size of wchar_t? Naturally enough, it's not defined in the language. I looked in the SystemV processor supplement and the...
8
by: Rui Maciel | last post by:
I've just started learning how to use the wchar_t data type as the basis for Unicode strings and unfortunately I'm having quite a bit of problems, both in the C front and the Unicode front. In...
3
by: john | last post by:
As far as I know there is only the type wchar_t. However my compiler compiles both "signed wchar_t" and "unsigned wchar_t". Are there both signed and unsigned wchar_t types?
16
by: Michael Brennan | last post by:
I guess this question only applies to programming applications for UNIX, Windows and similiar. If one develops something for an embedded system I can understand that wchar_t would be unnecessary. ...
5
by: yakir22 | last post by:
Hello experts, I am dealing now in porting our server from windows to linux. our client is running only on windows machine. to avoid the wchar_t size problem ( in windows its 2 bytes and linux is...
5
by: Samant.Trupti | last post by:
Hi, There is one thing I am cofused about.... If I have a declareation say char str; Now if I want to change it to wchar so do I have to change it like .... wchar_t str; or double the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.