473,397 Members | 2,068 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,397 software developers and data experts.

std::string assignment from const char* longer than 16 characters causes junk

In VC7, look at the following code:

char* ok1 = "1234567890ABCD";
char* ok2 = "1234567890ABCDE";
char* bad1 = "1234567890ABCDEF";
char* bad2 = "1234567890ABCDEFG";
string take;
take = ok1;
take = ok2;
take = bad1;
take = bad2;

Debug it and inspect take after take = bad1 and take = bad2. take is filled
with garbage!!!
If the const char* string is longer than 15 characters then the resulting
std::string is junk.

On VC7.1 it is fixed.

Is there a workaround? I searched on google groups but couldn't find
anything. This is a painful and glaring bug. I'm forced to suspect that it
may be my setup, but I can't see why that would be..

Please help! Must I rewrite std::string ????


Nov 16 '05 #1
1 2705
Ben,
In VC7, look at the following code:

char* ok1 = "1234567890ABCD";
char* ok2 = "1234567890ABCDE";
char* bad1 = "1234567890ABCDEF";
char* bad2 = "1234567890ABCDEFG";
string take;
take = ok1;
take = ok2;
take = bad1;
take = bad2;

Debug it and inspect take after take = bad1 and take = bad2. take is filled with garbage!!!
If the const char* string is longer than 15 characters then the resulting
std::string is junk.

On VC7.1 it is fixed.

Is there a workaround? I searched on google groups but couldn't find
anything. This is a painful and glaring bug. I'm forced to suspect that it
may be my setup, but I can't see why that would be..

Please help! Must I rewrite std::string ????


No. The problem is only on the debugger, not at runtime. This happens
because for VC7, std::string was rewritten so that it includes internally a
union used to store inline strings with 15 or fewer chars, or a pointer to
heap allocated space if it's larger, and the debugger only looked at the
first part.

--
Tomas Restrepo
to****@mvps.org
Nov 16 '05 #2

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

Similar topics

10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
8
by: Jason Heyes | last post by:
If s is a std::string, does &s refer to the contiguous block of characters representing s?
24
by: Marcus Kwok | last post by:
Hello, I am working on cleaning up some code that I inherited and was wondering if there is anything wrong with my function. I am fairly proficient in standard C++ but I am pretty new to the .NET...
7
by: JustSomeGuy | last post by:
I need to make a class called uid. A UID is a unique identifier. It looks like... 1.2.3.345.1.2.4.566 This uid get transmitted over a network as 8 bit binary data. If the length of the UID is...
3
by: doubts | last post by:
Hi all, I am trying to convert my bulk of code from VC++ 6.0 to VC++.Net. when using std::string type variable, the application causes exception at one instance and does not cause an exception at...
4
by: daroman | last post by:
Hi Guys, i've problem with my small C++ programm. I've just small template class which represetns a array, everything works fine up to combination with std::string. I did tried it with M$ VC++ and...
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
11
by: tech | last post by:
Hi, I need a function to specify a match pattern including using wildcard characters as below to find chars in a std::string. The match pattern can contain the wildcard characters "*" and "?",...
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: 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
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.