what would be the output for the following piece of code ?
if ( "hello" == "hello" )
printf("True\n");
else
printf("False\n");
What is the reason for that ?
-----------------------------------------------------
Secondly, consider the following piece of code.
void main(void)
{
char *str= "hello world";
}
I have seen may C tutorials that say that "str" cannot be
modified. What is the reason for that ? I tried this piece of
code on my machine and I was successfully able to modify it. 8 1759 ju**********@yahoo.co.in wrote: what would be the output for the following piece of code ?
if ( "hello" == "hello" ) printf("True\n"); else printf("False\n");
Implementation defined.
What is the reason for that ?
Identical string literals may either refer to same
or different objects.
----------------------------------------------------- Secondly, consider the following piece of code.
void main(void) { char *str= "hello world";
} I have seen may C tutorials that say that "str" cannot be modified. What is the reason for that ? I tried this piece of code on my machine and I was successfully able to modify it.
Attempts to modify the string "hello world"
yield undefined behavior, that's bad.
--
pete
Note: a better subject may be "questions about string literals" ju**********@yahoo.co.in wrote: what would be the output for the following piece of code ?
if ( "hello" == "hello" ) printf("True\n"); else printf("False\n");
It depends. You are essentially comparing the address of
the first char of a string literal "hello"(1) with the
address of the first char of a string literal "hello"(2).
If the compiler decided to have them at two separate locations
in memory, the addresses are not equal. If the compiler
throws all string literals which would return 0 when passed
to strcmp() together into a single one, the addresses are
equal.
What is the reason for that ?
The C standard does not specify which of the above has to
hold, so it is essentially arbitrary. If you want to
compare string literals with strings or other string literals,
use strcmp().
----------------------------------------------------- Secondly, consider the following piece of code.
void main(void)
Wrong. There is only one main() function in a C program
and it has return type int. From here on, everything
else is pure chance.
{ char *str= "hello world";
} I have seen may C tutorials that say that "str" cannot be modified. What is the reason for that ? I tried this piece of code on my machine and I was successfully able to modify it.
Well, the above piece of code -- even with main() returning
int -- does not modify the string literal.
Be that as it may:
- "hello world" is a string literal.
- string literals have static storage duration, i.e. exist
throughout the whole lifetime of the program
- the C standard says that trying to modify a string literal
invokes undefined behaviour -- so successfully modifying
the literal is definitely one possibility. Unfortunately,
a string literal may be stored in memory which cannot be
modified by your program which in turn could lead to your
program crashing just before giving you the output of some
five million years' worth of computation time... IYKWIM
Bottom line: Do not do it. If you want to modify it, use a
string literal as initialiser for a char array instead.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Michael Mair wrote: Note: a better subject may be "questions about string literals"
ju**********@yahoo.co.in wrote: what would be the output for the following piece of code ?
if ( "hello" == "hello" ) printf("True\n"); else printf("False\n");
It depends. You are essentially comparing the address of the first char of a string literal "hello"(1) with the address of the first char of a string literal "hello"(2).
If the compiler decided to have them at two separate locations in memory, the addresses are not equal. If the compiler throws all string literals which would return 0 when passed to strcmp() together into a single one, the addresses are equal.
What is the reason for that ?
The C standard does not specify which of the above has to hold, so it is essentially arbitrary. If you want to compare string literals with strings or other string literals, use strcmp().
----------------------------------------------------- Secondly, consider the following piece of code.
void main(void)
Wrong. There is only one main() function in a C program and it has return type int. From here on, everything else is pure chance.
{ char *str= "hello world";
} I have seen may C tutorials that say that "str" cannot be modified. What is the reason for that ? I tried this piece of code on my machine and I was successfully able to modify it.
Well, the above piece of code -- even with main() returning int -- does not modify the string literal. Be that as it may: - "hello world" is a string literal. - string literals have static storage duration, i.e. exist throughout the whole lifetime of the program - the C standard says that trying to modify a string literal invokes undefined behaviour -- so successfully modifying the literal is definitely one possibility. Unfortunately, a string literal may be stored in memory which cannot be modified by your program which in turn could lead to your program crashing just before giving you the output of some five million years' worth of computation time... IYKWIM Bottom line: Do not do it. If you want to modify it, use a string literal as initialiser for a char array instead.
Thanx for the help Michael. One thing which is not clear is that
why a string literal is stored in non modifiable memory.
What if I don't use any ROM for the execution of my program.
Then can't I modify it ? ju**********@yahoo.co.in wrote: Michael Mair wrote: Well, the above piece of code -- even with main() returning int -- does not modify the string literal. Be that as it may: - "hello world" is a string literal. - string literals have static storage duration, i.e. exist throughout the whole lifetime of the program - the C standard says that trying to modify a string literal invokes undefined behaviour -- so successfully modifying the literal is definitely one possibility. Unfortunately, a string literal may be stored in memory which cannot be modified by your program which in turn could lead to your program crashing just before giving you the output of some five million years' worth of computation time... IYKWIM Bottom line: Do not do it. If you want to modify it, use a string literal as initialiser for a char array instead.
Thanx for the help Michael. One thing which is not clear is that why a string literal is stored in non modifiable memory. What if I don't use any ROM for the execution of my program. Then can't I modify it ?
No. They can be stored wherever the compiler likes to store them.
That could easily by a place in memory you don't have permissions
to write to. They may be stored e.g. in some part of the memory
marked for code only and - to avoid e.g. the program overwriting
it's own code - this part of the memory being marked as read-only.
Moreover the compiler is allowed to concatenate string literals.
E.g. if you have two string literals like
char *a = "hello world";
char *b = "world";
the compiler could make a single string "hello world" out of it,
with 'a' pointing to the start of it and 'b' to the 7th char in
the same literal string. Now, if you would change the literal
string using pointer 'b' you would also change what 'a' is
pointing to - which is definitely not what you would expect.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de ju**********@yahoo.co.in wrote: Michael Mair wrote:
Note: a better subject may be "questions about string literals"
ju**********@yahoo.co.in wrote:
[snip]----------------------------------------------------- Secondly, consider the following piece of code.
void main(void)
Wrong. There is only one main() function in a C program and it has return type int. From here on, everything else is pure chance.
{ char *str= "hello world";
} I have seen may C tutorials that say that "str" cannot be modified. What is the reason for that ? I tried this piece of code on my machine and I was successfully able to modify it.
Well, the above piece of code -- even with main() returning int -- does not modify the string literal. Be that as it may: - "hello world" is a string literal. - string literals have static storage duration, i.e. exist throughout the whole lifetime of the program - the C standard says that trying to modify a string literal invokes undefined behaviour -- so successfully modifying the literal is definitely one possibility. Unfortunately, a string literal may be stored in memory which cannot be modified by your program which in turn could lead to your program crashing just before giving you the output of some five million years' worth of computation time... IYKWIM Bottom line: Do not do it. If you want to modify it, use a string literal as initialiser for a char array instead.
Thanx for the help Michael. One thing which is not clear is that why a string literal is stored in non modifiable memory. What if I don't use any ROM for the execution of my program. Then can't I modify it ?
The thing is that you could for example write software which
ends up stored in ROM, for example some library routines for
a library which comes with the hardware or the program
governing some chip's behaviour in your
car/washing machine/CD player or just the firmware of some
electronic toy of yours. Alternatively, your system might
provide faster access to memory pages in a read-only mode...
There are plenty of applications, so it makes some kind of
sense. The fact that this needs not to be marked clearly by,
say, having string literals accessed only by const char*
is due to historical reasons.
If you get away with modifying a string literal, then you are
unlucky -- on the next computer or with the next version of
your compiler, this may be different.
So, if you want to "do" something with string literals, use
them to initialise arrays of char; if you need only the literal,
then you might be better of with const char*, i.e.
const char *not_to_be_modified = "42";
char buf[] = "State: Initial ";
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 ju**********@yahoo.co.in writes: One thing which is not clear is that why a string literal is stored in non modifiable memory. What if I don't use any ROM for the execution of my program. Then can't I modify it ?
This is implementation-defined.
On GNU/Linux, for example, string literals will be stored in the
..rodata (read-only data) section of the ELF binary, and the run-time
linker ld.so will mmap() this section read-only (PROT_READ), unlike
..text (code) which will be readable and executable, but not writable
(PROT_READ|PROT_EXEC), or .data (modifiable data) which will be
(PROT_READ|PROT_WRITE). An attempt to modify (or execute) the string
literal will cause a segmentation violation and a core dump, due to
the read-only memory protection.
Other systems will do this differently: you'll need to check what your
specific compiler, linker and operating system do. In general,
modifying a string literal is always going to be a Bad Idea; current
versions of GCC don't allow it at all.
Regards,
Roger
- --
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>
iD8DBQFCiN6gVcFcaSW/uEgRAlmWAKDR3bt1xftM5saC49+xhMBM7W1MjQCeJEbF
mTjY6wQp/eAQYaEIFZldln8=
=/Nq6
-----END PGP SIGNATURE-----
Roger Leigh <${*******@invalid.whinlatter.ukfsn.org.invalid> writes: ju**********@yahoo.co.in writes:
One thing which is not clear is that why a string literal is stored in non modifiable memory. What if I don't use any ROM for the execution of my program. Then can't I modify it ?
This is implementation-defined.
On GNU/Linux, for example, string literals will be stored in the .rodata (read-only data) section of the ELF binary, and the run-time linker ld.so will mmap() this section read-only (PROT_READ), unlike .text (code) which will be readable and executable, but not writable (PROT_READ|PROT_EXEC), or .data (modifiable data) which will be (PROT_READ|PROT_WRITE). An attempt to modify (or execute) the string literal will cause a segmentation violation and a core dump, due to the read-only memory protection.
Unless you use the gcc option that causes it to put string literals in
read-write memory. (I believe this option has been removed in the
latest version.)
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
On 16 May 2005 05:05:19 -0700, ju**********@yahoo.co.in wrote: what would be the output for the following piece of code ?
if ( "hello" == "hello" ) printf("True\n"); else printf("False\n");
What is the reason for that ?
It is an implementation detail whether there are one or two strings
with the value "hello" in your program. ----------------------------------------------------- Secondly, consider the following piece of code.
void main(void)
int main(void)
{ char *str= "hello world";
str = "goodbye"; /* There is nothing wrong with this statement. It
is perfectly legal to modify str as often as you want. */
str[0] = 'G'; /* This statement is flawed. str points to a string
literal. While the literal is not formally defined as const, it
should be treated as such. The standard states that any attempt to
modify a string literal results in undefined behavior. */
} I have seen may C tutorials that say that "str" cannot be modified. What is the reason for that ? I tried this piece of code on my machine and I was successfully able to modify it.
The code you posted does not modify either str or the data it points
to. What code did you try?
<<Remove the del for email>> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ambika |
last post by:
Hello,
Am not very good with pointers in C,but I have a small doubt about
the way these pointers work..
We all know that in an array say x,x is gonna point to the first
element in that...
|
by: BILL |
last post by:
Hi Everyone,
I've been looking through these .NET groups and can't find the exact
answer I want, so I'm asking.
Can someone let me know the best way (you feel) to search a C# string
for an...
|
by: RKS |
last post by:
I am new to Com programming using C++. Can pointers be passed through
interface functions. I would like to pass multi dimensional arrays to
functions defined in the com interface. Any help is...
|
by: Pedro Pinto |
last post by:
Hi there!
I'm creating a function that copies some information to a buffer and
enters a delimiter string "//" between results.
My issue here is when i print the buffer it appears this weird...
|
by: reji_thomas |
last post by:
Hi,
I have a doubt in the following code:
struct xyz
{
int x;
long l;
float f;
};
| |
by: Deep |
last post by:
I'm in doubt about what is smart pointer. so, please give me simple
description about smart pointer and an example of that. I'm just
novice in c++.
regards,
John.
|
by: IsRaEl |
last post by:
Hello everyone,
I'm with a lot of questions about how to design a n-Tier, how to do
the code how to organize, and all the samples that i find, are with
only one table, one class with DataAcess,...
|
by: ashwini1680 |
last post by:
Hi,
I have a very basic problem related to strings using pointers in c++
//////////////////
i wrote code as,
int *i_ptr;
char *c_ptr;
int i=5;char c='A';
i_ptr=&i;
c_ptr=&c;
|
by: nembo kid |
last post by:
In the following function, s shouldn't be a pointer costant (array's name)?
So why it is legal its increment? Thanks in advance.
/* Code starts here */
void chartobyte (char *s) {
while...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
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...
| |
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...
| |