473,324 Members | 2,567 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,324 software developers and data experts.

how to measure size of an array

Can anyone tell how to measure the size of an array without use of
sizeof operator ?

Feb 14 '07 #1
14 3355
"asit dhal" <pe********@gmail.comwrites:
Can anyone tell how to measure the size of an array without use of
sizeof operator ?
How about referring to one of the many threads in the last few
months that discuss this same lame question, instead of starting
a new thread?
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Feb 14 '07 #2
On Feb 14, 9:02 am, "asit dhal" <penasit...@gmail.comwrote:
Can anyone tell how to measure the size of an array without use of
sizeof operator ?
The size information is given at declaration of an array. When an
array is passed into a function, the sizeof can't evaluate the size of
the array inside the function.

Feb 14 '07 #3
asit dhal:
Can anyone tell how to measure the size of an array without use of
sizeof operator ?

(char*)pover - (char*)p

--
~/JB\~
Feb 14 '07 #4
"asit dhal" <pe********@gmail.comwrites:
Can anyone tell how to measure the size of an array without use of
sizeof operator ?
Use the sizeof operator. That's what it's for.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 14 '07 #5
asit dhal wrote:
Can anyone tell how to measure the size of an array without use of
sizeof operator ?
No.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Feb 14 '07 #6
lovecreatesbea...@gmail.com said:
On Feb 14, 9:02 am, "asit dhal" <penasit...@gmail.comwrote:
>Can anyone tell how to measure the size of an array without use of
sizeof operator ?

The size information is given at declaration of an array. When an
array is passed into a function, the sizeof can't evaluate the size of
the array inside the function.
You don't pass an array to a function. You pass a pointer to that
array's first element.

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

"Jamie Boy" <a@b.cwrote in message n
>Can anyone tell how to measure the size of an array without use of
sizeof operator ?


(char*)pover - (char*)p

Make that unsigned char *.
char * should be reserved for genuine character data, and can contain trap
representations. A highly unlikely but allowable implementation could even
crash your program if one of the pointers points to a trap representation.
--
~/JB\~

Feb 14 '07 #8
"Malcolm McLean" <re*******@btinternet.comwrites:
"Jamie Boy" <a@b.cwrote in message n
>>Can anyone tell how to measure the size of an array without use of
sizeof operator ?
(char*)pover - (char*)p
Make that unsigned char *.
char * should be reserved for genuine character data, and can contain trap
representations. A highly unlikely but allowable implementation could even
crash your program if one of the pointers points to a trap representation.
I don't believe this. Can you cite chapter & verse on that?
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Feb 14 '07 #9
Malcolm McLean wrote, On 14/02/07 22:50:
"Jamie Boy" <a@b.cwrote in message n
>>Can anyone tell how to measure the size of an array without use of
sizeof operator ?

(char*)pover - (char*)p

Make that unsigned char *.
char * should be reserved for genuine character data, and can contain trap
representations. A highly unlikely but allowable implementation could even
crash your program if one of the pointers points to a trap representation.
What the pointers point to is irrelevant since they are not dereferenced
so casting to char* is fine. The problem with it is how you determine
the correct value of pover before you know the size of the array.
--
Flash Gordon
Feb 15 '07 #10
"Malcolm McLean" <re*******@btinternet.comwrites:
"Jamie Boy" <a@b.cwrote in message n
>>Can anyone tell how to measure the size of an array without use of
sizeof operator ?


(char*)pover - (char*)p

Make that unsigned char *.
char * should be reserved for genuine character data, and can contain trap
representations. A highly unlikely but allowable implementation could even
crash your program if one of the pointers points to a trap representation.
I don't believe so. The code does not attempt to evaluate any
pointed-to char object, just the pointers themselves.

Yes, unsigned char is generally better than char for this kind of
thing, but in this case there's no problem using char. (Assuming that
"pover" and "p" are defined appropriately; "Jamie Boy" gave no hint of
what they are.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 15 '07 #11

"Ben Pfaff" <bl*@cs.stanford.eduwrote in message
"Malcolm McLean" <re*******@btinternet.comwrites:
>"Jamie Boy" <a@b.cwrote in message n
>>>Can anyone tell how to measure the size of an array without use of
sizeof operator ?
(char*)pover - (char*)p
Make that unsigned char *.
char * should be reserved for genuine character data, and can contain
trap
representations. A highly unlikely but allowable implementation could
even
crash your program if one of the pointers points to a trap
representation.

I don't believe this. Can you cite chapter & verse on that?
No, you are right. It only traps when it is a "lvalue", which doesn't mean
"left hand side of the equation" but when you try to read it. So in this
case neither of the pointers are dereferenced and it can't trap. Just
pointing to it is OK. Additonally the rules have been changed on character
traps. It seems that they don't generate UB any more until you pass them to
a text stream or sprintf().

Feb 15 '07 #12
On Feb 14, 9:02 am, "asit dhal" <penasit...@gmail.comwrote:
Can anyone tell how to measure the size of an array without use of
sizeof operator ?
If you don't want to use the sizeof operator, two things that come to
mind are:

1) Keep track of the length of the array (or a pointer to the end) as
you build it, and that way you always know how big it is.

2) Put a sentinel value at the end of the array and the count the
number of elements from the start of the array until you hit the
sentinel.

The string library functions often work this second way, with a '\0'
character at the end of the array. That's how they know when to stop
counting, copying, printing, or whatever.

-Beej

Feb 15 '07 #13
On 2ÔÂ15ÈÕ, ÉÏÎç5ʱ21·Ö, Richard Heathfield <r...@see.sig.invalidwrote:
lovecreatesbea...@gmail.com said:
On Feb 14, 9:02 am, "asit dhal" <penasit...@gmail.comwrote:
Can anyone tell how to measure the size of an array without use of
sizeof operator ?
The size information is given at declaration of an array. When an
array is passed into a function, the sizeof can't evaluate the size of
the array inside the function.

You don't pass an array to a function. You pass a pointer to that
array's first element.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
very good!

Feb 15 '07 #14
On 2ÔÂ15ÈÕ, ÉÏÎç9ʱ41·Ö, "Beej" <b...@beej.uswrote:
On Feb 14, 9:02 am, "asit dhal" <penasit...@gmail.comwrote:
Can anyone tell how to measure the size of an array without use of
sizeof operator ?

If you don't want to use the sizeof operator, two things that come to
mind are:

1) Keep track of the length of the array (or a pointer to the end) as
you build it, and that way you always know how big it is.

2) Put a sentinel value at the end of the array and the count the
number of elements from the start of the array until you hit the
sentinel.

The string library functions often work this second way, with a '\0'
character at the end of the array. That's how they know when to stop
counting, copying, printing, or whatever.

-Beej
very very good!!!!!!!!!!!!!!!

Feb 15 '07 #15

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

Similar topics

22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
1
by: Anand Ganesh | last post by:
Hi All, I have an image which is made to fit in a Window. The user can Zoom In or Zoom Out and Pan the image. These images are Engineering drawings which has been drawn to scale say 1 inch =...
2
by: AlirezaH | last post by:
How to measure hard disk capacity?
1
by: Frank Rizzo | last post by:
I am doing some memory profiling work and I am trying to figure out the minimum size of an object. The object has a bunch of variables that are other objects (hashtables, array lists, and...
5
by: Joe Fallon | last post by:
Is there a good way to measure the size of a page delivered to the browser? Also, how do measure the size of Viewstate? I am just using View Source, saving the text file and looking at the number...
12
by: Crirus | last post by:
I need to measure how long a function take to execute... or a sequence of code within -- Ceers, Crirus ------------------------------ If work were a good thing, the boss would take it all...
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>
6
by: Scirious | last post by:
People, how can I know how big an object is? I mean, I have an object the collects data from a stream and when it grows to an especific size I need to create a new object to continue collecting the...
3
by: Neagu, Adrian | last post by:
Hello everybody, I try to solve the following problem: I have a python program that takes a lot of memory (>hundred Mb). I made an improvement (I hope) and I want to measure the gain (if...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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
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.