Segmentation Fault

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.
 
K

Keith Thompson

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.

Quibble: *objects* of type bool are addressable.
 
H

Herbert Rosenau

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);

Clearly undefined behavior - so anything can occure!

You ends up always and in each circumstance with UB ehen you uses a a
variable more than once in a single statement that has ide effects
like post- or preincrement. so the indec i * i tt isd clearly
undefined behavior as there is nothing that forbids to calculate
i=i+=1 before or after or in parallel the value of i (without the
increment operator) gets invoked.

An application that has undefined behavior can do anything it likes,
like demons out of your nouse, the third world wide war initiated, may
implode or explode the white house, the capitol or the kreml. it may
crash each airplane on air, ....

So nothing else on that program is worth to observe - game over!


--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2R Deutsch ist da!
 
H

Herbert Rosenau

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


It doesn't matter because i*i++ is undefined behavior twice!

i*i gives overflow and overflow of int is not defined

i*i++ itself is undefined behavior as the side effect on ++ is againxt
the rule that accessing the variable i more than once while ++ is in
that statement - so anything is broken.


--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2R Deutsch ist da!
 
H

Herbert Rosenau

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.

Twit shows again that he is a twit. INT_MAX*INT_MAX exceeds the
maximal value i can hold - that is overflow and undefined behavior is
guaranteed.

i*i++ is undefined behavior again - so multiple undefined behavior on
a single statement makes nothing than undefined behavior an a faulty
program. Your name is twit because you are a twit.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2R Deutsch ist da!
 
H

Herbert Rosenau

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?

So far so good but

in that loop undefined behavior occures twice:
i*i++ is undefined behavior because the value of i is used twice and
as the operator ++ wors on one it gives undefined behavior

In that loop undefined behavior occure too when the loop reaches the
value i/2+1 because then i*i overflows that give again undefined
behavior.

The answer to that question may give you a little more insight
into at least 2 quite literally bigger problems with the code.

So as the program has at least on undefined behavior on it anything
undefined occures when the program gets started - game over simply
before the game gets a chance to start.


--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2R Deutsch ist da!
 
P

Phil Carmody

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.

:-D This is why I don't implement a kill-anything-after-a-troll's-post
filter.

Maybe he has issues with the size of his bits?

Phil
 
P

Phil Carmody

Default User said:
So, boollocks?

Erm, Richard made that particular reference already. There's
no need to either repeat it or explain it, c.l.c isn't an
american sitcom (audience).

Phil
 
T

Tim Rentsch

Keith Thompson said:
Quibble: *objects* of type bool are addressable.

Counter quibble: bit-fields are also objects;
bit-fields of type bool are not addressable.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top