Hello,
Suppose I have a pointer to some structures. What would be the correct
type to store the number of entries in that array, as well as for indexing
that array? size_t seems like an obvious choice, but off_t also jumps to
mind.
Thanks,
--
Sean Hamilton <sh@bel.bc.ca > 6 1689
Sean Hamilton wrote: Hello,
Suppose I have a pointer to some structures. What would be the correct type to store the number of entries in that array, as well as for indexing that array? size_t seems like an obvious choice, but off_t also jumps to mind.
size_t is a good choice. off_t is not part of standard C, so obviously
that is not a good choice.
Bjørn
In article <pa************ *************** *@bel.bc.ca>,
Sean Hamilton <sh@bel.bc.ca > wrote: Hello,
Suppose I have a pointer to some structures. What would be the correct type to store the number of entries in that array, as well as for indexing that array? size_t seems like an obvious choice, but off_t also jumps to mind.
Unsigned types like size_t always have the disadvantage that they behave
in non-mathematical ways when you subtract 1 from 0.
if (i > count-1)
will give a complete nonsense result if count == 0. So you have to watch
out constantly to get everything right.
Use int if the number of elements is less than 32767, use long if the
number of elements is less than 2 billion.
Christian Bau wrote: In article <pa************ *************** *@bel.bc.ca>, Sean Hamilton <sh@bel.bc.ca > wrote:
Hello,
Suppose I have a pointer to some structures. What would be the correct type to store the number of entries in that array, as well as for indexing that array? size_t seems like an obvious choice, but off_t also jumps to mind.
Unsigned types like size_t always have the disadvantage that they behave in non-mathematical ways when you subtract 1 from 0.
if (i > count-1)
will give a complete nonsense result if count == 0. So you have to watch out constantly to get everything right.
Is that really a problem? I know that the code above is just an example,
but it could easily be written as
if(i + 1 > count)
if(i >= count)
and there would be no problem, as far as I can tell. Use int if the number of elements is less than 32767, use long if the number of elements is less than 2 billion.
Since functions/operators like sizeof/malloc/strlen/strspn and many,
many more all returns or expects a size_t, why not use it?
Bjørn
"Sean Hamilton" <sh@bel.bc.ca > wrote Suppose I have a pointer to some structures. What would be the correct type to store the number of entries in that array, as well as for indexing that array? size_t seems like an obvious choice, but off_t also jumps to mind.
In ANSI C, size_t is correct, since an array may not be larger than size_t's
maximum value.
However it is rather ugly. Also, is it reasonable to suppose that you might
have a company with more than 32767 employees, and also reasonable to
suppose that you might be running on a machine with 16-bit ints, but surely
not reasonable to suppose that a company with more that 32768 employees
would runs its payroll on a machine with 16 bits.
In practise int is better. You also have the advantage that, assuming garbge
values are random, 50% of them will be negative. If indicies are ints you
therefore have a reasonable chance of detecting garbage.
Christian Bau <ch***********@ cbau.freeserve. co.uk> wrote: Unsigned types like size_t always have the disadvantage that they behave in non-mathematical ways when you subtract 1 from 0.
Au contraire.. it behaves according to the rules of modular
arithmetic. Much better than signed types, which can cause
undefined behaviour when you subtract them.
In article <iQ************ *******@juliett .dax.net>,
Bjørn Augestad <bo*@metasystem s.no> wrote: Christian Bau wrote:
In article <pa************ *************** *@bel.bc.ca>, Sean Hamilton <sh@bel.bc.ca > wrote:
Hello,
Suppose I have a pointer to some structures. What would be the correct type to store the number of entries in that array, as well as for indexing that array? size_t seems like an obvious choice, but off_t also jumps to mind.
Unsigned types like size_t always have the disadvantage that they behave in non-mathematical ways when you subtract 1 from 0.
if (i > count-1)
will give a complete nonsense result if count == 0. So you have to watch out constantly to get everything right.
Is that really a problem? I know that the code above is just an example, but it could easily be written as if(i + 1 > count) if(i >= count) and there would be no problem, as far as I can tell.
Yes, you could. Actually, you have to. Which means that of two similar
ways to write your code, one contains a subtle bug, and one doesn't.
Sometimes you will get it wrong. Much better to avoid the problem in the
first place. Use int if the number of elements is less than 32767, use long if the number of elements is less than 2 billion.
Since functions/operators like sizeof/malloc/strlen/strspn and many, many more all returns or expects a size_t, why not use it?
See reasons above. Just because the C Standard Library functions do it,
doesn't mean it is a good idea. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Steve |
last post by:
I'm working on an e-commerce site, and one of the things I need to do is split an existing order into two orders. The problem I'm having is not
creating the new order, but getting the remaining items from the original order cleaned up in the array. What I've tried to do so far is:
1) The data is stored in a serialized array in the order_data field in the orders table. When the order is selected, it is unserialized and called
$order_data....
|
by: Michael Drumheller |
last post by:
(If you're not interested in NumArray, please skip this message.)
I am new to NumArray and I wonder if someone can help me with
array-indexing. Here's the basic situation: Given a rank-2 array
(i.e., a matrix) it seems to be trivial, with array indexing,
to extract a subset of its *columns*. But it does not seem
to be trivial to extract a subset of its *rows*. The code
snippet below describes the problem (if it really is a problem)...
|
by: Randell D. |
last post by:
Folks,
I can program fairly comfortably in PHP and can, for the most part using
these skills and others that I've picked up over the years manage to
read/understand most code in Javascript... so I'm just asking for a few
pointers (or the full solution if you have the time) for what I want to do.
Basically, I want to write a javascript wherby I only need to pass it the
names of form fields - then my javascript will check each form field...
|
by: Alan Johnson |
last post by:
For various reasons I'd like to be able to inherit from the built in
types, which obviously you cannot do. To get around this, I made a
template to emulate the built in types. So far I can't find any
situations in which this template fails to work interchangably with the
built in types. My question is if anyone knows of any gotcha type
situations in which this is going to fail.
Thanks,
Alan
|
by: VK |
last post by:
A while ago I proposed to update info in the group FAQ section, but I
dropped the discussion using the approach "No matter what color the cat
is as long as it still hounts the mice". Over the last month I had
enough of extra proof that the cat doesn't hount mice anymore in more
and more situations. And the surrent sicretisme among array and hash is
the base for it.
I summarized all points in this article:...
| |
by: shmartonak |
last post by:
For maximum portability what should the type of an array index be? Can
any integer type be used safely? Or should I only use an unsigned type?
Or what?
If I'm using pointers to access array elements as *(mptr+k) where I've
declared
MYTYPE *mptr;
what should be the type of 'k'? Should it be ptrdiff_t?
|
by: Martin v. Löwis |
last post by:
I've been working on PEP 353 for some time now.
Please comment, in particular if you are using 64-bit
systems.
Regards,
Martin
PEP: 353
Title: Using ssize_t as the index type
Version: $Revision: 42333 $
|
by: Patrick Sullivan |
last post by:
dim s_err as stringbuilder
dim xx(6) as double
dim ret_flag as integer
' This is a function to call an unmanaged C-style library function.
' The lib function fills an array of 6 doubles or error string.
Declare Auto Function calc Lib "calc32.dll" Alias "calc32.dll"( ByRef xx()
As Double, ByVal serr As System.Text.StringBuilder) As Integer
|
by: dennis.sam |
last post by:
Hi,
Is there away to define a multi-dimensional array with respect to the
number of dimensions the array has? For example, given a user spec of
"a b c d", I want to create a 4 dimensional array with dimensional
lengths of a, b, c and d. Thanx for any help.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |