'What are 'objects' in C language?
In the book, Modern C by Jens Gustedt, following statements have been made by the author:
In the following subsections, we will introduce the syntactical aspects (grammar) and three different semantic aspects: declarative parts (what things are), definitions of objects (where things are), and statements (what things are supposed to do).
The declarations of i and A declare variables , which are named items that allow us to store values. They are best visualized as a kind of box that may contain a “something” of a particular type:
Conceptually, it is important to distinguish the box itself (the object), the specification (its type), the box contents (its value), and the name or label that is written on the box (the identifier). In such diagrams, we put ?? if we don’t know the actual value of an item.
Generally, declarations only specify the kind of object an identifier refers to, not what the concrete value of an identifier is, nor where the object it refers to can be found. This important role is filled by a definition.
Questions regarding the aforementioned statements:
The first and third statements from the author imply that Definitions specify where the object referred to by the identifier can be found. What does 'where' mean here, in particular how is specifying a concrete value of an identifier in definition same/different from where the object the identifier refers to is?
From statement 2, what does 'Conceptually, it is important to distinguish the box itself (the object), the specification (its type), the box contents (its value), and the name or label that is written on the box (the identifier).' mean? Why's the 'box' an object here and not the value in the 'box'?
Solution 1:[1]
First of all, if you are a complete beginner, you don't need to worry about all these dirty details - simplified, think of an object as the same thing as a variable. Beginners can stop reading here, as the rest of the answer is an advanced topic.
The formal definition of object in C (C17 3.15):
object
region of data storage in the execution environment, the contents of which can represent values
What does 'where' mean here
It means that upon definition, an object is assigned a memory location - at least a relative one, later turned into an absolute address by the linker.
in particular how is specifying a concrete value of an identifier in definition same/different from where the object the identifier refers to is?
From statement 2, what does 'Conceptually, it is important to distinguish the box itself (the object), the specification (its type), the box contents (its value), and the name or label that is written on the box (the identifier).' mean? Why's the 'box' an object here and not the value in the 'box'?
To answer all of this at once, there are several ways to create objects in C. The most obvious would be something like int i=0;
where the object gets a type, a value and an identifier all at once. But in case of dynamic allocation, it happens differently.
Take int* p = malloc(sizeof *p)
.
malloc
returns a pointer to an unnamed object. The name of the object is notp
because that's the name of a pointer (which in itself is also an object) - the object itself has no corresponding identifier.- As the type rules of C go, the returned address has no declared type, so the object does not yet have a type.
- The value of the object is also indeterminate at this point.
In this case the object has neither a type nor a value until we do *p = 1;
. After that, the object got type int
and value 1
, because we used int
when we did the write access. This example of dynamic allocation is part of a somewhat advanced topic known as effective type, related to pointer aliasing. Gustedt most likely had that particular rule in mind when he wrote this chapter.
Solution 2:[2]
An object is a place where values can be stored. That's the terminology used by the language.
Formally, an object is a
region of data storage in the execution environment, the contents of which can represent values
https://port70.net/~nsz/c/c11/n1570.html#3.15p1
The book author uses the word "box", which is entirely appropriate in an informal discussion.
Solution 3:[3]
Q.1: The author in the first point refers to the definition & declaration as being similar things because both concepts are closely tied together(but there is a difference I think he will explain it clearly in further chapters, he just made sure you get started without going into more details for now).
To explain in simple terms 'where' refers to memory.
If you continue learning C, you will find the concept of 'Storage classes' where the declaration of a variable will tell the compiler where to store the value in the memory location(physically). It can be either RAM(Random Access Memory) or Register(hardware storage units of a processor).
int a; // declaration(only specify what type of value it can store but does not specifies actual value)
int a = 123; //both declaration & defination (specifies the type & actual value i,e 123)
when int a = 123
is executed compilers will allocate 4 bytes of memory (assuming your OS/compiler is 64 bit) in RAM. As your house has an address, every memory block also has an address, lets's assume starting byte address of the allocated memory is 101, thus it takes 101...104.
Q.2: The four bytes of allocated memory with value 123 inside it is called an object in a broader sense. You have a house with 4 rooms & an address. But not to forget you can also name your house like 'dexter's home'. So here 'a'
is the name/label you can use to refer to a particular location of memory when writing C code for your ease.
Note all these four boxes of bytes are considered a single box by the author, double
actually takes eight bytes(on 64 bit) but the author has drawn a single box for simplicity.
Solution 4:[4]
it is important to distinguish
- the box itself (the object)
- the specification (its type)
- the box contents (its value)
- and the name or label that is written on the box (the identifier)
And what about the address? Isn't there a line missing like
- the location of the box (its address)
I don't see much advantage in calling variables objects, and less in calling objects boxes.
Gustedt gives some good explanations and examples, but he is not very systematic.
Definitions and address:
I guess he has this in mind:
int *p = malloc(...);
Here the definition determines the "region of data storage".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Lundin |
Solution 2 | n. 1.8e9-where's-my-share m. |
Solution 3 | Mudassir Hussain |
Solution 4 |