3

I'm experiencing a problem. I'm trying to get the number of elements in an int array, without passing any explicit parameter, just the pointer to the int array. Now my curiosity:

int * set;
printf("-- %d --", sizeof(set)); //4
set=(int *) malloc(n*sizeof(int));
printf("-- %d --", sizeof(set)); //4  

Why are the values the same since before the malloc it isn't initialized and after it is. Thanks
UPDATE:
Is there any way to get the length of an int array?

2
  • There's no way to get the size of the pointed-to memory from the pointer value alone; you'll have to track it separately somehow. Commented May 4, 2012 at 21:17
  • Not with standard malloc implementations, but alternate implementations like dmalloc do in fact allow you to get the size from the pointer (they keep a lookup table internally). Commented May 4, 2012 at 21:30

5 Answers 5

9

Because sizeof is evaluated at compile time and yields the size of the type of set, a pointer to int.

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

Comments

6

There is no generic way to measure the size of memory pointed to by a pointer in C, other than the special case that strings are null terminated by convention.

sizeof will yield the size of the pointer (4 bytes on a 32-bit system, 8 bytes on a 64-bit system), not of the memory pointed to.

If you want to track the size of memory allocated, options are:

  • Track it in a separate variable
  • Introduce a special array terminator (for example, the minimum or maximum value of int if your application will never validly use that value.
  • Use an alternate memory management library (for dmalloc has dmalloc_examine, which will return the size of memory pointed to). These should drop right in with minimal or no code changes, except for where you want to use their expanded memory API.

Comments

3

The item you're measuring, set, is a pointer-to-integer ( int* ). And a pointer-to-integer is 4-bytes.

sizeof does NOT measure the amount of memory allocated to the pointer. It only measures the "item" itself (in this case, a pointer).

3 Comments

is there any way to measure the amount?
No way that is standard and portable. Nor is such a method needed. You know how much memory you allocated. If it is a value you will need in the future, you should keep track of that value in another variable.
dmalloc is not standard (separate library), but is highly portable. That is an option.
0

A pointer is 32 bits or 64 bits.

Meaning 4 or 8 bytes.

Comments

0
int * set;
printf("-- %d --", sizeof(set)); //4
set=(int *) malloc(n*sizeof(int));
printf("-- %d --", n*sizeof(int)); //n*sizeof(int) is the size of your malloc'd memory

hope that helps. in your last statement you were still asking the size of the individual integer pointer which was 4 bytes on your machine. to get the size of your malloced area of memory you need to use the same expression as is inside the malloc function.

and an integer pointer isn't necessarily the same size as an int itself as Mat corrected me on

1 Comment

certianly, I see now, I wasn't looking right before I've edited the response, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.