🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
6 messages
6 total messages Started by JD Sat, 06 May 2006 15:14
What happens when ...
#3761
Author: JD
Date: Sat, 06 May 2006 15:14
24 lines
1392 bytes
I'm dealing with some old legacy code and have a couple of
questions because I would never code it as it is.

What's actually happening internally when a local variable is
declared as such:

    char *ptr = "XXX";

The other question is about structure that declares a member of
it's own type within it's definition. For example:

    struct TSomeStruct
    {
        ....
        TSomeStruct *x;
        TSomeStruct *y;
    };

This compiles without warnings or errors but I would expect it
not to because it looks to me like it's an incomplete data type
when it hits x & y.

~ JD
Re: What happens when ...
#3760
Author: Helmut Giese
Date: Sat, 06 May 2006 15:24
32 lines
1924 bytes
Hi JD,
>I'm dealing with some old legacy code and have a couple of
>questions because I would never code it as it is.
Why not? To me it doesn't look bad.
>
>What's actually happening internally when a local variable is
>declared as such:
>
>    char *ptr = "XXX";
The compiler puts the string "XXX" into a segment (which you should
consider non-writable) and puts the address of the string into 'ptr'.
>
>The other question is about structure that declares a member of
>it's own type within it's definition. For example:
It doesn't 'declare a member of its own type' - it declares, or rather
_defines_; a _pointer_ to its own type, which is perfectly ok, since
the compiler knows a pointer's size and can reserve the appropriate
space for it.
>
>    struct TSomeStruct
>    {
>        ....
>        TSomeStruct *x;
>        TSomeStruct *y;
>    };
>
>This compiles without warnings or errors but I would expect it
>not to because it looks to me like it's an incomplete data type
>when it hits x & y.
See above.
HTH
Helmut Giese
Re: What happens when ...
#3762
Author: Michel Leunen
Date: Sat, 06 May 2006 16:55
29 lines
1748 bytes
JD wrote:

> What's actually happening internally when a local variable is
> declared as such:
> 
>     char *ptr = "XXX";

(Besides what others already told you)
The compiler allocates an array of 4 const characters (3 chars + the \0 
byte) and makes ptr points to the first character in this array. For 
backward compatibility, C++ allows you to not prefix your pointer with 
const. Actually,

char* ptr = "XXX";

is silently converted to:

const char* ptr = "XXX";

but it's perfectly legal code although deprecated.

Michel
-- 
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
Re: What happens when ...
#3763
Author: Alan Bellingham
Date: Sat, 06 May 2006 18:33
24 lines
1612 bytes
Michel Leunen <nospam@noreply.please> wrote:

>char* ptr = "XXX";
>
>is silently converted to:
>
>const char* ptr = "XXX";
>
>but it's perfectly legal code although deprecated.

It's more that it's silently converted to 

  char* ptr = const_cast<char*>("XXX");

as can be seen by the fact that you can assign through ptr. If you *do*
assign through ptr, then you're screwed, because you'll either get an
access violation, or a very difficult to find bug. But the compiler
won't stop you.

Alan Bellingham
-- 
Team Thai Kingdom
<url:http://www.borland.com/newsgroups/> Borland newsgroup descriptions
<url:http://www.borland.com/newsgroups/netiquette.html>      netiquette
Re: What happens when ...
#3764
Author: Michel Leunen
Date: Sun, 07 May 2006 09:03
21 lines
1671 bytes
Alan Bellingham wrote:

> It's more that it's silently converted to 
> 
>   char* ptr = const_cast<char*>("XXX");
> 
> as can be seen by the fact that you can assign through ptr. If you *do*
> assign through ptr, then you're screwed, because you'll either get an
> access violation, or a very difficult to find bug. But the compiler
> won't stop you.

Thanks for the correction. It was important.

Michel
-- 
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
Re: What happens when ...
#3765
Author: Danzer
Date: Mon, 08 May 2006 20:04
30 lines
1698 bytes
> The other question is about structure that declares a member of
> it's own type within it's definition. For example:
> 
>     struct TSomeStruct
>     {
>         ....
>         TSomeStruct *x;
>         TSomeStruct *y;
>     };
> 
> This compiles without warnings or errors but I would expect it
> not to because it looks to me like it's an incomplete data type
> when it hits x & y.
> 
> ~ JD
> 

This structure declaration would be necessary to create a doubly-linked 
list.  More meaning variable name would be

struct TListObject
{
   ...
   TListObject *PrevListObject; // At the head of the list this is NULL.
   TListObject *NextListObject; // At the tail of the list this is NULL.
};

It has been a long time since I've used such a structure.

Danzer
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads