2

I wrote the following function in C:

   void dummy(int* my_array, int size)
   {
     //my implementation
   }

Is there any way to check whether size is REALLY the size of my_array? For example, if I call the function and use for my_array an array with 4 elements but pass 5 for size, is there any way to know that 5 is not really the size of the array?

Thank you in evidence.

今天春天
  • 941
  • 1
  • 13
  • 27
  • 1
    I don't think it is possible. – Spikatrix Oct 09 '15 at 14:51
  • So there is no way for me to prevent that the person who uses dummy puts any value for size? – 今天春天 Oct 09 '15 at 14:52
  • There is no such thing as the "real" size of the array. If you try to define it, you'll find that you cannot do so. An array is just a collection of things in memory with a particular organization, and any contiguous, aligned portion of the array is also an array. "If someone gives me three cars, how can I tell if that's all of the cars?" Well, all of *what* cars. It's all the cars they gave you. – David Schwartz Oct 09 '15 at 14:56
  • Thank for that explanation. This is a very good way to understand this. – 今天春天 Oct 09 '15 at 14:57
  • 1
    @DavidSchwartz, not so. You can determine the size of an array just fine via the `sizeof` operator. What you cannot do is determine the size of an array from a pointer to its first element (or to any other element), which is what the OP wants to do. – John Bollinger Oct 09 '15 at 15:01
  • @JohnBollinger That's not what the OP wants to do. He wants to tell if the size reported is really the size of the array. If I tell you the size is 3 and `sizeof` says 4, which is "really the size of the array" that the function should operate on? – David Schwartz Oct 09 '15 at 16:33
  • @DavidSchwartz, the question is predicated on some measure of the size of an array that the OP considers the "real" one, and which can differ from the value of `size`. I could have inferred incorrectly which measure is meant, but *whatever* measure is meant, answering "how do I determine X" with (only) "X" is not a good name" is not helpful. – John Bollinger Oct 09 '15 at 18:33

4 Answers4

4

You're looking at it the wrong way. An array is contiguous piece of memory. In C, you can represent this concept with a pointer to its start and its size. Since your array is represented by a <my_array, size> tuple, it doesn't make sense to talk about my_array's size, since it's only the start-pointer of the array.

Tamás Zahola
  • 9,271
  • 4
  • 34
  • 46
  • 3
    "An array is contiguous piece of memory" -- much better than "an array is a pointer", but still a little weak. C itself describes an array as "a contiguously allocated nonempty set of objects with a particular member object type" (C2011 6.2.5/20). – John Bollinger Oct 09 '15 at 15:07
  • @JohnBollinger More accurately, "a contiguously allocated nonempty set of objects with a particular object type" and a specific boundary that you cannot exceed. I think leaving out that detail further complicates the issue. – Joel Trauger Oct 09 '15 at 15:25
  • @JoelTrauger, I quoted the standard's full characterization, albeit by drawing it from the definition of an array *type*. That there must be a boundary follows from "contiguously allocated", but in fact C doesn't talk about it. In particular, as in most things, C does not say what you cannot do in this area; it simply declines to define the behavior resulting from trying to exceed the array bounds. – John Bollinger Oct 09 '15 at 15:40
  • @JohnBollinger Exactly. That is not clearly evident to everyone, sadly, as evidenced by the number of people I've helped in person with array out of bounds errors. – Joel Trauger Oct 10 '15 at 00:26
0

unfortunately, there is no such way in C. As an array is always a pointer to an arbitrary address in memory, there is no such thing as "array bounds" that can be checked.

This is why many of the C functions have that special size parameter: because there's no other way of determining an array's size.

ANGeL-Devon
  • 109
  • 8
  • Okay thank you, this helped. I just thought - oh, there must be a way to do this! – 今天春天 Oct 09 '15 at 14:55
  • 2
    A bit misleading. An array isn't a pointer (check `sizeof` an array and see). But the function parameter *is* a pointer. The function doesn't know anything about arrays at the caller side. – juanchopanza Oct 09 '15 at 14:58
  • 4
    An array is *not* a pointer! That idea will cause you trouble (and *does* cause people trouble). The issue here is that in most contexts, including function arguments, an array name *evaluates to* a pointer to the first array element. You cannot determine the size of the array from that pointer. – John Bollinger Oct 09 '15 at 14:58
0

The size of the array is however big you made it when you declared and initialized it.

int my_array[100] = {0}; has a size of 100.

int my_other_array[50] = {0}; has a size of 50.

If you want the length of the data, then you're thinking in more abstract terms than the language can handle. The length of your data is a non-measurable parameter when the language does not support it.

Joel Trauger
  • 720
  • 1
  • 4
  • 24
-1

If my_array is dynamically allocated (with malloc) you can get the size of memory block but it depends on specific compiler. Unfortunately in most cases there is address alignments and you will not have the exact size to 1 byte but aligned to 32 bits or other.

i486
  • 6,491
  • 4
  • 24
  • 41