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

using std::string in VC++.Net causes exception.

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 other.

i have two functions in the same .cpp file

//exception occurs at this function
void call(const char* var2 )
{
std::string var1;

( var2 == NULL ) ? var1= "itsme": var1= var2 ; //exception occurs at
this line "var1 = "itstime"
.......
.....
}

//exception does not occur at this function
void call1(const char* var2 )
{
std::string var1;

( var2 == NULL ) ? var1= "itsme": var1= var2 ;
.......
.....
}

the exception is CRT exception. There is some problem regarding
allocation which i am not able to figure out.This error happens at
several other places when i am trying to assign a value to string eg.
(string temp = " hi bye" ).
please help me reagarding this..

regards,
doubts.

Sep 11 '06 #1
3 3178
doubts wrote:
Hi all,
I am trying to convert my bulk of code from VC++ 6.0 to VC++.Net.
Is that 2002 or 2003? Why not go the whole hog and use VC++8, e.g.
Visual Studio 2005 (no .NET in the name this time!).
when using std::string type variable, the application causes exception
at one instance and does not cause an exception at other.

i have two functions in the same .cpp file

//exception occurs at this function
void call(const char* var2 )
{
std::string var1;

( var2 == NULL ) ? var1= "itsme": var1= var2 ; //exception occurs at
this line "var1 = "itstime"
That would normally be written:

var1 = ( var2 == NULL ) ? "itsme": var2 ;

but the semantics are the same either way.
......
....
}

//exception does not occur at this function
void call1(const char* var2 )
{
std::string var1;

( var2 == NULL ) ? var1= "itsme": var1= var2 ;
......
....
}

the exception is CRT exception. There is some problem regarding
allocation which i am not able to figure out.This error happens at
several other places when i am trying to assign a value to string eg.
(string temp = " hi bye" ).
please help me reagarding this..
The code you've posted is fine, if unorthodox. That means that the
problem is likely to be heap corruption (when you allocate or deallocate
memory from the heap, it sometimes manages to detect corruption that has
occured at some previous time). Possible causes of heap corruption are
bugs in your code (such as double deletes, uses of uninitialized or
deleted pointers, array bounds overwrites) or in your configuration
(mixing modules (exes and dlls) using different DLL CRTs, or using
static CRTs).

Tom
Sep 11 '06 #2
Ray
doubts wrote:
Hi all,
I am trying to convert my bulk of code from VC++ 6.0 to VC++.Net.
Define conversion? Your bulk of code is still native C++ to native C++,
yes? (That is no managed C++/CLI stuff?)
when using std::string type variable, the application causes exception
at one instance and does not cause an exception at other.
Try stepping into the function that causes the exception, and examine
the values of the variables involved just before you execute the line
that triggers the exception--I wonder which exactly CRT exception you
are referring to?
i have two functions in the same .cpp file

//exception occurs at this function
void call(const char* var2 )
{
std::string var1;

( var2 == NULL ) ? var1= "itsme": var1= var2 ; //exception occurs at
this line "var1 = "itstime"
......
....
}

//exception does not occur at this function
void call1(const char* var2 )
{
std::string var1;

( var2 == NULL ) ? var1= "itsme": var1= var2 ;
......
....
}

the exception is CRT exception. There is some problem regarding
allocation which i am not able to figure out.This error happens at
several other places when i am trying to assign a value to string eg.
(string temp = " hi bye" ).
I agree with Tom, your code looks OK, although personally I wouldn't
code it that way. Assigning a value to a string shouldn't raise a CRT
exception under normal situations, so are you sure that this is
isolated? I think it's likely that it's not this assignment per se that
causes the exception, rather it's something else.

Cheers
Ray

please help me reagarding this..

regards,
doubts.
Sep 11 '06 #3
I noticed that when I assign string literals to string variables in
VC++.NET, I have to convert them into objects first...

ie:

( var2 == NULL ) ? var1= new System::String("itsme"): var1= var2 ;

I used System::String in your example, but you may need to use
std::string(). System::String is based on the standard template
library and should be compatible with your code.

Andy

B.T.W.
I'm having trouble accessing an unmanaged long from a managed class in
VC++.NET

When I do, the contents of the variable seem to be mangled. If I
access the same variable byte-by-byte, I get the correct value.
Regardless what I set the variable to, the value that is returned for a
long is always the same value. What's going on...can anyone help me?

A short version of the code follows:

//HEADER
namespace MyProgram
{

#pragma unmanaged
__nogc class unmanagedClass
{
public:unmanagedClass(); //CONSTRUCTOR

public: union{
struct {
long unmanagedLong; //UNMANAGED VARIABLE
} myData;
struct {
char bytes[4];
} b_myData;
} myUnion;
};
#pragma managed
public __gc class managedClass
{
public:managedClass(); //CONSTRUCTOR
private: unmanagedClass __nogc *ptrUnmanagedClass; //PTR TO UNMANAGED
CLASS
public:System::String* Get_unmanagedLong(); //METHOD TO GET
UNMANAGED VARIAHBLE
};
}

//CPP LISTING

//CONSTRUCTORS
MyProgram::unmanagedClass::unmanagedClass(){
unmanagedLong=1536; //HEX #0600
}
MyProgram::managedClass::managedClass(){
ptrUnmanagedClass=new unmanagedClass();
}
System::String* MyProgram::managedClass::Get_unmanagedLong(){
System::String *result;

//NEXT RETURNS CORRECT VALUE OF #0600
result=System::String::Concat(

System::String::Format("{0:x}",__box(ptrUnmanagedC lass->myUnion.b_myData.bytes[0])),

System::String::Format("{0:x}",__box(ptrUnmanagedC lass->myUnion.b_myData.bytes[1])),

System::String::Format("{0:x}",__box(ptrUnmanagedC lass->myUnion.b_myData.bytes[2])),

System::String::Format("{0:x}",__box(ptrUnmanagedC lass->myUnion.b_myData.bytes[3])));

//NEXT RETURNS INCORRECT VALUE OF #73CB6A62
result=System::String::Format("{0:x}",__box(ptrUnm anagedClass->myUnion.myData.unmanagedLong));

return(result);
}

Sep 12 '06 #4

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

Similar topics

13
by: Dave | last post by:
Can anybody tell me why the code below should produce the error below? I'm going nuts here! Am I missing something incredibly obvious and simple? std::string::size_type idx; std::string...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
18
by: JKop | last post by:
Can some-one please point me to a nice site that gives an exhaustive list of all the memberfunctions, membervariables, operators, etc. of the std::string class, along with an informative...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
2
by: Default User | last post by:
I received some code from another programmer who happens to be out of town. In this, he had something like: struct st { std::string s<20>; }; Looks like it's setting the string size.
5
by: Torben Laursen | last post by:
Hi Can anyone show me how to convert between VARIANT and std::string and back, thanks! Torben ---
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...
0
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 function and does not cause an exception at...
16
by: yu_kuo | last post by:
Is there any comparison data on perfomance difference between std::string and c style string? Or maybe if there are source code which could be used to measuer on different compiler/platform, in a...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
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...

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.