0

I'm trying to understand object oriented programming in C. During my research on the internet I came across this code:

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int x, y;
    int width;
    int height;
}   Rectangle;

Rectangle *Rectangle_init(int x, int y, int width, int heihgt) {
    Rectangle *obj = malloc(sizeof *obj);
    obj->x = x;
    obj->y = y;
    obj->width = width;
    obj->height = height;
    return obj;
}

void Rectangle_draw(Rectangle *obj) {
    printf("I just drew a nice %d by %d rectangle at position (%d, %d)!\n",
           obj->width,
           obj->height,
           obj->x,
           obj->y);
}

int main(void) {
    Rectangle *r = Rectangle_init(1, 1, 3, 5);
    Rectangle_draw(r);
    return 0;
}

The code compiles and runs and I do understand pretty much all of it. However what I don't get is why `obj' can be referenced in the malloc-call although it does not exist yet?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
f1nan
  • 156
  • 10
  • Where is an undefined object? – too honest for this site Sep 16 '17 at 16:55
  • 2
    `sizeof` is a compile-time operator which can accept an expression as argument and returns the size in bytes of the type of the expression. There's no need for evaluation as we're dealing with types only. – Jack Sep 16 '17 at 16:55
  • @Jack: It still requires the object to be defined, so that's not the point. – too honest for this site Sep 16 '17 at 16:58
  • But the pointer obj is the return value of malloc(sizeof *obj). I dont get why I can use obj to get the sizeof obj. Its like recursion but not really. I does not make any sense to me. – f1nan Sep 16 '17 at 17:01
  • 1
    Because sizeof is guaranteed not to actually evaluate its operand under most conditions. Since (*obj) is not evaluated, obj is never dereferenced. See paragraph 6.5.3.4 of the C standard. "The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant." – torstenvl Sep 16 '17 at 17:38
  • How does sizeof know that obj is a pointer to Rectangle if this is declared in the exact same line? – f1nan Sep 16 '17 at 17:49

1 Answers1

0

But the pointer obj is the return value of malloc(sizeof *obj). I dont get why I can use obj to get the sizeof obj. Its like recursion but not really. I does not make any sense to me

sizeof is an operator, much like the arithmetic operators you're used to and others. This means it is a symbol that tells the compiler to perform specific mathematical or logical functions. In this specific case, it tells the compiler to query the size of the object or type. When doing that, the compiler can tell that obj is a Rectangle on its code analysis.

savram
  • 500
  • 4
  • 18