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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
TGM
  • 1,659
  • 10
  • 30
  • 45

1 Answers1

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.

Sander De Dycker
  • 16,053
  • 1
  • 35
  • 40
  • 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 Oct 28 '11 at 13:25
  • @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). – Sander De Dycker Oct 28 '11 at 14:37