473,327 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

size of an array without sizeof operator

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

Mar 5 '07 #1
15 6522
su**************@yahoo.com, India wrote:
Is it possible to measure the size of an array without using the
sizeof operator ?
Why would you want to? That's what sizeof is for.

--
Ian Collins.
Mar 5 '07 #2
su**************@yahoo.com, India wrote:
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?

Mar 5 '07 #3
"su**************@yahoo.com, India" <su**************@yahoo.com>
writes:
Is it possible to measure the size of an array without using the
sizeof operator ?
The answer is the same as it has been in every single thread
that brings up this stupid question. Do some research.
--
"Am I missing something?"
--Dan Pop
Mar 5 '07 #4
On Mon, 05 Mar 2007 14:28:20 +1300, Ian Collins wrote:
su**************@yahoo.com, India wrote:
>Is it possible to measure the size of an array without using the
sizeof operator ?
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 ;)
Mar 5 '07 #5
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.

Mar 5 '07 #6
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.

Mar 5 '07 #7
su**************@yahoo.com, India wrote, On 05/03/07 01:47:
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.
--
Flash Gordon
Mar 5 '07 #8
<su**************@yahoo.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.com...
>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

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov

--
Posted via a free Usenet account from http://www.teranews.com

Mar 5 '07 #9
On Sun, 04 Mar 2007 18:40:49 -0800, su**************@yahoo.com, India
wrote:
<snip>
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.
Mar 5 '07 #10
su**************@yahoo.com, India wrote:
[ ... ]
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.

Mar 5 '07 #11
"su**************@yahoo.com, India" wrote:
>
.... 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
Mar 5 '07 #12
On Mar 5, 6:26 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
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(ja*******@gmail.com)

Mar 27 '07 #13
ja*******@gmail.com said:
On Mar 5, 6:26 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
>Is it possible to measure the size of an
arraywithoutusingthesizeofoperator ?

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!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 27 '07 #14
On Mar 27, 1:59 pm, Richard Heathfield <r...@see.sig.invalidwrote:
jamshe...@gmail.com said:
On Mar 5, 6:26 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Is it possible to measure the size of an
arraywithoutusingthesizeofoperator ?
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!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
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.




Mar 27 '07 #15
ja*******@gmail.com said:

<snip>
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).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 27 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
11
by: Linny | last post by:
Hi, I need some help in declaring an array of pointers to array of a certain fixed size. I want the pointers to point to arrays of fixed size only (should not work for variable sized arrays of the...
5
by: sugaray | last post by:
Hi, my problem with calculating the size of an array is when I pass an array as a parameter to a function which perform the calculation, the result never comes right, like below: int...
55
by: Robotnik | last post by:
Hello All, I want to know if we could know the size of a structyure without the use of sizeof(). Any hints.
12
by: manochavishal | last post by:
Hi, I have a question. How can i know the size of array when it is passed to a function. For Example i have this code: #include <stdio.h> #include <stdlib.h>
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
11
by: mast2as | last post by:
This question has been posted to this forum before and I read the thread but found that the answers were perhaps imcomplete, so I am trying again. Whenever I am creating objects I would like to...
42
by: Jeff P. Syverson | last post by:
Can anyone tell me the best way to find out the size of a struct, without using the sizeof function? I'm currently thinking of: struct foo s; int size_of_foo = (char *)&s - (char *)s; but is...
24
by: arnuld | last post by:
I want to create a new array of the same size of an array already available: #include <stdio.h> #include <string.h> int main(void) { char arrc = "URI";
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.