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?

5
  • 1
    ary is 8 because you printing the size of a pointer not the actual memory size allocated. Commented Nov 18, 2014 at 7:28
  • 2
    The array starts at zero, meaning randPages[0] == 7, randPages[1] == 9, randPages[2] == 4, randPages[3] == 6,... Commented Nov 18, 2014 at 7:31
  • possible duplicate of How to find the 'sizeof'(a pointer pointing to an array)? Commented Nov 18, 2014 at 7: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. Commented Nov 18, 2014 at 8: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. Commented Nov 18, 2014 at 14:03

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
No problem. Check my edited answer for some more information.
0

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

1 Comment

makes sense to me (now)!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.