473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Are constants and string literals considered "data objects" in c?

I was recently part of a little debate on the issue of whether
constants and string literals are considered "data objects" in C. I'm
more confused now than before.

I was always under the understanding that only "named" storage
areas(from the standard) were data objects. Since constants and string
literals don't have lvalues, they can't be data objects. Yet, I was
shown the first page of chapter 2 in K&R2, which states that variables
AND constants are data objects.

Does anyone have any opinions on this?

Nov 14 '05 #1
6 1988
ko********@gmail.com wrote:

I was recently part of a little debate on the issue of whether
constants and string literals are considered "data objects" in C. I'm
more confused now than before.

I was always under the understanding that only "named" storage
areas(from the standard) were data objects.
Since constants and string
literals don't have lvalues, they can't be data objects. Yet, I was
shown the first page of chapter 2 in K&R2, which states that variables
AND constants are data objects.


That's not a strict definition of "object".
Constants aren't objects.

N869
3.15
[#1] object
region of data storage in the execution environment, the
contents of which can represent values

String literals refer to arrays.
Arrays are objects.

--
pete
Nov 14 '05 #2
ko********@gmail.com wrote:

I was recently part of a little debate on the issue of whether
constants and string literals are considered "data objects" in C. I'm
more confused now than before.

I was always under the understanding that only "named" storage
areas(from the standard) were data objects.
Not so. The Standard says that an object is a "region of data
storage in the execution environment, the contents of which can
represent values". Nothing in there about "named" storage areas.
Since constants and string
literals don't have lvalues, they can't be data objects.


Yes, they can. Objects need not be writable at runtime. You'd
be hard-pressed to justify calling 6 an object, but string
literals are arrays of char which are guaranteed to stick around
for the life of the program, so it's hard to argue that they're
not objects.
Nov 14 '05 #3
In article <11**********************@f14g2000cwb.googlegroups .com>
<ko********@gmail.com> wrote:
I was recently part of a little debate on the issue of whether
constants and string literals are considered "data objects" in C. I'm
more confused now than before.

I was always under the understanding that only "named" storage
areas(from the standard) were data objects. ...

Does anyone have any opinions on this?


The C standard has an opinion, and as such, it is pretty much
final :-)

3.15 Object

[#1] A region of data storage in the execution environment,
the contents of which can represent values. Except for
bit-fields, objects are composed of contiguous sequences of
one or more bytes, the number, order, and encoding of which
are either explicitly specified or implementation-defined.
When referenced, an object may be interpreted as having a
particular type; see 6.2.2.1.

Of course, this C99 draft text says "objects", not "data objects",
but since the definition of "object" is "a region of data storage",
prefixing it with the word "data" appears to be benign, albeit
redundant.

Given this definition, a string literal that produces an array clearly
produces an object (or "data object", if you like the extra word):

const char *p = "hello";

even though the object itself has no name.

Ordinary constants, on the other hand, are not "region[s] of data
storage":

void f(void) {
int i = 3;
...

The constant 3 is not a "region of data storage", nor is it composed
of a "contiguous sequence of one or more bytes", especially if one
happens to be compiling on a SPARC:

mov #3, %l1 ! 3 does not appear as a separate value anywhere

(a "mov" instruction is a pseudo-instruction built out of a bitwise
"or" instruction, in this case, "or"ing %g0 with the signed immediate
13-bit constant that occupies the "simm13" field of the 32-bit
instruction word), or a 680x0:

moveq #3, d4 ! again, 3 does not appear as a separate value

(here the constant 3 is once again embedded within a bitfield in
the instruction word -- and in this case, because the bitfield is
only 3 bits wide, one cannot even isolate a byte with value 3).

(I add the last parenthetical remark because if you dump the
32-bit SPARC instruction as four separate bytes, one of them
*does* have the value 13. But if we just change the line to
read:

int i = 500;

-- which uses a single "mov #500, %l1" instruction -- we find there
is no 16-bit word containing 500, so this is something of a red
herring anyway.)

The named variable i, of course, *is* a "region of data storage"
and is therefore an object. If we take its address and decompose
it into a sequence of "unsigned char"s, we will find the machine's
representation of whatever "int" value i holds at the moment.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #4

ko********@gmail.com wrote:
I was recently part of a little debate on the issue of whether
constants and string literals are considered "data objects" in C. I'm more confused now than before.

I was always under the understanding that only "named" storage
areas(from the standard) were data objects. Since constants and string literals don't have lvalues, they can't be data objects. Yet, I was
shown the first page of chapter 2 in K&R2, which states that variables AND constants are data objects.

Does anyone have any opinions on this?

A data object in C is simply any region of memory that's obtained
during run-time execution. An object is synmonous to a data object. A
string constant(or string literal) does take a region of memory, but
the string itself is static.(strings are immutable in C).

Nov 14 '05 #5
On 24 Jan 2005 22:59:05 -0800, ko********@gmail.com wrote in
comp.lang.c:
I was recently part of a little debate on the issue of whether
constants and string literals are considered "data objects" in C. I'm
more confused now than before.

I was always under the understanding that only "named" storage
areas(from the standard) were data objects. Since constants and string
literals don't have lvalues, they can't be data objects. Yet, I was
shown the first page of chapter 2 in K&R2, which states that variables
AND constants are data objects.

Does anyone have any opinions on this?


Aside from several excellent and correct answers that you have already
received, consider this as well.

There are only two things in C that pointers can point to, namely
objects and functions.

The mere fact that you can send a string literal to printf(), for
example, means that you can have a pointer to the (first character of)
the string literal.

The mere fact that you can write code like this:

const int ci = 10;
const int *cip = &ci;

So string literals and constants must be objects, since you can take
their address and have pointers point to them, and they certainly are
not functions.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #6
Jack Klein wrote:
On 24 Jan 2005 22:59:05 -0800, ko********@gmail.com wrote in
comp.lang.c:

I was recently part of a little debate on the issue of whether
constants and string literals are considered "data objects" in C. I'm
more confused now than before.

I was always under the understanding that only "named" storage
areas(from the standard) were data objects. Since constants and string
literals don't have lvalues, they can't be data objects. Yet, I was
shown the first page of chapter 2 in K&R2, which states that variables
AND constants are data objects.

Does anyone have any opinions on this?

Aside from several excellent and correct answers that you have already
received, consider this as well.

There are only two things in C that pointers can point to, namely
objects and functions.

The mere fact that you can send a string literal to printf(), for
example, means that you can have a pointer to the (first character of)
the string literal.

The mere fact that you can write code like this:

const int ci = 10;
const int *cip = &ci;

So string literals and constants must be objects, since you can take
their address and have pointers point to them, and they certainly are
not functions.


Addendum (Jack knows this, just to clarify): Here, we are talking
not about "constant" but about "const qualified"; constants in the
C sense are something different and you cannot take the address
of, say, the constant value 4.2e1 or an enumeration constant.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #7

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

Similar topics

1
by: arvee | last post by:
Hi - I'm updating an Oracle table with an empty string and getting the error: An unhandled exception of type 'System.Exception' occurred in system.data.dll Additional information: Parameter...
2
by: hch | last post by:
dataAdapter.Update(data, "TableName") won’t work! I was about to deploy my first website on the Internet only to discover that the dataAdapter.Update() throws the Server Error in the third...
3
by: 21novembre | last post by:
Hi all, I made a question several days before to describe my strange trouble of mysqldump. But I still can't figour it out. Well, I just want to ask another question whether I could just backup...
6
by: Jon Davis | last post by:
I recently learned how to do an <OBJECT> alternative to <IFRAME> in current browsers using: <object id="extendedhtml" type="text/html" data="otherpage.html" width="250" height="400"></object> ...
4
by: Guadala Harry | last post by:
Is there any way for one Session to remove and update objects in another Session? I seriously doubt it, but thought I'd ask. Here's why: I have some data that is unique per user (or per session -...
11
by: sofeng | last post by:
I'm not sure if "data hiding" is the correct term, but I'm trying to emulate this object-oriented technique. I know C++ probably provides much more than my example, but I'd just like some feedback...
4
by: floppyzedolfin | last post by:
Hello! I'm actually encoding an encryption / decryption program. The encryption programes takes a file path in parameter, and encrypts the contents of the file and stores that into another file. ...
38
by: Sanders Kaufman | last post by:
I'm converting my table-based layouts to css-positioned divs - but the ONLY reason I'm doing it is because it's *considered* a best practice. I don't really see where anything goes hinky when...
3
by: BobRoyAce | last post by:
I am using Visual Studio 2008 w/ VB.NET. For the database, I am using SQL Server 2005, which is running on a dedicated server box. I am creating a WinForms application for a client. It is run...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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...
0
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...
0
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,...

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.