Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Visual Studio.NET / General / July 2004

Tip: Looking for answers? Try searching our database.

More a C++ Q then vs... but why not?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dead RAM - 07 Jul 2004 11:52 GMT
Well... Don't know C++ as well as a pro... so here goes...

I want to do somthing like this:

-------------------------------------------------------------------
class someThing {
   public:
       // stuff, constructor, destructor, etc...

   private:
       const char    myConstCharArrayInitializedAtObjectCreation[] =
"blah";
};

--------------------------------------------------------------------

After brute forcing my way through the errors I ended up with this:

-------------------------------------------------------------------
class someThing {
   public:
       // stuff, constructor, destructor, etc...

   private:
       const char*    myConstCharArrayInitializedAtObjectCreation;
};

// more stuff

someThing::someThing()
:    myConstCharArrayInitializedAtObjectCreation("blah")
{
// more stuff
}
-------------------------------------------------------------------

I'm mainly wondering, in the second (and error free) version, is it the
equivalent of the C statement:

const char ch[]="blah"

Umm... in other words, where is the "blah" data being stored and setup and
what not? in the data segment and then copied into the heep when the object
is created. And am I running into a situation were the const char wasn't
initialized properly and could potentialy lose the "blah" data (is the
memory pointed to by the const char* valid, or does it just happend to work
sometimes)?

Anyways, if you feel like rambling on about this for me i'd appriciate it ;)
andrew - 07 Jul 2004 22:08 GMT
This question is probably better asked in comp.c++.whawtever... but:

Consider using a static member for this. Initialize it in your class's .cpp
implementation file.

What you are doing seems unsafe to me. Your const char * ctor is being
passed the address of a string. The string itself is implicitly created on
the stack as you are declaring it right in the parameter list for a function
call. So it will go out of scope... or at least any other type would... but
a string may be a special case?? Since it is a string constant it must be
stored in the .exe's data segment somewhere. So is it being copied from
there to a temp. string which is created at time of ctor call? OR is the
direct address of the original string constant being passed? If so you get
lucky and it works.

> Well... Don't know C++ as well as a pro... so here goes...
>
[quoted text clipped - 45 lines]
>
> Anyways, if you feel like rambling on about this for me i'd appriciate it ;)
Alexander Grigoriev - 10 Jul 2004 15:46 GMT
String constants are static.

> This question is probably better asked in comp.c++.whawtever... but:
>
[quoted text clipped - 10 lines]
> direct address of the original string constant being passed? If so you get
> lucky and it works.
Alf P. Steinbach - 07 Jul 2004 22:38 GMT
* Dead RAM:
> Well... Don't know C++ as well as a pro... so here goes...
>
[quoted text clipped - 9 lines]
> "blah";
> };

You can't have an array of indeterminate size as a member.

> --------------------------------------------------------------------
>
[quoted text clipped - 16 lines]
> // more stuff
> }

This is OK.

> -------------------------------------------------------------------
>
> I'm mainly wondering, in the second (and error free) version, is it the
> equivalent of the C statement:
>
> const char ch[]="blah"

No it isn't.  What you have in class someThing is a pointer.  What
you have right above is a character array.


> Umm... in other words, where is the "blah" data being stored

The "blah" has static storage.

The pointer to "blah" is being stored in your object.

One per object.

> and setup

?

> And am I running into a situation were the const char wasn't
> initialized properly and could potentialy lose the "blah" data

Not in the code presented here.

> (is the
> memory pointed to by the const char* valid, or does it just happend to work
> sometimes)?

In the code presented it is valid.

Signature

A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.