|
Prev: UTF8 and std::string
Next: weird static cast
From: pramod on 19 Jun 2006 10:00 I don't know if this falls in this group or not but if someone can answer it will be appreciated. What's the difference between: const char* pS1 = "abc"; and const char pS2[] = "abc"; Which one goes to which segment? I believe there's a storage created for pS1 but not necessarily for pS2. Also pS1 goes into data segment whereas string "abc" goes into the constant segment. Can there be any system in which data segment is missing? In that case, how will these two statements be treated? How are the pS1 (or pS2) symbols transformed in binary? Are they offsets from the program base? Is there any exception to this? When should one be preferred over the other? Thanks [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Pete Becker on 20 Jun 2006 05:43 pramod wrote: > I don't know if this falls in this group or not but if someone can > answer it will be appreciated. > > What's the difference between: > const char* pS1 = "abc"; and > const char pS2[] = "abc"; > > Which one goes to which segment? I believe there's a storage created > for pS1 but not necessarily for pS2. Also pS1 goes into data segment > whereas string "abc" goes into the constant segment. > > Can there be any system in which data segment is missing? In that case, > how will these two statements be treated? How are the pS1 (or pS2) > symbols transformed in binary? Are they offsets from the program base? > Is there any exception to this? > > When should one be preferred over the other? > The string "abc" is a literal string. It gets stored wherever literal strings get stored. The statement const char *pS1 = "abc"; allocates storage (either automatic or static, depending on whether the statement is inside a function or not) for the object pS1, which is then initialized to point to the literal string "abc". The statement const char pS2[] = "abc"; allocates storage (either automatic or static, depending on whether the statement is inside a function or not) for the object ps2, which is then initialized by copying the contents of the literal string "abc". -- Pete Becker Roundhouse Consulting, Ltd. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Heinz Ozwirk on 20 Jun 2006 05:45 "pramod" <Pramod.Patangay(a)gmail.com> schrieb im Newsbeitrag news:1150720388.856256.140770(a)h76g2000cwa.googlegroups.com... > > I don't know if this falls in this group or not but if someone can > answer it will be appreciated. > > What's the difference between: > const char* pS1 = "abc"; and > const char pS2[] = "abc"; The first definition defines a pointer to a "string" (actually a pointer to a constant char, which happens to be the first of a sequence of characters) and makes it point to the first element of an array with for elements ('a', 'b', 'c' and '\0'). The second defines an array of four characters and initializes its elements with 'a', 'b', 'c' and '\0'. Where and how these arrays and variables are allocated depends on the compiler, the operating system, the program should run on, and to some extent where in your C++ code those definitions are used. > Which one goes to which segment? I believe there's a storage created > for pS1 but not necessarily for pS2. Also pS1 goes into data segment > whereas string "abc" goes into the constant segment. > Can there be any system in which data segment is missing? In that case, > how will these two statements be treated? How are the pS1 (or pS2) > symbols transformed in binary? Are they offsets from the program base? > Is there any exception to this? This depends on the operating system and how code produced by the compiler co-orporates with it. It is beyond the scope of C++. > When should one be preferred over the other? Prefer whatever the compiler will do. Implement you algorithms as good as you can and let the compiler worry about all those minor details. HTH Heinz [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Thomas Maeder on 20 Jun 2006 05:51 "pramod" <Pramod.Patangay(a)gmail.com> writes: > I don't know if this falls in this group or not but if someone can > answer it will be appreciated. > > What's the difference between: > const char* pS1 = "abc"; and > const char pS2[] = "abc"; The first line defines the pointer pS1; it is initialized with the address of the first character of the string literal "abc". The second line defines the array pS2; it is initialized as a copy of the string literal "abc". > Which one goes to which segment? Whether "segments" are used and, if yes, where what goes, is entire implementation defined. > I believe there's a storage created for pS1 but not necessarily for > pS2. There is memory allocated for both. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Maxim Yegorushkin on 20 Jun 2006 05:51
pramod wrote: > I don't know if this falls in this group or not but if someone can > answer it will be appreciated. > > What's the difference between: > const char* pS1 = "abc"; and > const char pS2[] = "abc"; > > Which one goes to which segment? I believe there's a storage created > for pS1 but not necessarily for pS2. Also pS1 goes into data segment > whereas string "abc" goes into the constant segment. > > Can there be any system in which data segment is missing? In that case, > how will these two statements be treated? How are the pS1 (or pS2) > symbols transformed in binary? Are they offsets from the program base? > Is there any exception to this? > > When should one be preferred over the other? http://people.redhat.com/drepper/dsohowto.pdf gives an explanation why the second definition is preferred. It is one less relocation for a dynamic linker. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |