473,698 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read-only functionality without 'const'

Hi,

While trying to understand the difference between the following 2
methods, i have some interesting queries.
Method 1) char *s = "Hello";
and
Method 2) char s[] = "Hello";

How does the string 'hello' in first method lie in read-only memory
and the string 'hello' in second method lie in a modifiable memory ?
Only 'const' provides the 'Read-only' functionality in C . How come
this "char *s" provides that functionality ? What is the internal of
this functionality actually ?

The following is the snapshot of the info that has prompted me to
raise this query :-
In any context, char *s = "Hello"; just means that the pointer s is
assigned the address of the string literal "Hello". Normally, that
string literal will reside in read-only memory which means that it's
not legal to do:
char *s = "Hello"; s[1] = 'a';

while it's perfectly legal to do
char s[] = "Hello"; s[1] = 'a';

Thx in advans,
Karthik Balaguru

Aug 16 '07
26 2113
In article <11************ *********@d55g2 000hsg.googlegr oups.com>,
karthikbalaguru <ka************ ***@gmail.comwr ote:
>put the value in read-only memory, because there is no defined way to
modify it.
>Around this point revolves my query.
What does this read-only memory mean here ?
What does it refer to ? Is it a portion of stack or heap or something
else ?
Where is it ?
Typically it would be a segment of memory marked as unwritable by the
operating system (as, for example, the program code usually is). The
operating system does this by setting some bit in thte page
descriptor, which is interpreted by the processor as meaning "don't
allow writes to this page".

In principle it could be some kind of ROM on an embedded system.

Both of these are only likely for const variables initialised to
compile-time constants, because otherwise it would have to make
the memory writable to store the constant value.

-- Richard

--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Aug 17 '07 #21
Remember that "const" doesn't really mean "constant"; it merely means
"read-only". For example, 42 is a constant, but:
const int x = 42;
x is not a constant; it's merely read-only. (But the compiler can
choose to store x in read-only memory, or not store it at all if its
address is never used; nevertheless, x can't be used where a constant
expression is required.)
I came across 'restrict' keyword that is very interesting to share
with you here.
I find that the 'restrict' keyword overcomes the drawbacks of 'const'.
Using 'restrict' keyword, all data accessed through it will be
accessed only through that but not through any other methods.
Any comments/fireballs w.r.t this are welcome.

Thx,
Karthik Balaguru.
Aug 21 '07 #22
karthikbalaguru wrote:
>Remember that "const" doesn't really mean "constant"; it merely means
"read-only". For example, 42 is a constant, but:
const int x = 42;
x is not a constant; it's merely read-only. (But the compiler can
choose to store x in read-only memory, or not store it at all if its
address is never used; nevertheless, x can't be used where a constant
expression is required.)

I came across 'restrict' keyword that is very interesting to share
with you here.
I find that the 'restrict' keyword overcomes the drawbacks of 'const'.
restrict has no connection with const. They are both for different purposes.
restrict is used to permit the compiler to optimise certain operations which
it would otherwise not be able to do, due to the possibility of aliasing.

It's extensively used by standard library functions.

Aug 21 '07 #23
karthikbalaguru <ka************ ***@gmail.comwr ites:
>Remember that "const" doesn't really mean "constant"; it merely means
"read-only". For example, 42 is a constant, but:
const int x = 42;
x is not a constant; it's merely read-only. (But the compiler can
choose to store x in read-only memory, or not store it at all if its
address is never used; nevertheless, x can't be used where a constant
expression is required.)
I wrote the above. Please leave attribution lines in place for any
quoted text (such as the line above that says "karthikbalagur u
<ka************ ***@gmail.comwr ites:"). They make it substantially
easier to follow the discussion.
I came across 'restrict' keyword that is very interesting to share
with you here.
I find that the 'restrict' keyword overcomes the drawbacks of 'const'.
Using 'restrict' keyword, all data accessed through it will be
accessed only through that but not through any other methods.
Any comments/fireballs w.r.t this are welcome.
The 'restrict' keyword is new in C99, so many compilers may not (yet?)
support it.

Unlike 'const', it's really just an optimization hint. By using it,
your not asking the compiler to enforce any restriction; instead,
you're promising the compiler that *you* won't violate the
restriction. The compiler is then free to assume that you haven't
violated your promise, which allows it to perform certain
optimizations; if you break your promise, either deliberately or
accidentally these optimizations can break your code.

C99 6.7.3p7:

An object that is accessed through a restrict-qualified pointer
has a special association with that pointer. This association,
defined in 6.7.3.1 below, requires that all accesses to that
object use, directly or indirectly, the value of that particular
pointer. The intended use of the restrict qualifier (like the
register storage class) is to promote optimization, and deleting
all instances of the qualifier from all preprocessing translation
units composing a conforming program does not change its meaning
(i.e., observable behavior).

See also the formal description in C99 6.7.3.1. (The latest C99 draft
is available at
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf>.)

--
Keith Thompson (The_Other_Keit h) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 21 '07 #24
karthikbalaguru wrote:
>
>Remember that "const" doesn't really mean "constant"; it merely
means "read-only". For example, 42 is a constant, but:
const int x = 42;
x is not a constant; it's merely read-only. (But the compiler
can choose to store x in read-only memory, or not store it at
all if its address is never used; nevertheless, x can't be used
where a constant expression is required.)

I came across 'restrict' keyword that is very interesting to
share with you here. I find that the 'restrict' keyword
overcomes the drawbacks of 'const'. Using 'restrict' keyword,
all data accessed through it will be accessed only through that
but not through any other methods.
I think you have things mixed up. 'restrict' signals the called
routine that the parameters are totally independent, i.e. do not
overlap. It does not enforce that condition. Its primary purpose
is to enable more aggressive optimization.

Check the C standard.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 22 '07 #25
On Aug 22, 3:23 am, Keith Thompson <ks...@mib.orgw rote:
karthikbalaguru <karthikbalagur ...@gmail.comwr ites:
Remember that "const" doesn't really mean "constant"; it merely means
"read-only". For example, 42 is a constant, but:
const int x = 42;
x is not a constant; it's merely read-only. (But the compiler can
choose to store x in read-only memory, or not store it at all if its
address is never used; nevertheless, x can't be used where a constant
expression is required.)

I wrote the above. Please leave attribution lines in place for any
quoted text (such as the line above that says "karthikbalagur u<karthikbalagu r...@gmail.comw rites:"). They make it substantially

easier to follow the discussion.
I came across 'restrict' keyword that is very interesting to share
with you here.
I find that the 'restrict' keyword overcomes the drawbacks of 'const'.
Using 'restrict' keyword, all data accessed through it will be
accessed only through that but not through any other methods.
Any comments/fireballs w.r.t this are welcome.

The 'restrict' keyword is new in C99, so many compilers may not (yet?)
support it.

Unlike 'const', it's really just an optimization hint. By using it,
your not asking the compiler to enforce any restriction; instead,
you're promising the compiler that *you* won't violate the
restriction. The compiler is then free to assume that you haven't
violated your promise, which allows it to perform certain
optimizations; if you break your promise, either deliberately or
accidentally these optimizations can break your code.

C99 6.7.3p7:

An object that is accessed through a restrict-qualified pointer
has a special association with that pointer. This association,
defined in 6.7.3.1 below, requires that all accesses to that
object use, directly or indirectly, the value of that particular
pointer. The intended use of the restrict qualifier (like the
register storage class) is to promote optimization, and deleting
all instances of the qualifier from all preprocessing translation
units composing a conforming program does not change its meaning
(i.e., observable behavior).

See also the formal description in C99 6.7.3.1. (The latest C99 draft
is available at
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf>.)

--
Keith Thompson (The_Other_Keit h) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
The link of the latest c99 draft clarified many of my doubts.
Thx for the info .

Karthik Balaguru

Aug 22 '07 #26
karthikbalaguru <ka************ ***@gmail.comwr ites:
On Aug 22, 3:23 am, Keith Thompson <ks...@mib.orgw rote:
[53 lines deleted]
>
The link of the latest c99 draft clarified many of my doubts.
Thx for the info .
You're welcome.

When you post a followup, *please* take the time to delete parts of
the previous article that aren't directly relevant to your response.
In particular, don't quote the signature (the stuff following the
"-- " line) unless you're actually commenting on it. See most
of the followups posted here for examples.

--
Keith Thompson (The_Other_Keit h) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 22 '07 #27

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

Similar topics

2
8997
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
3471
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
0
1873
by: munif | last post by:
i wnat write a C++ program that would read from a CD and display information about files and folders in the given CD In simple words you would be performing a simple (ls -l) or (DIR /S) on the CD. LOOk at this code /*
12
11656
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
3087
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
2504
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
3844
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
5777
by: Arpan | last post by:
The contents of a text file are as follows: The Quick Brown Fox Jumped Over The Lazy Dog. Note that there isn't any space at the end of each of the 3 lines. Now when I do this:
7
2143
by: Tracks | last post by:
I have old legacy code from vb5 where data was written to a file with a variant declaration (this was actually a coding error?)... in vb5 the code was: Dim thisdata as integer Dim thatdata Dim someother as integer thatdata = ubound( Array1 )
4
2799
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
0
8674
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
9157
Oralloy
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...
1
8895
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,...
1
6518
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
5860
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
4369
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
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
3
2001
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.