.NETGURU
const char* and std::string problem with .net (works with 6.0)
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.vc.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

anelma via .Net Guru (VIP)
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 Guru (http://www.dot-net-guru.com/)

<Id>FCPH9ioddE2faW+WEvjukQ==</Id>
Reply to this message...
 
    
Vladimir Nesterovsky
"anelma via .Net Guru" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
> 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).
[Original message clipped]

example,
[Original message clipped]

std::string owns memory returned with c_str(). Destiny of this memory is
implementation defined when string is changed.
--
Vladimir Nesterovsky
e-mail: Click here to reveal e-mail address
home: www.nesterovsky-bros.com

Reply to this message...
 
    
Ary Sulistyono
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 Guru wrote:
[Original message clipped]

Reply to this message...
 
 




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification