473,657 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string constants / conversion from const char * to String

I want to declare an array of constant strings. In unmanaged C++ I'd
do this

static const char *string[] = { "One", "Two, "Three" };

However if I do this in .NET and want to pass these to a function
which takes an Object * then I get compilation errors. The only way to
avoid these seems to be to create a temporary string on the heap,
e.g.

String *str = new String(string[1]);
function(str);

Which seems a bit wasteful.

If I try to declare my static const array as Strings instead then I
get warnings about not being able to declare managed types as statics.

What is the right way to do this? Is there a better way to declare the
array of constant strings of a better way to convert const char * to
Object?

TIA,
KK

Jun 22 '07 #1
5 3998

<ke***********@ googlemail.comw rote in message
news:11******** **************@ p77g2000hsh.goo glegroups.com.. .
>I want to declare an array of constant strings. In unmanaged C++ I'd
do this

static const char *string[] = { "One", "Two, "Three" };

However if I do this in .NET and want to pass these to a function
which takes an Object * then I get compilation errors. The only way to
avoid these seems to be to create a temporary string on the heap,
e.g.

String *str = new String(string[1]);
function(str);

Which seems a bit wasteful.

If I try to declare my static const array as Strings instead then I
get warnings about not being able to declare managed types as statics.
Is that a function-local static? I know that static strings are allowed.
Please note that const doesn't work in managed code, but there is "initonly"
which is similar. In your case, however, you probably want "literal".
>
What is the right way to do this? Is there a better way to declare the
array of constant strings of a better way to convert const char * to
Object?

TIA,
KK
Jun 22 '07 #2
On 22 Jun, 16:51, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
<kelvin.koo...@ googlemail.comw rote in message

news:11******** **************@ p77g2000hsh.goo glegroups.com.. .
If I try to declare my static const array as Strings instead then I
get warnings about not being able to declare managed types as statics.

Is that a function-local static? I know that static strings are allowed.
Please note that const doesn't work in managed code, but there is "initonly"
which is similar. In your case, however, you probably want "literal".
I was trying

const String *strings[] = {S"One", S"Two", S"Three};

Note I'm using VS 2003.
Jun 22 '07 #3

<ke***********@ googlemail.comw rote in message
news:11******** **************@ g4g2000hsf.goog legroups.com...
On 22 Jun, 16:51, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
><kelvin.koo... @googlemail.com wrote in message

news:11******* *************** @p77g2000hsh.go oglegroups.com. ..
If I try to declare my static const array as Strings instead then I
get warnings about not being able to declare managed types as statics.

Is that a function-local static? I know that static strings are allowed.
Please note that const doesn't work in managed code, but there is
"initonly"
which is similar. In your case, however, you probably want "literal".
I was trying

const String *strings[] = {S"One", S"Two", S"Three};

Note I'm using VS 2003.
That's not good. Microsoft has completely dropped support for Managed
Extensions for C++, and there are known bugs with all mixed-mode assemblies
that cannot be worked around. Everyone really needs to upgrade to C++/CLI
as soon as possible to get the compiler fixes, even if you continue using
the old syntax (/clr:oldSyntax). Visual C++ 2005 Express is preferable to
any edition of 2003 from that regard -- all the optimization in the matter
doesn't work if it deadlocks.

Jun 22 '07 #4
On 22 Jun, 21:39, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
<kelvin.koo...@ googlemail.comw rote in message

news:11******** **************@ g4g2000hsf.goog legroups.com...


On 22 Jun, 16:51, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
<kelvin.koo...@ googlemail.comw rote in message
>news:11******* *************** @p77g2000hsh.go oglegroups.com. ..
If I try to declare my static const array as Strings instead then I
get warnings about not being able to declare managed types as statics.
Is that a function-local static? I know that static strings are allowed.
Please note that const doesn't work in managed code, but there is
"initonly"
which is similar. In your case, however, you probably want "literal".
I was trying
const String *strings[] = {S"One", S"Two", S"Three};
Note I'm using VS 2003.

That's not good. Microsoft has completely dropped support for Managed
Extensions for C++, and there are known bugs with all mixed-mode assemblies
that cannot be worked around. Everyone really needs to upgrade to C++/CLI
as soon as possible to get the compiler fixes, even if you continue using
the old syntax (/clr:oldSyntax). Visual C++ 2005 Express is preferable to
any edition of 2003 from that regard -- all the optimization in the matter
doesn't work if it deadlocks.- Hide quoted text -

- Show quoted text -
Sadly we're stuck with VS 2003. In this instance I'm trying to just
use .NET. My problem is that I'm only familiar with standard C++
syntax and I'm not sure what the right way to declare an array of
constant strings in .NET is.

Jun 25 '07 #5

<ke***********@ googlemail.comw rote in message
news:11******** **************@ n2g2000hse.goog legroups.com...
On 22 Jun, 21:39, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
><kelvin.koo... @googlemail.com wrote in message

news:11******* *************** @g4g2000hsf.goo glegroups.com.. .


On 22 Jun, 16:51, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
<kelvin.koo... @googlemail.com wrote in message
>>news:11****** *************** *@p77g2000hsh.g ooglegroups.com ...
If I try to declare my static const array as Strings instead then I
get warnings about not being able to declare managed types as
statics.
>Is that a function-local static? I know that static strings are
allowed.
Please note that const doesn't work in managed code, but there is
"initonly"
which is similar. In your case, however, you probably want "literal".
I was trying
const String *strings[] = {S"One", S"Two", S"Three};
Note I'm using VS 2003.

That's not good. Microsoft has completely dropped support for Managed
Extensions for C++, and there are known bugs with all mixed-mode
assemblies
that cannot be worked around. Everyone really needs to upgrade to
C++/CLI
as soon as possible to get the compiler fixes, even if you continue using
the old syntax (/clr:oldSyntax). Visual C++ 2005 Express is preferable
to
any edition of 2003 from that regard -- all the optimization in the
matter
doesn't work if it deadlocks.- Hide quoted text -

- Show quoted text -

Sadly we're stuck with VS 2003. In this instance I'm trying to just
use .NET. My problem is that I'm only familiar with standard C++
syntax and I'm not sure what the right way to declare an array of
constant strings in .NET is.
The correct way to do anything in .NET 1.1 is with C#. C++ code on .NET 1.1
suffers from the loader lock bug, and there is no workaround except for
upgrading to .NET 2.0 and VS 2005. Even the "/clr:pure" option that
prevents mixed-mode compilation isn't available until VC++ 2005.

So find whoever is mandating VS 2003, and show them this:
http://www.codeproject.com/scrapbook/semicolon_2.asp
and this: http://msdn2.microsoft.com/en-us/lib...66(vs.80).aspx
and this: http://msdn2.microsoft.com/En-US/lib...48(vs.71).aspx

Specifically:

"Second, when loading DLLs in versions 1.0 and 1.1 of the .NET Framework,
the CLR assumed that the loader lock was not held and performed several
actions that are invalid under loader lock. Assuming the loader lock is not
held is a valid assumption for purely .NET DLLs, but, because mixed DLLs
execute native initialization routines, they require the native Windows
loader and therefore the loader lock. Consequently, even if the developer
was not attempting to execute any MSIL functions during DLL initialization,
there was still a small possibility of nondeterministi c deadlock with
versions 1.0 and 1.1 of the .NET Framework."

"Consequent ly, due to the way the loader works in version 1.0 and 1.1 of the
common language runtime, deadlock situations are always possible with mixed
DLLs, even though they are often quite rare in practice. The worst part of
this is that mixed DLLs that happen to work on most systems can begin
deadlocking if the system is stressed, the image is signed (since the
security functionality require more managed code to run during assembly
load), hooks are installed into the system, or the behavior of the runtime
changes through service packs or new releases. In summary, this is a serious
problem that must be addressed for all mixed DLLs."

I'm sorry, but there is no rationale for staying with VS 2003 that can
possibly trump a latent nondeterministi c deadlock.

Jun 25 '07 #6

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

Similar topics

10
3610
by: Marcin Kalicinski | last post by:
Why string literals are regarded as char * not as const char *? (1) void f(char *); (2) void f(const char *); f("foo") will call version (1) of function f. I understand that the exact type of literal "foo" is char, which by means of standard conversion becomes char *. Still, there's something going wrong here, because this allows modification of "foo", which in my opinion should
34
4981
by: E. Robert Tisdale | last post by:
Please find attached the physical constants header file physical.h It defines conversion factors to mks units. It might be used like this: > cat main.cc #include<iostream> #include"physical.h" int main(int argc, char* argv) {
7
4352
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
8
4339
by: Duncan Winn | last post by:
I am new to VC++7. I am using a method GetPrivateProfileString that requires an LPTSTR. I have defined this as a: char * data_name; I am then trying to convert this to an LPOLESTR and I have done this as follows: LPOLESTR dunk_data = (LPOLESTR)T2CW(data_name);
6
7606
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards, Karthi
2
11703
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; };
4
6123
by: costantinos | last post by:
Hello. I have a string which looks like and I need to read these numbers. The logical implementation was to read the string char by char and check whether I have a space and when I find one I could create a vector with all the numbers (after i convert them to number of course). the problem is that when i am trying to do so I have errors with both implementations that i have tried. for(my_iter = mla.begin(); my_iter != mla.end();...
3
9522
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...
5
2888
by: polas | last post by:
Good morning, I have a quick question to clear up some confusion in my mind. I understand that using a string literal in a declaration such as char *p = "string literal" declares a pointer to memory holding the string and the string might very well be held in read only memory. However, I am sure that I read somewhere that the declaration char a = "string literal", even though a is an array (and I understand the differences between...
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8732
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...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7324
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
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
4151
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...
1
2726
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
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.