0

Possible Duplicate:
How to find the 'sizeof' (a pointer pointing to an array)?

I red this but it didn't help me much.

int* sum;
int size = 10;
sum = calloc(10, sizeof(int));

printf("Total length: %d", sizeof(sum)/sizeof(*sum))

The code returns 1 as expected.

I know that it would be easier to user the size variable.

My question is that is there a way of finding out this array correct size using sizeof?

0

1 Answer 1

2

Other than keeping track of the size yourself (eg. the size variable in your example), there's no portable/standard way of getting the size of a dynamically allocated block of memory.

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

2 Comments

Ok so in I want to pass this memory block as a function parameter it is necessary to pass the array size too. There is no other way. Thanks!
@TGM : pretty much, yes. You can make it more convenient by grouping the pointer and the size in a struct (and possibly even provide several functions to access and manipulate the data).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.