1

This may be due to my lack of understanding of C, but I'm getting some (what i would call) obscure errors when dealing with arrays:

Here is the code:

int * generateRandomArray(int numPageReplacements) {
    // Seed random number generator
    srand(time(NULL));

    int * randPages = (int *)malloc(sizeof(int)*numPageReplacements);

    for (int i = 0; i < numPageReplacements; i++) {
        randPages[i] = rand() % 10;     // generate (with uniform distribution) number 0 - 9
        printf("%d ", randPages[i]);    // for testing purposes
    }
    printf("\nRandomPages[3] = %d \n" ,randPages[3]);
    return randPages;   // return array pointer
}

Output:

7 9 4 6 4 6 5 7 6 3 
RandomPages[3] = 6 
Program ended with exit code: 0

If I run this back to back (take random numbers generated above) it will give 4 sometimes (what one would expect) and 6 at others (it's like it doesn't know the bounds of each array cell).

When I attempt to obtain the array from this function from the call:

int * ary = generateRandomArray(int numPageReplacements);
printf("Size of ary = %lu",sizeof(ary));

Output:

Size of ary = 8

It's equal to 8 no matter WHAT numPageReplacements is.

Compiler:

Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn) Target: x86_64-apple-darwin14.0.0 Thread model: posix

Am I missing something here?

HelloWorld
  • 605
  • 1
  • 7
  • 22
  • 1
    `ary` is 8 because you printing the size of a pointer not the actual memory size allocated. – SSC Nov 18 '14 at 07:28
  • 2
    The array starts at zero, meaning randPages[0] == 7, randPages[1] == 9, randPages[2] == 4, randPages[3] == 6,... – Casperah Nov 18 '14 at 07:31
  • possible duplicate of [How to find the 'sizeof'(a pointer pointing to an array)?](http://stackoverflow.com/questions/492384/how-to-find-the-sizeofa-pointer-pointing-to-an-array) – dandan78 Nov 18 '14 at 07:35
  • C doesn't have any runtime environment to check array bounds etc., like some other languages may have. Therefore the compiler only knows what can be inferred at compile time. Statically allocated arrays have a known size at compile-time, whereas the size of dynamically allocated arrays are (often) not known until runtime. – Morten Jensen Nov 18 '14 at 08:54
  • not sure how i missed that i was actually getting the 4th element with [3] (was a long day i suppose) thanks for that point, casperah. – HelloWorld Nov 18 '14 at 14:03

2 Answers2

5

Calling sizeof on an pointer give you only the size of that pointer, which is what ary is. An array would be int ary[10];

You are working with dynamically allocated memory, not arrays. Sure, they work like arrays, kind of, because you can use [] to access its elements, but under the hood, they are very different beasts.

Just so you know, there is no way to find out how big an an array, or even if it is an array at all, as opposed to just the address of a single int, that's being passed to a function. A pointer merely stores a memory address. Interpreting what's at that address is up to the programmer. That's why C functions that accept arrays/buffers always take the size of that array/buffer as a separate parameter.

dandan78
  • 13,328
  • 13
  • 64
  • 78
  • thank you Dan. didn't really think about it like that. the link to how to find the size of a pointer to an array (or lack thereof) is what i'm looking for – HelloWorld Nov 18 '14 at 14:11
  • No problem. Check my edited answer for some more information. – dandan78 Nov 18 '14 at 15:12
0

sizeof(ary), ary is an integer pointer and its size is 8 bytes(64bit) in a 64 bit machine that you are running.