N
Nobody
Remember that a bool is one single bit
As others have pointed out, this isn't true. Values of type "bool" are
addressable (you can have pointer-to-bool), which precludes using
individual bits.
Remember that a bool is one single bit
Nobody said:As others have pointed out, this isn't true. Values of type "bool" are
addressable (you can have pointer-to-bool), which precludes using
individual bits.
Hello Friends ~
We have the assignment to write efficient function to decide if an integer
is a square. An integer n is a square if n = m * m where m is an other
integer. Then write a test program to show whether the first ten integers
are squares.
I believe my code is correct and caches all results in an Array for
efficiency (only look up is needed). But it generates a Segmentation Fault
everytime I run it.
Can anyone see what the problem is?
~~ Thanks
#include"stdio.h"
#include"limits.h"
#include"stdbool.h"
static bool squares[INT_MAX]; // automatically initialized to 0, no
memset needed
setupsquares ()
{
int i;
for(i = 0; i < INT_MAX; squares[i * i++] = true);
I believe my code is correct and caches all results in an Array for
efficiency (only look up is needed). But it generates a Segmentation Fault
everytime I run it.
Can anyone see what the problem is?static bool squares[INT_MAX]; // automatically initialized to 0, no
You shouldn't assume that you will be able to allocate an array this
large (on a 32-bit system, INT_MAX is typically 2^31-1, which would
require at least 2GiB of memory).
setupsquares ()
{
int i;
for(i = 0; i < INT_MAX; squares[i * i++] = true);
If i can go up to INT_MAX-1, then i*i can go up to (INT_MAX-1) squared,
which is outside the bounds of the array. But you almost certainly won't
be able to allocate an array that large (for INT_MAX == 2^31-1,
that's approximately 4 Exabytes).
Wrong.
By your usual "assume nothing beyond the ISO Standard" bullshit, it
could be as few as 32767 bools. Hardly "one heck of a lot" in the modern
age.
setupsquares ()
{
int i;
for(i = 0; i < INT_MAX; squares[i * i++] = true);
}
Why miss out INT_MAX - it's a perfectly valid int value?
The answer to that question may give you a little more insight
into at least 2 quite literally bigger problems with the code.
Richard said:Mr Twink, as usual, is talking a complete load of bools.
Richard Heathfield said:Not only that, but 32767 /is/ a heck of a lot of bools in an
environment where INT_MAX is 32767.
Mr Twink, as usual, is talking a complete load of bools.
Default User said:So, boollocks?
Keith Thompson said:Quibble: *objects* of type bool are addressable.
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.