473,471 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3186
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
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
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...
1
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
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...
0
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 ...

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.