473,382 Members | 1,648 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,382 software developers and data experts.

Handling unsigned overflow of size_t

Hi all!

When dealing with dynamically adjusted objects are you ever concerned that
you'll double the requested size of the object and overflow size_t so that
the requested size e.g. becomes zero?

I realise it's extremely likely that all memory and address space will be
exhausted before one is able to malloc an object close to the maximum of
size_t. I'd just like to know best practice.

Let's consider a 32-bit address space with an object 0x80000000 size_t.
The algorithm is to always double the size of the object. When you compute
the new size of the object it will be zero.

To avoid this one could start with a size_t one less than a power of two
and always add one before doubling. Afterwards subtracting one again.

So the object above would be 0x7FFFFFFF size_t. One adds 1 (0x80000000),
doubles it (0x00000000) and subtracts 1 (0xFFFFFFFF) so that realloc
doesn't have a hope of succeeding and will return NULL to check for
instead of undefined behaviour resulting from reallocating a smaller
object.

On the other hand it feels odd to request a new size each time that is not
a power of two. Who ever allocates 2^n-1 bytes instead of 2^n?

Thanks for your advice.

Regards,
Adam
Nov 14 '05 #1
5 4916
"Adam Warner" <us****@consulting.net.nz> wrote in message
news:pa****************************@consulting.net .nz...
When dealing with dynamically adjusted objects are you ever concerned
that you'll double the requested size of the object and overflow size_t
so that the requested size e.g. becomes zero?


If you're concerned, why not just detect the overflow?

Alex
Nov 14 '05 #2
Hi Alex Fraser,

On Fri, 07 Jan 2005 10:18:23 +0000, Alex Fraser wrote:
"Adam Warner" <us****@consulting.net.nz> wrote in message
news:pa****************************@consulting.net .nz...
When dealing with dynamically adjusted objects are you ever concerned
that you'll double the requested size of the object and overflow size_t
so that the requested size e.g. becomes zero?


If you're concerned, why not just detect the overflow?


I've decided to keep allocating by 2^n except when overflow is detected:

if (new_realloc_size<=old_realloc_size) {
new_realloc_size=SIZE_MAX;
new_stack_size=new_realloc_size/sizeof(void *);
}

I expect this will be more idiomatic than reallocating by a size of
2^n-1, i.e. (previous_size+1)*2-1 each time; where if the previous_size
starts as one less than a power of 2 there is never any need to
explicitly check for unsigned overflow.

Regards,
Adam
Nov 14 '05 #3
On Sat, 08 Jan 2005 01:24:24 +1300, Adam Warner wrote:
Hi Alex Fraser,

On Fri, 07 Jan 2005 10:18:23 +0000, Alex Fraser wrote:
"Adam Warner" <us****@consulting.net.nz> wrote in message
news:pa****************************@consulting.net .nz...
When dealing with dynamically adjusted objects are you ever concerned
that you'll double the requested size of the object and overflow size_t
so that the requested size e.g. becomes zero?
If you're concerned, why not just detect the overflow?


I've decided to keep allocating by 2^n except when overflow is detected:

if (new_realloc_size<=old_realloc_size) {
new_realloc_size=SIZE_MAX;
new_stack_size=new_realloc_size/sizeof(void *);
}

I expect this will be more idiomatic than reallocating by a size of
2^n-1, i.e. (previous_size+1)*2-1 each time; where if the previous_size


Or just previous_size*2+1

Also consider not doubling the size each time, that can lead to a lot of
wastage. Something like previous_size + previous_size/2 can work well
given a suitable starting size.
starts as one less than a power of 2 there is never any need to
explicitly check for unsigned overflow.


You're assuming that an allocation of SIZE_MAX will fail. That's not
necessarily true, although likely on 32 bit and larger implementations.
You would be much better off testing for overflow, which as others have
said has happened if the new value is less than the old one. You can
probably reject the equal case too.

Lawrence
Nov 14 '05 #4
Lawrence Kirby wrote:
"Adam Warner" <us****@consulting.net.nz> wrote in message

When dealing with dynamically adjusted objects are you ever
concerned that you'll double the requested size of the object and
overflow size_t so that the requested size e.g. becomes zero?
.... snip ...
Also consider not doubling the size each time, that can lead to a
lot of wastage. Something like previous_size + previous_size/2 can
work well given a suitable starting size.


The starting size doesn't affect anything more than in the doubling
scheme. The only real difference is the minimum usage percentage.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5
CBFalconer wrote:

Lawrence Kirby wrote:
"Adam Warner" <us****@consulting.net.nz> wrote in message

> When dealing with dynamically adjusted objects are you ever
> concerned that you'll double the requested size of the object and
> overflow size_t so that the requested size e.g. becomes zero?

... snip ...

Also consider not doubling the size each time, that can lead to a
lot of wastage. Something like previous_size + previous_size/2 can
work well given a suitable starting size.


The starting size doesn't affect anything more than in the doubling
scheme.


Oh, but it does! With the strategy prev + prev / 2, two starting
values (0 and 1) are most /un/suitable, as I'm sure Mr Kirby is aware.
Nov 14 '05 #6

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

Similar topics

7
by: Matthias Czapla | last post by:
Hi! Whats the canonical way for handling raw data. I want to read a file without making any assumption about its structure and store portions of it in memory and compare ranges with constant...
96
by: John Harrison | last post by:
I knew that unsigned integral data types were the cause of scads of mostly spurious warning messages, but I didn't realise that they were a security risk too (see here...
1
by: Adam Warner | last post by:
Hi all! When dealing with dynamically adjusted objects are you ever concerned that you'll double the requested size of the object and overflow size_t so that the requested size e.g. becomes...
11
by: Andrew Poelstra | last post by:
I hammered this out this morning to fix inconsistancies with the way my programs handle errors. The code itself is fine, in that it compiles with Richard Heathfield's gcc tags (plus -c because it...
10
by: Jim Langston | last post by:
Is the following well defined? size_t IntVal = 65537; unsigned short Length; if ( IntVal static_cast<unsigned short>( -1 ) ) { std::cout << "Value too long to fit in a short" << std::endl;...
26
by: John Harrison | last post by:
I have a problem. I want to compare an integral value, n, against three half open ranges as follows [-A, 0) // range 1 [0, B) // range 2 [B, C} // range 3 Each range corresponds to a...
24
by: Paulo Matos | last post by:
Hello, Is it safe to assume a size_t is an unsigned long? (is it forced by the standard?) Thank you, Paulo Matos
105
by: Keith Thompson | last post by:
pereges <Broli00@gmail.comwrites: These types already have perfectly good names already. Why give them new ones? If you must rename them for some reason, use typedefs, not macros. --
10
by: Alex Vinokur | last post by:
Hi, Is it possible to do C++-casting from const pair<const unsigned char*, size_t>* to const pair<unsigned char*, size_t>* ? Alex Vinokur
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.