size of an array without sizeof operator

  • Thread starter subramanian100in
  • Start date
S

subramanian100in

Is it possible to measure the size of an array without using the
sizeof operator ?
 
S

santosh

Is it possible to measure the size of an array without using the
sizeof operator ?

Not portably. sizeof is the standard for a reason, why do you want to
jump through hoops to simulate it imperfectly?
 
W

William Ahern

Why would you want to? That's what sizeof is for.

To please your professor?

I was going to say "To pass a class". But that would be rather ambiguous ;)
 
S

subramanian100in

I saw this question in one of the earlier posts but the answer was not
given.

I am learning C from home. I have a PC at home. I am reading K & R
second edition. I post my doubts in this forum to get clarified. I am
preparing for interview. That is why I am asking.

I thought of doing it in one way without using sizeof. But it ASSUMES
that sizeof pointer should not be bigger than maximum value of
unsigned long or unsigned long long. Before writing the code, let me
explain it in plain English because, in this forum I wil get to know
if the assumption is correct.

In the following, assume that size, str, etc exist.

Suppose I have, for some specific Type,
Type a[size];

I calculate

sprintf(str1, "%p", (void *)(a+1));
sprintf(str2, "%p", (void *)a);

Then I have my own routine to convert str1 and str2 to unsigned long
or unsigned long long based on the ASSUMPTION mentioned above. I find
the difference of these converted numbers and then multiply the
difference by size.

This is what strikes me.

Correct me for all errors.
 
S

subramanian100in

I saw this question in one of the earlier posts but the answer was not
given.

I am learning C from home. I have a PC at home. I am reading
K & R second edition. I post my doubts in this forum to get clarified.
I am preparing for interview. That is why I am asking.

I thought of doing it in one way without using sizeof. But it ASSUMES
that sizeof pointer should not be bigger than maximum value of
unsigned long or unsigned long long. Before writing the code, let me
explain it in plain English because, in this forum I wil get to know
if the assumption is correct.

In the following, assume that size, str, etc exist.


Suppose I have, for some specific Type,
Type a[size];


I calculate


sprintf(str1, "%p", (void *)(a+1));
sprintf(str2, "%p", (void *)a);


Then I have my own routine to convert str1 and str2 to unsigned long
or unsigned long long based on the ASSUMPTION mentioned above. I find
the difference of these converted numbers and then multiply the
difference by size.

This is what strikes me.

Correct me for all errors.
 
F

Flash Gordon

I saw this question in one of the earlier posts but the answer was not
given.

<snip>

How can you have seen the question but not seen the answer that it
always given?

Also, please quote just enough of the post you are replying to to
provide context. There is no guarantee that the person reading your post
will have seen (or will ever see) the post you are replying to.
 
C

CBFalconer

.... snip ...

Suppose I have, for some specific Type,
Type a[size];

I calculate

sprintf(str1, "%p", (void *)(a+1));
sprintf(str2, "%p", (void *)a);
.... snip ...

Correct me for all errors.

Why should the result of the sprintf be meaningful as an integer?
Maybe the output of sprintf is "Go to schoolhouse, room C. Select
small boy third from left in front row. Ask him where that data is
stored. Give him a slate on which to write his answer."

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
S

Stephen Sprunk

I saw this question in one of the earlier posts but the answer was not
given.

I am learning C from home. I have a PC at home. I am reading
K & R second edition. I post my doubts in this forum to get clarified.
I am preparing for interview. That is why I am asking.

I thought of doing it in one way without using sizeof. But it
ASSUMES that sizeof pointer should not be bigger than maximum
value of unsigned long or unsigned long long. Before writing the
code, let me explain it in plain English because, in this forum I wil
get to know if the assumption is correct.

In the following, assume that size, str, etc exist.

Suppose I have, for some specific Type,
Type a[size];

I calculate

sprintf(str1, "%p", (void *)(a+1));
sprintf(str2, "%p", (void *)a);

Then I have my own routine to convert str1 and str2 to unsigned
long or unsigned long long based on the ASSUMPTION mentioned
above. I find the difference of these converted numbers and then
multiply the difference by size.

This is what strikes me.

Correct me for all errors.

"(char*)(a+1)-(char*)(a)" will yield the same result as sizeof(Type).
Multiply by "size" and you will get the size of the total array. No need to
jump through any further hoops like you describe.

Usually when people ask this question, they don't know "size", or they're
trying to reimplement the sizeof() function in general. Neither of those
problems can be solved portably; what you're asking is a more specific case
that can.

S
 
W

William Ahern

Suppose I have, for some specific Type,
Type a[size];


I calculate


sprintf(str1, "%p", (void *)(a+1));
sprintf(str2, "%p", (void *)a);


Then I have my own routine to convert str1 and str2 to unsigned long
or unsigned long long based on the ASSUMPTION mentioned above. I find
the difference of these converted numbers and then multiply the
difference by size.

This is what strikes me.

Correct me for all errors.

That sounds like a roundabout way of simply doing:

ptrdiff_t sz = (char *)(a + 1) - (char *)a;

Your sprintf() method isn't viable because the format of the %p
specifier is unspecified. It's too late for me to figure out whether the
cast-and-subtract is well defined.

Also, your original post was ambiguous. I had originally figured that
the length of the array was also an unknown.
 
S

santosh

I thought of doing it in one way without using sizeof. But it ASSUMES
that sizeof pointer should not be bigger than maximum value of
unsigned long or unsigned long long. Before writing the code, let me
explain it in plain English because, in this forum I wil get to know
if the assumption is correct.

In the following, assume that size, str, etc exist.

Suppose I have, for some specific Type,
Type a[size];

I calculate

sprintf(str1, "%p", (void *)(a+1));
sprintf(str2, "%p", (void *)a);

Then I have my own routine to convert str1 and str2 to unsigned long
or unsigned long long based on the ASSUMPTION mentioned above. I find
the difference of these converted numbers and then multiply the
difference by size.

This is what strikes me.

This won't do what you want.
 
J

jamsheedm

Is it possible to measure the size of an arraywithoutusingthesizeofoperator ?

Yes, its possible. I am explaining it below with an example.

int a[10];

printf("total size occupied by the array is %d\n", ( ((&a+1)-&a) *
((char*)(a+1)-(char*)a) );
this will yield the same size as sizeof(a) yeilds.

Is that the answer that you are expecting?..

Regards
Jamsheed M([email protected])
 
R

Richard Heathfield

(e-mail address removed) said:
Yes, its possible.

Albeit silly.
I am explaining it below with an example.

int a[10];

printf("total size occupied by the array is %d\n", ( ((&a+1)-&a) *
((char*)(a+1)-(char*)a) );
this will yield the same size as sizeof(a) yeilds.

Well, it's hard to see how, since it won't actually compile. (Check your
parentheses.)

When I fix that, and add a printf of the result of sizeof, I get the
following code:

#include <stdio.h>

int main(void)
{
int a[10];

printf("total size occupied by the array is %d\n",
(((&a + 1) - &a) * ((char *)(a + 1) - (char *)a)));

printf("sizeof a = %d\n", (int)sizeof a);
return 0;
}

When I run this code, I get the following output:

total size occupied by the array is 4
sizeof a = 40
Is that the answer that you are expecting?..

Sure as eggs as eggs it isn't the answer *you* were expecting!
 
J

jamsheedm

(e-mail address removed) said:
Yes, its possible.

Albeit silly.
I am explaining it below with an example.
int a[10];
printf("total size occupied by the array is %d\n", ( ((&a+1)-&a) *
((char*)(a+1)-(char*)a) );
this will yield the same size as sizeof(a) yeilds.

Well, it's hard to see how, since it won't actually compile. (Check your
parentheses.)

When I fix that, and add a printf of the result of sizeof, I get the
following code:

#include <stdio.h>

int main(void)
{
int a[10];

printf("total size occupied by the array is %d\n",
(((&a + 1) - &a) * ((char *)(a + 1) - (char *)a)));

printf("sizeof a = %d\n", (int)sizeof a);
return 0;

}

When I run this code, I get the following output:

total size occupied by the array is 4
sizeof a = 40
Is that the answer that you are expecting?..

Sure as eggs as eggs it isn't the answer *you* were expecting!

Yes you are right.

the code need to be changed like this.

int main()
{
int a[10];
printf("total size =%d\n",(char*)(&a+1)-(char*)(&a));
printf("sizeof value=%d\n",sizeof(a));
}


this both will print the same size 40.
 
R

Richard Heathfield

(e-mail address removed) said:

Yes you are right.

the code need to be changed like this.

int main()
{
int a[10];
printf("total size =%d\n",(char*)(&a+1)-(char*)(&a));
printf("sizeof value=%d\n",sizeof(a));
}

No, the code needs to be removed to take away the silly array
calculation that takes far longer to type than sizeof. If you want to
know the size of an array, use sizeof. That's what it's *for* - finding
out the sizes of things (and types of things).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top