BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Pointer to AnsiString
Goto page 1, 2, 3, 4  Next
 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
Phil V
Guest





PostPosted: Thu Dec 04, 2003 12:24 pm    Post subject: Pointer to AnsiString Reply with quote



Sorry guys, have a 'slow' moment here!

I want to be able to have a pointer that points straight at the ansistring
of the caption of a label. How do I do that?

I tried,

AnsiString* TheCaption = Label1->Caption;

But of course this tries to assign the content of the string rather than the
address of the string. I think I might need an '&' in there somewhere, but
not sure where!

As a second point;
If i wanted to have a list of parameters in memory, pointed to directly
like this so as to not need to worry about accessing the actual components
how would I do it, and is it even possible?

Thanks,

Phil V

--

"If it is called software, why is it so hard?" - Unknown


Back to top
Rodolfo Frino
Guest





PostPosted: Thu Dec 04, 2003 2:27 pm    Post subject: Re: Pointer to AnsiString Reply with quote



I am not sure if this is what you want:

Label1->Caption = "Hello Software World,
101010101010101010101010101010---";

String S = Label1->Caption;
String* p = &S;
ShowMessage(*p);

if so, why not using?

String S = Label1->Caption;
ShowMessage(S);

Rodolfo


"Phil V" <pvalentine_REMOVE_THIS_ (AT) _REMOVE_THIS_vislink (DOT) com> wrote

Quote:
Sorry guys, have a 'slow' moment here!

I want to be able to have a pointer that points straight at the ansistring
of the caption of a label. How do I do that?

I tried,

AnsiString* TheCaption = Label1->Caption;

But of course this tries to assign the content of the string rather than
the
address of the string. I think I might need an '&' in there somewhere, but
not sure where!

As a second point;
If i wanted to have a list of parameters in memory, pointed to directly
like this so as to not need to worry about accessing the actual components
how would I do it, and is it even possible?

Thanks,

Phil V

--

"If it is called software, why is it so hard?" - Unknown





Back to top
Phil V
Guest





PostPosted: Thu Dec 04, 2003 2:37 pm    Post subject: Re: Pointer to AnsiString Reply with quote



Ok, I can see what you are doing here, but from the looks of things what you
have done is copied the string from Label1->Caption into S, and then
provided a pointer 'p' to S.

What I was trying to achieve was to be able to have a list of pointers, to
enable an easy update of the value in the components. For example:

String P1* = Label1->Caption;
String P2* = Memo1->Text;

Then allowing for a simply function knowing nothing of the actual components
to do something like:

AnsiString MyTxt = "This is my text"
P1 = MyTxt; // Make the CAPTION property contain "This is my text"
P2 = MyTxt; // Make the TEXT property contain "This is my text"

See, I don't actually want to copy the data, I want to be able to have a
pointer directly to the property. How do i do this?

Thanks,

Phil

"Rodolfo Frino" <MenInBlack (AT) nowhere (DOT) com> wrote

Quote:
I am not sure if this is what you want:

Label1->Caption = "Hello Software World,
101010101010101010101010101010---";

String S = Label1->Caption;
String* p = &S;
ShowMessage(*p);

if so, why not using?

String S = Label1->Caption;
ShowMessage(S);

Rodolfo


"Phil V" <pvalentine_REMOVE_THIS_ (AT) _REMOVE_THIS_vislink (DOT) com> wrote in
message
news:3fcf279d$1 (AT) newsgroups (DOT) borland.com...
Sorry guys, have a 'slow' moment here!

I want to be able to have a pointer that points straight at the
ansistring
of the caption of a label. How do I do that?

I tried,

AnsiString* TheCaption = Label1->Caption;

But of course this tries to assign the content of the string rather than
the
address of the string. I think I might need an '&' in there somewhere,
but
not sure where!

As a second point;
If i wanted to have a list of parameters in memory, pointed to
directly
like this so as to not need to worry about accessing the actual
components
how would I do it, and is it even possible?

Thanks,

Phil V

--

"If it is called software, why is it so hard?" - Unknown







Back to top
Bruce Salzman
Guest





PostPosted: Thu Dec 04, 2003 3:54 pm    Post subject: Re: Pointer to AnsiString Reply with quote

AnsiString* TheCaption = &(Label1->Caption);
AnsiString MyTxt = "This is my text";
*TheCaption = MyTxt;

Regards,
Bruce


Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Thu Dec 04, 2003 4:01 pm    Post subject: Re: Pointer to AnsiString Reply with quote

" Bruce Salzman" <bruce (AT) nospam (DOT) org> writes:

Quote:
AnsiString* TheCaption = &(Label1->Caption);

This is disastrous, since you're taking the address of a temporary,
and it's immediately destroyed after the assignment finishes. The
result is a pointer to an invalid region of memory that used to hold
the temporary.

Quote:
AnsiString MyTxt = "This is my text";
*TheCaption = MyTxt;

This is undefined, since TheCaption points to invalid memory and now
you're dereferencing it.

--
Chris (TeamB);

Back to top
Bruce Salzman
Guest





PostPosted: Thu Dec 04, 2003 4:04 pm    Post subject: Re: Pointer to AnsiString Reply with quote


"Chris Uzdavinis (TeamB)" <chris (AT) uzdavinis (DOT) com> wrote

Quote:
" Bruce Salzman" <bruce (AT) nospam (DOT) org> writes:

AnsiString* TheCaption = &(Label1->Caption);

This is disastrous, since you're taking the address of a temporary,
and it's immediately destroyed after the assignment finishes. The
result is a pointer to an invalid region of memory that used to hold
the temporary.


I thought that only had to do with the pointer returned by c_str()...
Why can't you take the address of the AnsiString itself?



Back to top
Phil V
Guest





PostPosted: Thu Dec 04, 2003 4:04 pm    Post subject: Re: Pointer to AnsiString Reply with quote

Chris,

I can see that in the 'second' section the mistake is dereferencing it, but
what do you mean in the first section where you say it is a temporary? Does
this NOT give me the memory location of the text that is making up the
caption of the label then?

Phil

"Chris Uzdavinis (TeamB)" <chris (AT) uzdavinis (DOT) com> wrote

Quote:
" Bruce Salzman" <bruce (AT) nospam (DOT) org> writes:

AnsiString* TheCaption = &(Label1->Caption);

This is disastrous, since you're taking the address of a temporary,
and it's immediately destroyed after the assignment finishes. The
result is a pointer to an invalid region of memory that used to hold
the temporary.

AnsiString MyTxt = "This is my text";
*TheCaption = MyTxt;

This is undefined, since TheCaption points to invalid memory and now
you're dereferencing it.

--
Chris (TeamB);



Back to top
Liz Albin
Guest





PostPosted: Thu Dec 04, 2003 4:14 pm    Post subject: Re: Pointer to AnsiString Reply with quote

On Thu, 4 Dec 2003 14:37:31 -0000, Phil V wrote:

Quote:
See, I don't actually want to copy the data, I want to be able to have a
pointer directly to the property. How do i do this?

As the property is a temporary, I don't think it's a wise idea.

--
liz

Back to top
Rodolfo Frino
Guest





PostPosted: Thu Dec 04, 2003 5:03 pm    Post subject: Re: Pointer to AnsiString Reply with quote

Perhaps this is what you want

void __fastcall TForm1::UpdatePropertyValues(String* p)
{
Label1->Caption = p[0];
Edit1->Text = p[1];
Button2->Caption = p[2];
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
String* p = new String[1000];

p[0] = "TLABEL";
p[1] = "TEDIT";
p[2] = "TBUTTON";

UpdatePropertyValues(p);

p[0] = "New Label caption";
p[1] = "New Edit1 text";
p[2] = "New Button2 caption";

UpdatePropertyValues(p);
}


Rodolfo


"Phil V" <pvalentine_REMOVE_THIS_ (AT) _REMOVE_THIS_vislink (DOT) com> wrote

Quote:
Ok, I can see what you are doing here, but from the looks of things what
you
have done is copied the string from Label1->Caption into S, and then
provided a pointer 'p' to S.

What I was trying to achieve was to be able to have a list of pointers, to
enable an easy update of the value in the components. For example:

String P1* = Label1->Caption;
String P2* = Memo1->Text;

Then allowing for a simply function knowing nothing of the actual
components
to do something like:

AnsiString MyTxt = "This is my text"
P1 = MyTxt; // Make the CAPTION property contain "This is my text"
P2 = MyTxt; // Make the TEXT property contain "This is my text"

See, I don't actually want to copy the data, I want to be able to have a
pointer directly to the property. How do i do this?

Thanks,

Phil

"Rodolfo Frino" <MenInBlack (AT) nowhere (DOT) com> wrote in message
news:3fcf4454 (AT) newsgroups (DOT) borland.com...
I am not sure if this is what you want:

Label1->Caption = "Hello Software World,
101010101010101010101010101010---";

String S = Label1->Caption;
String* p = &S;
ShowMessage(*p);

if so, why not using?

String S = Label1->Caption;
ShowMessage(S);

Rodolfo


"Phil V" <pvalentine_REMOVE_THIS_ (AT) _REMOVE_THIS_vislink (DOT) com> wrote in
message
news:3fcf279d$1 (AT) newsgroups (DOT) borland.com...
Sorry guys, have a 'slow' moment here!

I want to be able to have a pointer that points straight at the
ansistring
of the caption of a label. How do I do that?

I tried,

AnsiString* TheCaption = Label1->Caption;

But of course this tries to assign the content of the string rather
than
the
address of the string. I think I might need an '&' in there somewhere,
but
not sure where!

As a second point;
If i wanted to have a list of parameters in memory, pointed to
directly
like this so as to not need to worry about accessing the actual
components
how would I do it, and is it even possible?

Thanks,

Phil V

--

"If it is called software, why is it so hard?" - Unknown









Back to top
Bruce Salzman
Guest





PostPosted: Thu Dec 04, 2003 5:10 pm    Post subject: Re: Pointer to AnsiString Reply with quote

"Phil V" <pvalentine_REMOVE_THIS_ (AT) _REMOVE_THIS_vislink (DOT) com> wrote

Quote:
Chris,

I can see that in the 'second' section the mistake is dereferencing it,
but
what do you mean in the first section where you say it is a temporary?
Does
this NOT give me the memory location of the text that is making up the
caption of the label then?


Right...the properties of a TForm ARE temporaries! This pointer
assignment/dereferencing would be OK for an AnsiString that stays in scope.
The TForm property objects only exist long enough to bridge the gap between
C++ and the Delphi code under the hood. It's easy to forget those tricks
that VCL needs to mimic C++ classes. But I guess the moral is not to forget
that they are not C++ classes! I miss OWL. :^(

Regards,
Bruce



Back to top
Pete Fraser
Guest





PostPosted: Thu Dec 04, 2003 5:34 pm    Post subject: Re: Pointer to AnsiString Reply with quote

Probably because it's not a variable it's a 'property' which has a hidden
getter and setter.
The getter returns a temporary AnsiString which is what you have a pointer
to so is of no use whatsoever.
HTH Pete

" Bruce Salzman" <bruce (AT) nospam (DOT) org> wrote

Quote:

"Chris Uzdavinis (TeamB)" <chris (AT) uzdavinis (DOT) com> wrote in message
news:j5ad68incm.fsf (AT) explicit (DOT) atdesk.com...
" Bruce Salzman" <bruce (AT) nospam (DOT) org> writes:

AnsiString* TheCaption = &(Label1->Caption);

This is disastrous, since you're taking the address of a temporary,
and it's immediately destroyed after the assignment finishes. The
result is a pointer to an invalid region of memory that used to hold
the temporary.


I thought that only had to do with the pointer returned by c_str()...
Why can't you take the address of the AnsiString itself?





Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Thu Dec 04, 2003 5:39 pm    Post subject: Re: Pointer to AnsiString Reply with quote

"Phil V" <pvalentine_REMOVE_THIS_ (AT) _REMOVE_THIS_vislink (DOT) com> writes:

Quote:
Chris,

I can see that in the 'second' section the mistake is dereferencing it, but
what do you mean in the first section where you say it is a temporary? Does
this NOT give me the memory location of the text that is making up the
caption of the label then?

Asking for the Caption from a label returns a temporary AnsiString, I
think. Since AnsiStrings are reference counted, and the string's not
copied "by value" the reference count isn't increased, so when the
return value is destroyed, the refcount goes to 0 and the string is
deleted.

Taking its address doesn't affect the refcount. Since AnsiStrings are
very lightweight anyway, copying them by value is rather cheap, and
avoids all sorts of problems. (The data isn't copied, since it's
refcounted. Copying an AnsiString basically involves only copying a
pointer, and increasing the refcount.)

--
Chris (TeamB);

Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu Dec 04, 2003 6:55 pm    Post subject: Re: Pointer to AnsiString Reply with quote


"Phil V" <pvalentine_REMOVE_THIS_ (AT) _REMOVE_THIS_vislink (DOT) com> wrote


Quote:
I want to be able to have a pointer that points straight at
the ansistring of the caption of a label.

Can't be done, sorry. The Caption is not an actual AnsiString instance to
begin with, so you can't assign a pointer to it. Instead, the Caption is a
pair of getter and setter methods that work with temporary AnsiString
instances and retreive/update the caption text internally.


Gambit



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu Dec 04, 2003 6:56 pm    Post subject: Re: Pointer to AnsiString Reply with quote


" Bruce Salzman" <bruce (AT) nospam (DOT) org> wrote

Quote:
AnsiString* TheCaption = &(Label1->Caption);

That won't work. You are taking the address of a *temporary* value, not the
property directly.

Quote:
*TheCaption = MyTxt;

You are then updating the *temporary* value, not the property directly.


Gambit



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu Dec 04, 2003 7:02 pm    Post subject: Re: Pointer to AnsiString Reply with quote


" Bruce Salzman" <bruce (AT) nospam (DOT) org> wrote


Quote:
I thought that only had to do with the pointer returned by c_str()...

That is a different issue than the one being discussed here. The c_str()
method returns a pointer to the AnsiString's internal memory buffer. The
returned pointer is only valid as long as the AnsiString is not modified.
As soon as the AnsiString is modified, its internal buffer is (usually)
reallocated, and the pointer you obtained is no longer pointing to the same
memory as before. This all assumes, however, that the AnsiString itself
remains in memory the whole time you are using it. On the other hand...

Quote:
Why can't you take the address of the AnsiString itself?

You can, but that is not the problem. As Chris already mentioned, the
AnsiString returned by the Caption property is a *temporary* instance.
Since you are not assigning it to a separate AnsiString variable, the
temporary is going to go out of scope as soon as the statement is finished.
When AnsiString goes out of scope, it is automatically freed, and you will
be left with a pointer that is pointing to invalid memory since the
AnsiString no longer exists. Anything you do to access that pointer will
result in undefined behavior if you do not reassign it to valid memory
first.


Gambit



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Goto page 1, 2, 3, 4  Next
Page 1 of 4

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.