4

In the book, Modern C by Jens Gustedt, following statements have been made by the author:

  1. 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).

  2. 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:

    enter image description here

    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.

  3. 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:

  1. 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?

  2. 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'?

halfer
  • 19,824
  • 17
  • 99
  • 186
dexter
  • 185
  • 8
  • 1
    'object' here is a simpler form of the C++ object concept. In C, an object is data (a number of consecutive bytes) recognized by a name (simple type like int, double, ... or a struct) which has an address in memory. (not the C++ concept of inheritance, polymorphism, ... methods and properties, while they can be, of course, simulated in C). – Déjà vu May 12 '22 at 06:15
  • 1
    @Déjàvu: The name of an object is the identifier that refers to it. It is not the type. Objects do not have to have names. – Eric Postpischil May 12 '22 at 07:34
  • 1
    @Déjàvu - Neither of those concepts in C++ are used to define objects. The C++ definition of objects is the C definition, almost verbatim. Everything you mentioned is stuff C++ **can do** with objects. – StoryTeller - Unslander Monica May 12 '22 at 07:51
  • @EricPostpischil Of course it's not the type :) Maybe the phrasing confused you. – Déjà vu May 12 '22 at 08:45

4 Answers4

3

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.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • beware though, "boxed" can have a specific meaning when talking about memory representation (kind of the equivalent of storing a pointer compared to storing a variable directly) https://stackoverflow.com/questions/1418296/what-does-it-mean-to-say-a-type-is-boxed – tbrugere May 12 '22 at 06:14
1

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.

enter image description here

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.

Mysterious Jack
  • 621
  • 6
  • 18
1

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".

neslrac
  • 121
  • 1
  • 1
  • 3
  • Note: 'register' keyword can also determine the region of data storage which is written left side of the equals sign, which can be part of the declaration(without definition) but I feel your point is correct with what is in authors mind – Mysterious Jack May 12 '22 at 10:00
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 not p 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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Is function also an object in C? 3rd statement implies, they are. 'declarations only specify the kind of object an identifier refers to' Here by the kind of object, i reckon it means if its an array or function etcetera and not the type? – dexter May 12 '22 at 10:27
  • 1
    @dexter No, functions are not objects. C also has distinctive rules for pointers to objects and pointers to functions separately. – Lundin May 12 '22 at 10:56
  • What would 'sizeof *p' return considering p was never declared or defined? Would p just be some garbage? – Happy John Mar 15 '23 at 00:03
  • @HappyJohn It would be the size of an `int` and the operand of sizeof isn't evaluated beyond that, so it doesn't matter that `p` isn't pointing anywhere at that point. – Lundin Mar 15 '23 at 07:21
  • @Lundin How would p to be known to be an int? It can be referenced in its own declaration? – Happy John Mar 15 '23 at 07:59
  • 1
    @HappyJohn Since you wrote `int* p` then de-referencing `p` gives an `int`. Yes it can be de-referenced in the line where it was declared but only because sizeof doesn't evaluate the operand. It's trick related to the sizeof operator specifically. – Lundin Mar 15 '23 at 09:11