473,387 Members | 1,942 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,387 software developers and data experts.

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<std::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>FCPH9ioddE2faW+WEvjukQ==</Id>
Nov 17 '05 #1
2 1907

"anelma via .NET 247" <an*******@dotnet247.com> wrote in message
news:ei**************@tk2msftngp13.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<std::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******@nesterovsky-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::DoSomething(vector<string> 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<std::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>FCPH9ioddE2faW+WEvjukQ==</Id>

Nov 17 '05 #3

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

Similar topics

5
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....
7
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
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...
3
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
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
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...
5
by: Jae | last post by:
Real(const string &fileName) { FILE * myInputFile = fopen(fileName, "rt"); ..... fclose(myInputFile);
2
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...
3
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++...
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...
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...
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,...

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.