#I don't get this

1 messages · Page 1 of 1 (latest)

lament meadow
#
using Index = std::ptrdiff_t;

// Sample loop using index
for (Index index{ 0 }; index < static_cast<Index>(arr.size()); ++index)

why did the learncpp example uses ptrdifft? and why is it casing it to arr size? and why is it saying "what signed type should we use?" why can't u just use int

btw show me an example code of int being ineffective and will make the code break unless u use ptrdifft

cerulean geodeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

latent pasture
#

int, which is usually 32 bit, is prone to being overflowed/underflowed

#

arr size returns the unsigned pointer size which needs to be casted back to signed pointer size

#

i think ssize is probably the better alternative as its always signed

lament meadow
#

it's better for u to just give me an example code bc i have trouble understanding it

lament meadow
#

@latent pasture pls

languid otter
#

the underlying type ?

uncut totem
latent pasture
#

i cant give an example its just an integer

uncut totem
#

Bruh, heap I meant!

languid otter
#

i wonder if you can even get 3gb on stack

#

time to test it out

uncut totem
latent pasture
#

if you want to iterate over a array using ptrs or iterators or unsigned integers would be better

languid otter
uncut totem
lament meadow
uncut totem
lament meadow
#

give me a code that represents what ur talking about pls

#

how large can ptrdifft hold btw

uncut totem
languid otter
#

2^63

lament meadow
languid otter
#

then you will understand what std::ptrdiff_t does

#

or should do

lament meadow
#

why are u refusing to send an example code tho

#

just send it so I can look at it after I understand some

languid otter
#

if you don't understand pointer arithmetic then you won't understand neither the code example

#
#include <cstddef>
#include <iostream>
 
int main()
{
    const std::size_t N = 10;
    int* a = new int[N];
    int* end = a + N;
    for (std::ptrdiff_t i = N; i > 0; --i)
        std::cout << (*(end - i) = i) << ' ';
    std::cout << '\n';
    delete[] a;
}
lament meadow
#

how large does the element have to be to make it only for ptrdifft

uncut totem
#

;compile

constexpr unsigned limit = std::numeric_limits<int>::max() + 1U; // 2'731'540'480;
auto *pb = new std::byte[limit];
auto *pe = pb + limit;

assert(pb);
int diff = pe - pb;
std::ptrdiff_t pdiff = pe - pb;
// std::printf("%p\n%p\n", pb, pe);

std::cout << "allocated: " << limit << "\nwith int: " << diff << "\nwith ptrdiff: " << pdiff;
karmic breachBOT
#
Program Output
allocated: 2147483648
with int: -2147483648
with ptrdiff: 2147483648
lament meadow
#

;compile

constexpr unsigned limit = 2731540479;
auto *pb = new char[limit];
auto *pe = pb + limit;

assert(pb);
int diff = pe - pb;
std::ptrdiff_t pdiff = pe - pb;
// std::printf("%p\n%p\n", pb, pe);

std::cout << "with int: " << diff << '\n' << "with ptrdiff: " << pdiff;
karmic breachBOT
#
Program Output
with int: -1563426817
with ptrdiff: 2731540479
lament meadow
#

ok

#

I wonder how does it calculates the undefined int behavior

#

and why -1

lament meadow
uncut totem
#

It overflowed.

uncut totem
# lament meadow how did it become negative

The above example allocates 1byte more than 2GB, (2Gigs being the upper limit of int) so INT_MAX + 1, overflows the int, that's why it's really not fit for pointer difference.

lament meadow
#

but why does it +1 the int_max

uncut totem
lament meadow
#

why do u want to overflow it and then just -1 it after 💀 (or im misunderstanding it)

uncut totem
#

Not really -1'ing now, at this time it represents the canonical range, the pe is off the end pointer.

uncut totem
#

If you don't understand any of this, just keep in mind, int may not be fit for pointer difference, always opt for implementation defined std::ptrdiff_t, it exists for a reason just like std::size_t.

uncut totem
#

Just an example, I'm sure it's returned to the system after the program terminate s.