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? 24 2713
v4vijayakumar wrote: 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?
Character pointer `str` is /not/ read-only. You are free to assign any
other character pointer value to it. What /is/ read-only is the string
literal somewhere in memory to which `str` points.
One reason for this is that it enables embedded applications to put the
string literal into (physically) read-only memory. This may or may not
gain performance, but certainly saves RAM which in these systems tends
to command a price premium.
v4vijayakumar wrote: why the following string, 'str' is read-only?
Because the C standard says so.
(well, it says it's undefined behavior to modify a string
literal) char *str = "test string";
anyhow 'str' needs to be in memory. do you think, making 'str' red-only
str is not the same thing as "test string", str can be modified :-)
would gain performance? or, it is right?
Le 21-06-2006, v4vijayakumar <v4***********@ yahoo.com> a écrit*: why the following string, 'str' is read-only?
char *str = "test string";
Because that is the way the langage is defined ?
You could wonder why the langage is designed this way.
I was not there, but my few knoledges in compilation let
me assume that, for some compiler, it was easier to do
so (it allow to store it in read-only memory).
Marc Boyer
Vladimir Oka wrote: v4vijayakumar wrote: 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?
Character pointer `str` is /not/ read-only. You are free to assign any other character pointer value to it. What /is/ read-only is the string literal somewhere in memory to which `str` points.
One reason for this is that it enables embedded applications to put the string literal into (physically) read-only memory. This may or may not gain performance, but certainly saves RAM which in these systems tends to command a price premium.
saving RAM?! anyhow still, you have to have these 12 bytes in RAM.
Marc Boyer wrote: Le 21-06-2006, v4vijayakumar <v4***********@ yahoo.com> a écrit : why the following string, 'str' is read-only?
char *str = "test string";
Because that is the way the langage is defined ? You could wonder why the langage is designed this way. I was not there, but my few knoledges in compilation let me assume that, for some compiler, it was easier to do so (it allow to store it in read-only memory).
Marc Boyer
shouldn't it be the other way? it should be easier to programmers.
Le 21-06-2006, v4vijayakumar <v4***********@ yahoo.com> a écrit*: Marc Boyer wrote: Le 21-06-2006, v4vijayakumar <v4***********@ yahoo.com> a écrit : > why the following string, 'str' is read-only? > > char *str = "test string";
Because that is the way the langage is defined ? You could wonder why the langage is designed this way. I was not there, but my few knoledges in compilation let me assume that, for some compiler, it was easier to do so (it allow to store it in read-only memory).
shouldn't it be the other way? it should be easier to programmers.
You could not expect C to be an 'universal' langage,
to find a C compiler for every platform *and* to require
too many things from the compiler.
So, yes, the C langage does not requires behaviors
that would be too hard/costly to implement (like a stop
of the program when dereferencing null pointer for
example, or out-of-bound array acces).
Marc Boyer
"v4vijayaku mar" <v4***********@ yahoo.com> wrote: Vladimir Oka wrote: v4vijayakumar wrote: 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? Character pointer `str` is /not/ read-only. You are free to assign any other character pointer value to it. What /is/ read-only is the string literal somewhere in memory to which `str` points.
One reason for this is that it enables embedded applications to put the string literal into (physically) read-only memory. This may or may not gain performance, but certainly saves RAM which in these systems tends to command a price premium.
saving RAM?!
In embedded devices, yes, why not? In large programs on a PC, yes, why
not, if the string isn't "test string" but a 2000-entry array of
200-character strings?
anyhow still, you have to have these 12 bytes in RAM.
Says who? You put them in ROM, of course. ROM is often cheaper than RAM.
In "normal" programs, you can put them in the executable image, save
some superfluous string copying, and eliminate duplicates.
Richard
v4vijayakumar schrieb: Vladimir Oka wrote: v4vijayakumar wrote: 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? Character pointer `str` is /not/ read-only. You are free to assign any other character pointer value to it. What /is/ read-only is the string literal somewhere in memory to which `str` points.
One reason for this is that it enables embedded applications to put the string literal into (physically) read-only memory. This may or may not gain performance, but certainly saves RAM which in these systems tends to command a price premium.
saving RAM?! anyhow still, you have to have these 12 bytes in RAM.
No, they can safely be placed into ROM, as the compiler can assume that
you will never modify the string literal. If you do so, you invoke
undefined behaviour, so the compiler is not responsible for this. If you
want a modifiable string, use the string literal as an initializer for
an array:
char str[] = "test string";
--
Marc Thrun http://www.tekwarrior.de/
"v4vijayaku mar" <v4***********@ yahoo.com> wrote: Marc Boyer wrote: Le 21-06-2006, v4vijayakumar <v4***********@ yahoo.com> a =E9crit : why the following string, 'str' is read-only?
char *str = "test string";
Because that is the way the langage is defined ? You could wonder why the langage is designed this way. I was not there, but my few knoledges in compilation let me assume that, for some compiler, it was easier to do so (it allow to store it in read-only memory).
shouldn't it be the other way? it should be easier to programmers.
Why would it be easier for programmers? You can get the behaviour you
want using
char str[] = "test string";
and the efficient behaviour using
char *str = "test string";
Choice is good. Catering to stupid hack programmers is not good.
Richard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
|
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>
|
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.
|
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>
| |
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
|
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!
|
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.
|
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
|
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(,
|
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...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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...
|
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();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |