Sign in to follow this  

Length of array using pointer

This topic is 3773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! Everyone, int *array; Suppose, I've initialized it in somewhere. Now, how do I know the length of the array using this pointer? int getLength() { return ..... what? } Thanks in advance.

Share this post


Link to post
Share on other sites
You can't.
You can either inject a terminator character in that array, and check for it or store the length in a variable.
Maybe you can create a struct with an int to indicate the length:


struct intArray
{
int* data
int length;
};

Share this post


Link to post
Share on other sites
To expand on the above suggestion to keep the length around yourself, your main alternatives are, in C++, to use a container (std::vector offers all the storage benefits of raw memory buffers, without the disadvantages), and in C to keep the size of the object around (this generally forces you to implement the array bookkeeping for every type).

A generic but type-unsafe system would be:

typedef struct { 
size_t size;
void * data;
};



A generic type-safe template-like system could look like:

#define ARRAY(T) struct { T *data; size_t size; }

#define ALLOC_ARRAY(a,size) do { \
a.data = malloc( size * sizeof(*a.data) ); \
a.size = size; } while(0)

#define RESIZE_ARRAY(a,size) do { \
a.data = realloc( a.data, size * sizeof(*a.data) ); \
a.size = size; } while(0)

#define FREE_ARRAY(a,size) do { \
free(a.data); a.data = 0; } while(0)

#define VALID_ARRAY(a) (a.data != 0)

#define ARRAY_GET(a,i) (a.data[i])
#define ARRAY_SET(a,i,x) do { \
a.data[i] = (x); } while(0)


WIth the obvious "macros are evil" restriction that a should always be an array variable and not an expression.

Share this post


Link to post
Share on other sites
There are compiler dependent methods for determining the size of a dynamically allocated array. For instance on MSVC, you can use _msize() on a pointer to determine how many bytes were allocated on that pointer.

Previous thread on this topic.

Share this post


Link to post
Share on other sites
Quote:
Original post by moo_gamer
Hi! Everyone,

int *array;

Suppose, I've initialized it in somewhere.

Now, how do I know the length of the array using this pointer?

int getLength() {

return ..... what?

}

Thanks in advance.


you can't, and it's not good practise. You must keep the length somewhere.
I suggest you use some sort of container (Like a Linked List or a Vector or some sort of ArrayList, etc..)

Share this post


Link to post
Share on other sites
Just to clarify:

Quote:
Original post by SiCrane
There are compiler dependent methods, for some compilers/platforms, for determining the size of a dynamically allocated array.


Quote:
Original post by memento_mori
you can't portably, and it's not good practise. ...the length must be kept somewhere.

Share this post


Link to post
Share on other sites

This topic is 3773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this