473,944 Members | 2,790 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read-only string

why the following string, 'str' is read-only?

char *str = "test string";

anyhow 'str' needs to be in memory. do you think, making 'str' red-only
would gain performance? or, it is right?

Jun 21 '06
24 2730
Rk

Andrew Poelstra wrote:
Well, being as the compiler is allowed to put "test string" wherever it
wants, it could elect to put it within the executable code.


Directly not related to the main topic, but while we are at it I feel
like clarifying this question of mine. I and my friend the other day
were discussing, where "test string" would be stored. In which part of
the memory.
After going through few tutorials on internet, I concluded, if it is
local declaration, the "test string" should resides in STACK. If it is
global or static declaration then, it should reside in "HEAP". In case
we are not using "heap" (in embedded systems we do not), then I could
just say that it stays in RAM.
Now you said it could be in executable code, which was what my friend
guessed.
Could you please clarify.
Thanks.
======
Rk
I am logged in therefore I am.

Jun 22 '06 #21
Rk wrote:
Andrew Poelstra wrote:
Well, being as the compiler is allowed to put "test string" wherever it
wants, it could elect to put it within the executable code.
Directly not related to the main topic, but while we are at it I feel
like clarifying this question of mine. I and my friend the other day
were discussing, where "test string" would be stored. In which part of
the memory.
After going through few tutorials on internet, I concluded, if it is
local declaration, the "test string" should resides in STACK. If it is
global or static declaration then, it should reside in "HEAP". In case
we are not using "heap" (in embedded systems we do not), then I could
just say that it stays in RAM.


The compiler can put the string "test string" wherever it likes. All
that is required is that there is a single location, it has the
required contents, and so long as the program exists (or at least
has a reference to that location or a character within it) the
location exists too - "static storage".

Note that if the declaration

char *str = "test string";

is function-local, it's /str/ that's function-local, not "test string".
In particular it would generally be unsound to store the characters
"test string" [plus terminating 0] on /that function call's/ "stack
frame" (if the implementation has one).
Now you said it could be in executable code, which was what my friend
guessed.


Yes. It /could/ be. Or it /could/ be in some chunk consisting of all
the initialised data from the program. Or it /could/ be in a chunk
consisting of all the initialised and non-writeable data from the
program. Such chunks might be allocated from the same part of the
address space as the "heap" (if there is one) or the "stack" (ditto).

--
Chris "seeker" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/

Jun 22 '06 #22
Chris Dollin wrote:

Rk wrote:
Andrew Poelstra wrote:
Well, being as the compiler is allowed to put "test string" wherever it
wants, it could elect to put it within the executable code.
Directly not related to the main topic, but while we are at it I feel
like clarifying this question of mine. I and my friend the other day
were discussing, where "test string" would be stored. In which part of
the memory.
After going through few tutorials on internet, I concluded, if it is
local declaration, the "test string" should resides in STACK.
If it is global or static declaration then,
it should reside in "HEAP". In case
we are not using "heap"
(in embedded systems we do not), then I could
just say that it stays in RAM.


The compiler can put the string "test string" wherever it likes. All
that is required is that there is a single location,


That depends on what you mean by "single location".
("test string" == "test string") is not guaranteed to be true.

it has the
required contents, and so long as the program exists (or at least
has a reference to that location or a character within it) the
location exists too - "static storage".

Note that if the declaration

char *str = "test string";

is function-local, it's /str/ that's function-local,
not "test string".


Yes.
The return values of func1 and func2 are defined.
The return value of func3 is not defined.
char *func1(void)
{
char *str = "test string";
return str;
}

char *func2(void)
{
return "test string";
}

char *func3(void)
{
char str[] = "test string";
return str;
}

--
pete
Jun 22 '06 #23
pete wrote:
Chris Dollin wrote:

The compiler can put the string "test string" wherever it likes. All
that is required is that there is a single location,


That depends on what you mean by "single location".
("test string" == "test string") is not guaranteed to be true.


Indeed. But in

char *spoo(void) { return "fresh spoo"; }

each call of `spoo` returns the same value: that particular string
`fresh spoo` has only one location during the lifetime of the
program, which is what I was trying to say.

I agree that I hadn't said clearly what I meant.

--
Chris "mud, mud, glorious\\\\\\\ \muddy mud" Dollin
"No-one here is exactly what he appears." G'kar, /Babylon 5/

Jun 22 '06 #24
Richard Heathfield wrote:

Kenneth Brody said:

[...]
I assume that it could also merge "Hello" and "Hello World", _if_ the
"Hello" were defined without the trailing nul?

Now, how does one define a pointer to 5 chars?

cdecl says:

cdecl> explain char (*foo)[5];
declare foo as pointer to array 5 of char


Yes.

but gcc complaint on this:

char (*foo)[5] = "Hello";


Yes, because char (*)[5] is not the same as char * (which is what you get
when you evaluate a char[6] such as "Hello").

This works, though:

char bar[5] = "Hello"; /* caveat - bar does not contain a string */
char (*foo)[5] = &bar;


True, but that won't allow the compiler to merge "Hello" with "Hello
World" (as it can with char *foo="World",*b ar="Hello World"), which
is what I was wondering about.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Jun 22 '06 #25

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9023
by: Gunnar | last post by:
Hello, I've just written a CPP program that reads integers from a binary file, and used this code while (my_ifstram.read( (char* ) &number, sizeof(int)) { // do something with number } My question is now, where can I find a manual that describes what the read method does with the ifstream object? I'm sitting here with my Linux/Debian machine, but I have not found any
6
3486
by: Steve | last post by:
Hi, I'm trying to convert a file reading loop into one using streams. The BSD OS read API returns the number of bytes read, but istream::read returns itself. How can I find out the number of bytes actually read? What the code fragment should do is read up to 1000 bytes into a buffer, or finish early if reading failed. Just your average read loop. I have: (this is a simplified version; I know there's no detailed error
12
11698
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I currently have as a test for using std::ifstream::read(). Is there anything wrong with the way I'm getting the file? #include <vector> #include <iomanip> #include <fstream> #include <iostream>
2
3106
by: Sandman | last post by:
Just looking for suggestion on how to do this in my Web application. The goal is to keep track of what a user has and hasn't read and present him or her with new material I am currently doing this by aggregating new content from all databases into a single indexed database and then saving a timestamp in the account database (for the current user) that tells me when the user last read items in the aggregated database.
2
2516
by: Andrea Bauer | last post by:
Hallo, wie kann ich so eine Datei unter .Net schreiben C++ oder C#. Bitte mit Funktionsaufrufen. Vielen Dank. Grüße Andrea <Product> <ProgramNumber>2</ProgramNumber>
4
3856
by: Ollie Cook | last post by:
Hi, I am having some difficulty with read(2) and interrupting signals. I expect I am misunderstanding how the two work together, so would appreciate some guidance. I am trying to 'time out' a socket read after a certain delay. The logic is (I will provide a test program below): - create and connect socket
1
4014
by: Jose Reckoner | last post by:
I'm running python 2.3 on Windows XP. Anyone have a quick small script to convert .DT1 and .DEM data to ASCII or some other format? I don't need a viewer. Thanks!
0
4780
by: phplasma | last post by:
Hey, I am currently attempting to implement a multi-threaded C# socket, using SSL (.pem file/certification/private key combo) server using Visual Studio C# Express. I have successfully made the client application establish a connection, and send data, which appears in plain, de-crypted text on the server - this works.
4
2830
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have following data.bin (3 Data appended to 2 Data): 2, data0, data1, 3, data0, data1, data2
5
12891
by: Thomas Christensen | last post by:
This issue has been raised a couple of times I am sure. But I have yet to find a satisfying answer. I am reading from a subprocess and this subprocess sometimes hang, in which case a call to read() call will block indefinite, keeping me from killing it. The folloing sample code illustrates the problem: proc = subprocess.Popen(,
0
10148
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9974
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11140
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10679
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9871
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8239
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7402
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3523
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.