4

Wheneven I go through some tutorials/notes for C, I quite come across the term "objects". I was always wondering what does the object got to do with the procedural language. Going a bit deep I could understand that something occupying a piece of memory is termed as an "object" in c.

My question is whether my understanding is correct or is there something I am missing. Thanks!

Shash
  • 4,160
  • 8
  • 43
  • 67
  • 1
    You might find this question interesting: http://stackoverflow.com/questions/351733/can-you-write-object-oriented-code-in-c – MByD May 14 '12 at 08:30
  • Which tutorial or notes have you been reading? Please mention some reference. Such a terminology may be particular that tutorial. – Jay May 14 '12 at 08:31
  • @Jay There were a few tutorials which I had referred for learing pointers. Whenever going through some other notes or tutorials I sometimes come across these terminologies for which I get confused. – Shash May 14 '12 at 08:39
  • 1
    The word "object" is used in the C standard to indicate a "thing". It has nothing to do with OO. When you call malloc(), you get a pointer to an *object*. When you perform an assignment, the lvalue (which must be an object) is modified. In C, "object" is only a word for a thing, without any implied behaviour. It must always be a physical thing: you cannot modify an expression. – wildplasser May 14 '12 at 08:40
  • The rule of thumb is that objects are things you can take the address of. Except functions, which aren't objects but you can take their address. – Steve Jessop May 14 '12 at 08:46

3 Answers3

6

From the draft of the C99 Standard:

3.14
object
region of data storage in the execution environment, the contents of which can represent values

So, you're basically right.

Notes:

  • an object can have a name: int object = 42;
  • an object can be a part of a larger object: struct tm x; /* (x) and (x.tm_year) are objects */
  • an object can be allocated dinamycally: int *arr = malloc(42); if (arr) /* arr[4] is an object */;
pmg
  • 106,608
  • 13
  • 126
  • 198
2

In the C standard at least, an "object" is roughly a piece of data occupying contiguous memory. So int, long, float, pointer variables are all objects, as well as arrays or structs or arrays of structs, or data in malloc'd chunks of memory.

zvrba
  • 24,186
  • 3
  • 55
  • 65
  • 1
    `int`, `long`, `float` are all types. Objects of type `int`, `long`, `float` are all objects. Values of type `int`, `long`, `float` aren't objects, they're values, for example the literal `1` is not an object. – Steve Jessop May 14 '12 at 08:45
  • Well there, I was deliberately imprecise in order to avoid self-referential explanation like yours. – zvrba May 14 '12 at 16:10
2

There was this post a while back on comp.lang.c related to this by the famous Chris Torek which may help you.

dirkgently
  • 108,024
  • 16
  • 131
  • 187