473,692 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const char* and std::string problem with .net (works with 6.0)

Following code works fine, when compiled with VS 6.0, but not anymore when compiled in .NET. What's wrong here, I can't see it by myself?
arrString content will be garbage with .net compilation, but when compiled with 6.0 it contains string from Vector (that's how I want it to work).

std::vector<std ::string> Vector;
...

void MyClass::DoThis (std::vector<st d::string> Vector)
{
const char *arrString[10];

for (int i = 0; i < 10 && i < Vector.size(); i++)
arrString[i] = Vector[i].c_str();
When I debug these, I see that the problem is with my array of const char*. Const char* items seems to change, when std:string changes. For example,
Vector[0].c_str = "dog"
arrString[0]="dog"
Vector[1].c_str = "cat"
arrString[0]="cat" --> this is the problem, why it happens?
This happens only with .net, with 6.0 remains arrString[0] = "dog"
--------------------------------
From: anelma

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>FCPH9ioddE2 faW+WEvjukQ==</Id>
Nov 17 '05 #1
2 1931

"anelma via .NET 247" <an*******@dotn et247.com> wrote in message
news:ei******** ******@tk2msftn gp13.phx.gbl...
Following code works fine, when compiled with VS 6.0, but not anymore when compiled in .NET. What's wrong here, I can't see it by myself? arrString content will be garbage with .net compilation, but when compiled with 6.0 it contains string from Vector (that's how I want it to work).
std::vector<std ::string> Vector;
..

void MyClass::DoThis (std::vector<st d::string> Vector)
{
const char *arrString[10];

for (int i = 0; i < 10 && i < Vector.size(); i++)
arrString[i] = Vector[i].c_str();
When I debug these, I see that the problem is with my array of const char*. Const char* items seems to change, when std:string changes. For
example, Vector[0].c_str = "dog"
arrString[0]="dog"
Vector[1].c_str = "cat"
arrString[0]="cat" --> this is the problem, why it happens?
This happens only with .net, with 6.0 remains arrString[0] = "dog"


std::string owns memory returned with c_str(). Destiny of this memory is
implementation defined when string is changed.
--
Vladimir Nesterovsky
e-mail: vl******@nester ovsky-bros.com
home: www.nesterovsky-bros.com
Nov 17 '05 #2
Using VS.Net 2003, I don't see the problem you're seeing.

void
MyChass::DoSome thing(vector<st ring> v)
{
const char *arr[10];
for (int i = 0; i < 10 && i < v.size(); i++)
{
arr[i] = v[i].c_str();
}

// this will print "dog" and "cat" correctly
for (int j=0; j < 10 && j < v.size(); j++)
{
cout << "arr[" << j << "] = " << arr[j] << endl;
}
}

int _tmain(int argc, _TCHAR* argv[])
{
MyChass m;
std::string s1 = "dog";
std::string s2 = "cat";
std::vector<std ::string> v;
v.push_back(s1) ;
v.push_back(s2) ;
m.DoSomething(v );
return 0;
}

Ary Sulistyono
anelma via .NET 247 wrote:
Following code works fine, when compiled with VS 6.0, but not anymore when compiled in .NET. What's wrong here, I can't see it by myself?
arrString content will be garbage with .net compilation, but when compiled with 6.0 it contains string from Vector (that's how I want it to work).

std::vector<std ::string> Vector;
..

void MyClass::DoThis (std::vector<st d::string> Vector)
{
const char *arrString[10];

for (int i = 0; i < 10 && i < Vector.size(); i++)
arrString[i] = Vector[i].c_str();
When I debug these, I see that the problem is with my array of const char*. Const char* items seems to change, when std:string changes. For example,
Vector[0].c_str = "dog"
arrString[0]="dog"
Vector[1].c_str = "cat"
arrString[0]="cat" --> this is the problem, why it happens?
This happens only with .net, with 6.0 remains arrString[0] = "dog"
--------------------------------
From: anelma

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>FCPH9ioddE2 faW+WEvjukQ==</Id>

Nov 17 '05 #3

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

Similar topics

5
9400
by: selder21 | last post by:
Hello, I have a class with constructor taking a const string&. Now i want to call this constructor with a string literal. Because this is of type char* there are overload resolution conflicts. If i make another constructor with parameter const char*, how can i call the constructor with the const string& ? I tried
7
3565
by: Sims | last post by:
Hi, if i have a code const char * GetValue() { std::string szVectorValue = ...// get a std::string from the vector return szVectorValue.c_str(); }
9
10266
by: Mathieu Malaterre | last post by:
Hello, This thread follow my previous one on the gcc mailing list. Basically I -still- have a problem in my code. I define in a header file: static const std::string foo = "bar"; Which not only seems to be a bad practice but also creates a bug on MacOSX panther (gcc 3.3).
3
2470
by: Old Wolf | last post by:
Hi all. G++ fails to compile the following: #include <string> int main() { std::string foo("abc=123"); std::string::const_iterator delimiter = std::find(foo.begin(), foo.end(), '=');
1
1743
by: yccheok | last post by:
Hi, consider the following bug at #include <stdio.h> class Drink { public: Drink( char *name_ ) : name( name_ ) {} char *name; char & get(int i) { return name; } };
2
11706
by: msaladin | last post by:
Hi all, I spent today with finding an error, I found it, though I don't no why. I have a class with static constants, like this: class EXPORT_API BusConstants { public: static const std::string PIPE_DIRECTORY; };
5
8825
by: Jae | last post by:
Real(const string &fileName) { FILE * myInputFile = fopen(fileName, "rt"); ..... fclose(myInputFile);
2
10906
by: pookiebearbottom | last post by:
Just looking for opinion on which of the 3 methods below people use in their code when they convert a 'const char *' to a 'const std::string &' came across #3 in someone's code and I had to think for a sec. At first I read it as converting a 'const char *' to a 'std::string *' void f(const std::string &s) { std::cout << s.size() << "\n";
3
9524
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++ code (ie. wrappers etc). In Managed C++ there was an automatic conversion between const char* and String^. This was useful to us for two reasons: 1. We declared our string constants as eg. const char* const c_My_Constant = "blah", and these...
0
8540
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9086
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8963
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8803
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5820
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4324
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4557
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2241
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.