0

I am trying to understand a portion of code. I am leaving out a lot of the code in order to make it simpler to explain, and to avoid unnecessary confusion.

typedef void *UP_T;

void FunctionC(void *pvD, int Offset) {
    unsigned long long int temp;
    void *pvFD = NULL;

    pvFD = pvD + Offset;
    temp = (unsigned long long int)*(int *)pvFD;
}

void FunctionB(UP_T s) {
    FunctionC(s, 8);
}

void FunctionA() {
    char *tempstorage=(char *)malloc(0);
    FunctionB(tempstorage);
}

int main () {
    FunctionA();
    return 0;
}

Like I said, I am leaving out a ton of code, hence the functions that appear useless because they only have two lines of code.

What is temp? That is what is confusing me. When I run something similar to this code, and use printf() statements along the way, I get a random number for pvD, and pvFD is that random number plus eight.

But, I could also be printing the values incorrectly (using %llu instead of %d, or something like that). I am pretty sure it's a pointer to the location in memory of tempstorage plus 8. Is this correct? I just want to be certain before I continue under that assumption.

unwind
  • 391,730
  • 64
  • 469
  • 606
halexh
  • 3,021
  • 3
  • 19
  • 19
  • `char *tempstorage=(char *)malloc(0);` just returns an address which cannot be dereferenced if at all it returns an address. It is implementation defined whether `malloc(0)` should return an address or `NULL`. – Alok Save Dec 31 '12 at 14:39
  • [If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free()](http://www.kernel.org/doc/man-pages/online/pages/man3/malloc.3.html) – Grijesh Chauhan Dec 31 '12 at 14:44
  • 2
    May be duplicate: http://stackoverflow.com/questions/2022335/whats-the-point-in-malloc0 And a useful link: http://techpreparation.com/c-interview/malloc-valid-memory-address.htm – Grijesh Chauhan Dec 31 '12 at 14:45
  • @GrijeshChauhan: This could be an answer. and is a good answer – MOHAMED Dec 31 '12 at 14:48

2 Answers2

3

The standard specifies that malloc(0) returns either NULL or a valid pointer, but that pointer is never to be dereferenced. There aren't any constraints regarding the actual implementation, so you can't rely on the returned pointer being another plus 8.

  • I was mostly curious about the `temp = ...` line though. If pvFD is the location in memory of tempstorage + 8, then its being typecasted to a int, then turned into a pointer, and then being typecasted again into a unsigned long long int? – halexh Dec 31 '12 at 14:52
  • 1
    @halexh `temp = (unsigned long long int)*(int *)pvFD;` means: take the `pvFD` address as a pointer to `int`. Dereference it as such, to obtain an `int`, then upcast that `int` to an `unsigned long long`. –  Dec 31 '12 at 14:55
1

It's random in the sense that malloc is typically non-deterministic (i.e. gives different results from run to run).

The result of malloc(0) is implementation-defined (but perfectly valid), you just shouldn't ever dereference it. Nor should you attempt to do arithmetic on it (but this is generally true; you shouldn't use arithmetic to create pointers beyond the bounds of the allocated memory). However, calling free on it is still fine.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680