Any way to find the length of an array without knowing it's bounds?

SunnyD

Belgian Waffler
Jan 2, 2001
32,675
146
106
www.neftastic.com
Given pseudocode:

Pointer *p where *p = arbitrary length array of items A

NOT knowing what the length of A is before hand, but you know the type of A, is there a way to determine the length of the array?

Ahh, but there's a catch! You don't know what, if any termination unit is the array.

For example, given a string:

char p[] = "Some string"

The common delimiter is null, which is what strlen uses to find the end of the string. However what about the case that you may unknowningly be parsing, say, a unicode string in which case it looks like every other character is a null (in ANSI).

Basically, without explicitly knowing the length of the array, is there any way to find it's length pro grammatically?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
C/C++ pointers just hold memory addresses, and arrays are just heap or stack buffer allocations.

So no, not unless it's stored in every element of A, as a length or pointer to the end of the active array or some other equivalent.

Or you get access to the heap manager's internal allocation list and find the buffer that contains the pointer's address. Or you have your own tracking system to record all buffer allocations.

I don't do .NET coding, there may be something in it to do the managed heap lookup for you.
 

SunnyD

Belgian Waffler
Jan 2, 2001
32,675
146
106
www.neftastic.com
Nope, it's not managed. I pretty much knew that was the answer, looks like I'll have to pass the length along with the data since I can't rely on the terminator.
 

Kyteland

Diamond Member
Dec 30, 2002
5,747
1
81
With something like char p[] = "Some string" you can also do sizeof(p)/sizeof(char), but that won't help you if you have a pointer to p.
 

spamsk8r

Golden Member
Jul 11, 2001
1,787
0
76
You would most likely need to create a data structure that wraps an array and an object counter so you could retrieve the length in O(1) time (assuming you add 1 to the length every time you add an element, and remove 1 every time you delete one). You could even override the [] operator to make it act like an array (you could do the same for the * operator but that might prove confusing).
 

SunnyD

Belgian Waffler
Jan 2, 2001
32,675
146
106
www.neftastic.com
Originally posted by: spamsk8r
You would most likely need to create a data structure that wraps an array and an object counter so you could retrieve the length in O(1) time (assuming you add 1 to the length every time you add an element, and remove 1 every time you delete one). You could even override the [] operator to make it act like an array (you could do the same for the * operator but that might prove confusing).

That kind of defeats the purpose when it comes to using it for Win32 API calls written in C.
 

Venix

Golden Member
Aug 22, 2002
1,084
3
81
Originally posted by: SunnyD
Originally posted by: spamsk8r
You would most likely need to create a data structure that wraps an array and an object counter so you could retrieve the length in O(1) time (assuming you add 1 to the length every time you add an element, and remove 1 every time you delete one). You could even override the [] operator to make it act like an array (you could do the same for the * operator but that might prove confusing).

That kind of defeats the purpose when it comes to using it for Win32 API calls written in C.

I use std::vector all the time for use with Win32; if you want examples, feel free to ask.

If your code is C++, you should really avoid the crappy built-in arrays whenever possible.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
you could compile with a bounds-checking library and have it tell you when you've gone past the end of the data.
 

sao123

Lifer
May 27, 2002
12,653
205
106
C++.net provides a managed array class.
standard C++ does not. Its merely a buffer of memory.
 

programmer

Senior member
Mar 12, 2003
412
0
0
Basically if you have a pointer to the first member of an array *and* you know the data type of the pointer, you can get the length with the 'ol countof macro. It would work with your example with defines a null-terminated static string, but would not work if given just a char pointer. The countof method is great for arrays of structs or base types, but I would be leery of using it for strings -- a null check and strlen can do that. Here is some example code of what works and what wouldn't, and a link to a page discussing this very thing.

source code:

#include "stdio.h"

#ifndef countof
#define countof(x) (sizeof(x)/sizeof(x[0]))
#endif

int main(int argc, char* argv[])
{
char p1[] = "Some string";
wchar_t u1[] = L"Some wide string";

printf("The countof macro will work on arrays\n");
printf("The MBCS string '%s' is %ld characters long, including the terminating null\n", p1, countof(p1));
printf("The Unicode string '%S' is %ld characters long, including the terminating null\n", u1, countof(u1));
printf("\n");

char* p2 = "Some string";
wchar_t* u2 = L"Some wide string";

printf("The countof macro will NOT work on pointers\n");
printf("The MBCS string '%s' is %ld characters long, including the terminating null\n", p2, countof(p2));
printf("The Unicode string '%S' is %ld characters long, including the terminating null\n", u2, countof(u2));
printf("\n");

return 0;
}

output:

The countof macro will work on arrays
The MBCS string 'Some string' is 12 characters long, including the terminating null
The Unicode string 'Some wide string' is 17 characters long, including the terminating null

The countof macro will NOT work on pointers
The MBCS string 'Some string' is 4 characters long, including the terminating null
The Unicode string 'Some wide string' is 2 characters long, including the terminating null


See this page for a good discussion
http://blogs.msdn.com/the1/arc...2004/05/07/128242.aspx
 

SunnyD

Belgian Waffler
Jan 2, 2001
32,675
146
106
www.neftastic.com
Originally posted by: programmer
The countof macro will work on arrays
The MBCS string 'Some string' is 12 characters long, including the terminating null
The Unicode string 'Some wide string' is 17 characters long, including the terminating null

The countof macro will NOT work on pointers
The MBCS string 'Some string' is 4 characters long, including the terminating null
The Unicode string 'Some wide string' is 2 characters long, including the terminating null


See this page for a good discussion
http://blogs.msdn.com/the1/arc...2004/05/07/128242.aspx

That's the problem, I would need/want it to work on pointers. The only possible way I could do this on a pointer is if I had the length already, or the terminating unit - assuming there is one. I'll survive.
 

programmer

Senior member
Mar 12, 2003
412
0
0
Tough nut to crack. Since you are writing the code, you know when and where you need to do this calculation. Especially when you throw in void pointers, there is no way to find the "size" of the object being pointed to because you don't even know the type.

Again, if you control the code, you can attack this in different ways for different reasons (e.g. strlen or some equivalent for strings, countof for other kinds of arrays, other methods, etc.) -- but frankly I think you may spin your wheels trying to find a one-size-fits-all solution for this problem in C/C++.

Good luck with it. Perhaps if you gave more info or sample of exactly what kind of data you are doing this to and why the good folks here could come up with more suggestions.