0

Possible Duplicate:
c dynamic memory allocation and sizeof()

I am trying to find the cache block by keeping a huge block of 16 MiB and by trying to access different elements each time to find the time. I just can't write the length of the array. How can I write a for loop to iterate over the array. I need the length of the array; how can I find that? I have tried sizeof(a)/sizeof(a[0]) but this doesn't work or I am doing something wrong because my assignment sheet tells me it can hold 4 million int's..

register *a;
a = malloc(16777216);
int i;
for (i = 0; i < sizeof(a)/sizeof(a[0]); i = i + 1) {

    printf("\ni = %d", i);
}

This only prints i = 0 i = 1.

Community
  • 1
  • 1
  • You need to use a more modern compiler or turn on some warnings. The notation `register *a` is archaic (for over 20 years it has been discouraged) way of writing `register int *a`. 'Tis funny; for the `int i;` to compile, you must be using either C++ or a C99 compiler because a declaration was not allowed after a statement in C89. Pay attention to what your compiler is warning you (and if it isn't warning you, turn on the warnings or get a better compiler). You have to be using a 64-bit machine to get the `0, 1` answer. – Jonathan Leffler Dec 11 '12 at 03:42
  • Read through these 1) http://stackoverflow.com/questions/12131925/getting-the-size-of-a-malloc-only-with-the-returned-pointer 2) http://c-faq.com/aryptr/aryparmsize.html 3) http://c-faq.com/aryptr/index.html – user93353 Dec 11 '12 at 03:44

2 Answers2

1

The code sizeof(a) simply returns the size of the pointer register *a, and is completely unrelated to the size of the array that a points to.

C arrays do not track how many items they contain. You can use this syntax only if the size of the array is known at compile time. But you can't do it with an array allocated this way.

For this task, you'll need to track this information yourself. You can store that value in a variable, or you could append an array element with a special value that indicates it's the end of the array (much like we do with C strings).

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • 1
    "C arrays do not track how many items they contain" ... that's not strictly true. If you use `sizeof` on an actual array type it will give you the size in bytes of the array. Here, the OP is using `sizeof` with a pointer type, not an array type, so it is only giving him the size of the pointer. `sizeof("hello")` for example returns 6, because the string literal `"hello"` is of type `char[6]`. – dreamlax Dec 11 '12 at 03:54
  • `sizeof` also works for variable-length arrays as well, meaning that the size of the array must be kept somewhere for `sizeof` to return the correct size of the variable-length array. – dreamlax Dec 11 '12 at 03:55
  • I did specifically state that it works if the array is declared statically. He may be using a pointer but, conceptually, he is allocating an array. And that dynamically allocated array does not track the number of elements. – Jonathan Wood Dec 11 '12 at 03:55
  • @dreamlax: I'd love to see an example where `sizeof()` works with a variable-length array. – Jonathan Wood Dec 11 '12 at 03:56
  • Consider a struct with a member that is an array type (`struct X { int a[4]; }`. The struct can be allocated dynamically and therefore the array-type member must be allocated dynamically also; `sizeof` will still return the correct size of the array type member `a`. It isn't how the object is "allocated", it is how the object is **typed**. – dreamlax Dec 11 '12 at 03:59
  • It's part of the C99 standard that `sizeof` works with variable-length arrays. – dreamlax Dec 11 '12 at 03:59
  • Well, that's right. But how is `int a[4]` a variable-sized array? The compiler must have a way to know the size. The array itself does not include size information. – Jonathan Wood Dec 11 '12 at 04:01
  • You mentioned that `sizeof` only works for statically allocated arrays, but I was explaining that arrays can be allocated dynamically. See footnote 7 of section 6.5.3.4 of the C99 standard where it exhibits use of `sizeof` on a variable length array. – dreamlax Dec 11 '12 at 04:06
  • Yes, I stand corrected regarding dynamically allocated arrays. It is possible to dynamically allocate an array where the compiler knows the size, as in your example. – Jonathan Wood Dec 11 '12 at 04:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20905/discussion-between-dreamlax-and-jonathan-wood) – dreamlax Dec 11 '12 at 04:10
0

a is a register pointer an not an array. so sizeof(a) will not return 16MB as you are expecting. please use 16777216 directly instead of sizeof()

aakash
  • 751
  • 5
  • 18