Thread 105948747 - /g/ [Archived: 160 hours ago]

Anonymous
7/18/2025, 7:48:51 PM No.105948747
carbon
carbon
md5: f2e5a3b670f724d6791123c05139f6f2🔍
Why does this print b = 20?
Is this C's bug?
Replies: >>105948761 >>105948793 >>105948818 >>105948820 >>105948888 >>105949676 >>105950693 >>105950811 >>105952054 >>105952222 >>105955353 >>105955766 >>105956275 >>105957203 >>105957400 >>105957962
Anonymous
7/18/2025, 7:50:42 PM No.105948761
>>105948747 (OP)
ints are 4 bytes, 5*4=20
Replies: >>105948915 >>105948963 >>105949671 >>105955353
Anonymous
7/18/2025, 7:54:33 PM No.105948793
>>105948747 (OP)
You do realize you're moving a fucking pointer. Not adding numbers.
Replies: >>105949094
Anonymous
7/18/2025, 7:56:56 PM No.105948818
>>105948747 (OP)
bc the pointer's virtual address is signed int 20
if gcc you have compiler warnings
Anonymous
7/18/2025, 7:57:08 PM No.105948820
>>105948747 (OP)
>anon learns pointers and dereferencing
Replies: >>105949186
caжe
7/18/2025, 8:05:32 PM No.105948888
>>105948747 (OP)
itoddlers should stick to swift
Anonymous
7/18/2025, 8:08:54 PM No.105948915
>>105948761
the last platforms that used 16 bit ints were m68k apple and turbo c in the 80's
Replies: >>105949223 >>105957962
Anonymous
7/18/2025, 8:13:27 PM No.105948963
>>105948761
I'm not gonna lie, I was afraid the level was so low nowadays the answer was not given. Based first responder.
Anonymous
7/18/2025, 8:27:53 PM No.105949094
>>105948793
No because JavaScript devs iq peaks at 95
Anonymous
7/18/2025, 8:30:09 PM No.105949115
>prints pointer address as integer
Anonymous
7/18/2025, 8:38:16 PM No.105949186
>>105948820
nothing is dereferenced in this case
Anonymous
7/18/2025, 8:42:33 PM No.105949223
>>105948915
4 bytes are 32 bits. Why are you bringing up 16 bit ints?
Replies: >>105949408
Anonymous
7/18/2025, 9:00:23 PM No.105949370
normally I just lurk. but 4chan is sped, never ask for any tech related advice (especially OS related). this has nothing to do with an int being 4 bytes. you can Google what a pointer is but essentially your taking a memory address, ie 0x0001 and iterating 5 to the address 0x0006. you are then printing this pointer as an int which is undefined behavior. the reason it is 20 is not obvious without a lot of debugging and desu not important at all. dont ask for help here ever everyone's fucking retarded
Replies: >>105949562 >>105949588
Anonymous
7/18/2025, 9:04:51 PM No.105949408
>>105949223
4*4=16 retrad
Replies: >>105949534 >>105957962
Anonymous
7/18/2025, 9:21:10 PM No.105949534
1398106136911
1398106136911
md5: ae460f5d0835f3c66749ceedc942f924🔍
>>105949408
A byte is 8 bits. I really hope you're being le ebin trollerman.
Replies: >>105949671
Anonymous
7/18/2025, 9:24:53 PM No.105949562
>>105949370
>everyone's fucking retarded
I'm just pretending to be retarded for the luls. I'm actually highly intelligent.
t. highest IQ on /g/
Anonymous
7/18/2025, 9:27:38 PM No.105949588
1623712061132
1623712061132
md5: 090014043562d80a85d365d297cceb98🔍
>>105949370
>taking a shitpost this seriously
You're not even a midwit, you're a straight up lackwit, lmao.
Anonymous
7/18/2025, 9:36:20 PM No.105949671
>>105948761
>ints are 4 bytes
says who?
>>105949534
>a byte is 8 bits
no it's not. stop making assumptions
Replies: >>105949712 >>105949718 >>105950222 >>105957962 >>105958438
Anonymous
7/18/2025, 9:36:29 PM No.105949676
>>105948747 (OP)
you're lucky to be on a little endian machine, otherwise this would print 0 and leave you confused.

a and b are pointers, and pointer arithmetic always adds in chunks of the pointed to type's size.
ints are 4 byte on many platforms these days, so 5 * [sizeof (int)] = 20.
hence a = 0, and b = 20. to correctly print the value of the pointer, use %x instead of %d.
Anonymous
7/18/2025, 9:38:45 PM No.105949712
1724086674939204
1724086674939204
md5: a397e5a75c61567a70ab2dc8fbffd910🔍
>>105949671
Replies: >>105949718
Anonymous
7/18/2025, 9:39:25 PM No.105949718
>>105949671
>no it's not. stop making assumptions
the programme displays behaviour consistent with little endianness and sizeof (int) == 4, so I think it's safe to assume that CHAR_BITS == 8 ;)

>>105949712
CHAR_BIT is not required to equal 8
Replies: >>105949751
Anonymous
7/18/2025, 9:42:04 PM No.105949751
>>105949718
Your breath does not need to smell like cock, but it still always does.
Anonymous
7/18/2025, 10:24:24 PM No.105950145
nigga u are assigning a pointer to memory address 0 and then creating a new pointer that is 20 bytes offset from it and then printing the memory address with %d so it gets represented in decimal. i pray this is bait and all of the replies as well.
Replies: >>105950185
Anonymous
7/18/2025, 10:29:13 PM No.105950185
>>105950145
>memory address 0
who says the null pointer has to have address 0
Replies: >>105950189
Anonymous
7/18/2025, 10:30:03 PM No.105950189
>>105950185
does the provided "code" have a nullptr?
Replies: >>105950567
Anonymous
7/18/2025, 10:33:48 PM No.105950222
>>105949671
Wtf, how can byte not be 8 bit?
As for int being 4 bytes it depends on arch and the compiler I guess so it's not wrong to say it's not necessarily 4 bytes.
Replies: >>105952528 >>105958438
Anonymous
7/18/2025, 10:38:01 PM No.105950265
>be me, advanced computer
>made in one of those factories china wants so much
>billion computations per second
>made from parts all across the world
>a marvel of technology

>be user
>outdated organic being with lower intellect
>asks me what's the integer stored in the virtual address 0x5
>the answer infuriates him
>he immediately opens mongolian snowboard forum
>starts to type furiously and discuss with all those others humans
>writes a meltdown rant and then goes jerkoff
>fuck my life
Anonymous
7/18/2025, 11:09:54 PM No.105950567
>>105950189
in c
insertpointerhere = 0;
yields a null pointer
Anonymous
7/18/2025, 11:20:45 PM No.105950693
>>105948747 (OP)
Retard
Anonymous
7/18/2025, 11:31:17 PM No.105950811
>>105948747 (OP)
>nocoders shilling c actually have no idea how pointers work
many, many such cases
Anonymous
7/19/2025, 2:19:15 AM No.105952054
>>105948747 (OP)
this is very surprising the first time it happens, but basically the reason why is because pointer arithmetic takes into account the underlying type that's being pointed to.

in other words, the following are equivalent:
int *b = a + 5;
int *b = &a[5];

but the second one makes it much more obvious.

for this reason i always 'index' into pointers, since it prevents this kind of misunderstanding.

if you first cast `a` to a void pointer, then it will print what you originally expected:

int *b = (void *)a + 5;


however this may cause problems on some architectures which expect integer load/stores to be aligned on power-of-two boundaries, possibly slow down performance, in extreme case it will trigger a system trap.
Replies: >>105955312 >>105957268 >>105957319
Anonymous
7/19/2025, 2:44:53 AM No.105952202
Imagine solving OP's CS assignment for free
Replies: >>105952238 >>105952240
Anonymous
7/19/2025, 2:48:42 AM No.105952222
>>105948747 (OP)
Does MacOS natively make these weird jeety screencaps?
Replies: >>105952232 >>105953539
Anonymous
7/19/2025, 2:49:40 AM No.105952232
>>105952222
the apple name is """"screenscapes"""
Anonymous
7/19/2025, 2:50:07 AM No.105952238
>>105952202
the fact that he's here and not at chatgpt.com is a strong indicator that this is a real thread.
Anonymous
7/19/2025, 2:50:22 AM No.105952240
>>105952202
Imagine giving advice to people for free
Now go back to your cage wagie
Anonymous
7/19/2025, 3:27:23 AM No.105952528
>>105950222
>how can byte not be 8 bit
By not being 8 bits of course. Do you have so little imagination that you can't imagine this? Do you have so little knowledge that you can't think of actual historical machines that didn't have 8-bit bytes? The IBM 7090 had 6-bit bytes. The PDP-10 could have byte sizes ranging from 1 to 36 bits.
Anonymous
7/19/2025, 6:13:03 AM No.105953539
>>105952222
i like them
Anonymous
7/19/2025, 12:05:41 PM No.105955312
>>105952054
Thank you, chatgpt.
Anonymous
7/19/2025, 12:14:29 PM No.105955353
>>105948747 (OP)
>>105948761
I understand that you put a value of 5 + address written in a but why is it 20?
Replies: >>105955425 >>105958528
Anonymous
7/19/2025, 12:26:46 PM No.105955425
>>105955353
>but why is it 20?
Because a starts at address 0 and an int is 32 bits.
So address 0, 1, 2, 3 are the a[0]
4, 5, 6, 7 are a[1]
8, 9, 10, 11 are a[2]
12, 13, 14, 15 are a[3]
16, 17, 18, 19 are a[4]
20, 21, 22, 23 are a[5]
The first byte of the word is the address of that word.
Anonymous
7/19/2025, 1:25:42 PM No.105955766
>>105948747 (OP)
It is that easy to farm (you)s huh

It is an UB btw and you are all retarded
Replies: >>105955782 >>105956136 >>105958589
Anonymous
7/19/2025, 1:27:05 PM No.105955782
>>105955766
>It is an UB btw
>everything I don't understand is UB
Replies: >>105955810
Anonymous
7/19/2025, 1:31:11 PM No.105955810
>>105955782
t. retard
Anonymous
7/19/2025, 2:17:00 PM No.105956124
nullptr
nullptr
md5: 1ce2d559898c84f33e05d2bc09a41200🔍
Null pointer is valid.
Replies: >>105956602
Anonymous
7/19/2025, 2:18:33 PM No.105956136
1711847809920336
1711847809920336
md5: 32782497b3c95d0ec4b51b295b91126f🔍
>>105955766
>pointer arithmetic is UB
Replies: >>105956251 >>105957259
Anonymous
7/19/2025, 2:33:42 PM No.105956251
>>105956136
It indeed is, read the standard retard
Anonymous
7/19/2025, 2:36:08 PM No.105956275
>>105948747 (OP)
Was it necessary to include the gay syntax highlighting and the itoddler window?
Anonymous
7/19/2025, 3:19:23 PM No.105956602
>>105956124
But adding to it is not
Anonymous
7/19/2025, 4:33:45 PM No.105957203
>>105948747 (OP)
a is a pointer, not a number.
This is the color palette's fault. It gives pointers and stored variables the same color, when 90% of coding mishaps occur because they different worlds of function.
Anonymous
7/19/2025, 4:41:53 PM No.105957259
>>105956136
adding non-zero value to a null point is UB, yes
Replies: >>105957330
Anonymous
7/19/2025, 4:42:58 PM No.105957268
>>105952054
>however this may cause problems on some architectures which expect integer load/stores to be aligned on power-of-two boundaries
Does amd64 care about alignment? I know GCC is anal about it but does the processor care?
Replies: >>105957319
Anonymous
7/19/2025, 4:43:59 PM No.105957273
void *ops_brain = NULL;
Anonymous
7/19/2025, 4:51:56 PM No.105957319
>>105957268
Modern "big" processors handle unaligned accesses transparently with zero or close to zero penalty, unless the access crosses the cache line or page. But this is only if you are accessing memory. Many (most?) loads are not handled by a memory (cache) subsystem, but are resolved by store-to-load forwarding (within load-store queue). This is where alignment becomes important, because there are limits to how CPU can forward data there. So unaligned loads from a huge array have no penalty, but passing small 2-word structure to a function through memory unaligned can cause stalls until cache handles all accesses.
>>105952054
By the standard, (void*) is an incomplete type and you cannot add anything to such pointers. You have to use (char *). Variant with (void *) is a GCC extension.
Anonymous
7/19/2025, 4:52:38 PM No.105957330
>>105957259
It's implementation defined and the implemented definition is almost certainly all zeros unless you time traveled from 1984.
Replies: >>105957350 >>105957380
Anonymous
7/19/2025, 4:55:41 PM No.105957350
>>105957330
>It's implementation defined
What is implementation defined and is all zeroes?
I said that adding 5 to a NULL pointer is UB.
Replies: >>105957493
Anonymous
7/19/2025, 5:00:09 PM No.105957380
>>105957330
Pointer arithmetic requires operand and final result to be "pointing to the same array boundary or one element past it", otherwise it is undefined.

The standard is a bit fuzzy for null pointers but it sounds like it should belong to the "otherwise undefined" part, neither source nor result points to the same array object, there is no object to speak of
Anonymous
7/19/2025, 5:02:54 PM No.105957400
>>105948747 (OP)
The only correct answer is "undefined behavior", without knowing the underlying architecture we cannot assume that an integer is 32bit
Anonymous
7/19/2025, 5:15:05 PM No.105957493
>>105957350
Except it isn't, because a null pointers bit representation is zeros, which is why you can add to it and then print that bit representations integer value. it matters not that the address is garbage or still null, since we are not attempting to dereference it. we are merely changing its address.
Replies: >>105957621 >>105957650
Anonymous
7/19/2025, 5:31:32 PM No.105957621
6.5.6 - 8
6.5.6 - 8
md5: e2cfa0e4136771c6eec381da5ba02cad🔍
>>105957493
>it matters not that the address is garbage or still null, since we are not attempting to dereference it. we are merely changing its address.
Not how it works, Chuddie. See 6.5.6/8.
Replies: >>105957658 >>105960126
Anonymous
7/19/2025, 5:34:46 PM No.105957650
>>105957493
>manual pointer math in an operating system environment where the OS has ultimate arbitration over memory addressing
yeah that's not going to work out bud
Replies: >>105957664
Anonymous
7/19/2025, 5:36:06 PM No.105957658
>>105957621
That has absolutely nothing to do with what we're talking about.
Replies: >>105957835
Anonymous
7/19/2025, 5:37:07 PM No.105957664
>>105957650
>literally worked in op
Anonymous
7/19/2025, 5:58:21 PM No.105957835
C23_6.5.6_8
C23_6.5.6_8
md5: 8feb7df6fa37d5cb4c56352c8bdc1bfa🔍
>>105957658
Did you even read it? I will even give you updated version from C23.
I don't know what YOU are talking about, but adding non-zero value to a NULL pointer is UB. NULL pointer representation is irrelevant here.
Anonymous
7/19/2025, 6:11:55 PM No.105957962
>>105948747 (OP)
Fuck, you're dumb.
>>105948915
Fuck, you're dumb.
>>105949408
Fuck, you're dumb.

>>105949671
>be genuinely retarded
>get caught on it
>act retarded on purpose to try and retrofit your previous stupidity into trolling intent
No, you're just retarded, and probably will never escape wageslavery.
Anonymous
7/19/2025, 7:12:50 PM No.105958438
>>105949671
>says who?
OP is using one of Windows, MacOS, Linux, Android, or iOS. Anything else is way too fucking obscure for someone who doesn't know basic pointer arithmetic. The int data type is 32 bits on all such platforms, and CHAR_BIT on all such platforms is 8.

>>105950222
Byte is defined as the smallest addressable unit of memory, not as 8 bits. There are some CPUs which used 18 bit or 36 bit words, and naturally that meant that the smallest addressable unit was 9 bits. If you want to be consistent in referring to something that is always 8 bits, the term you are looking for is octet. Although for data transmitted over the Internet, 1 byte = 1 octet.

The number of bits in a byte is represented in C using the constant CHAR_BIT.
Anonymous
7/19/2025, 7:20:47 PM No.105958528
>>105955353
Google pointer arithmetic
Anonymous
7/19/2025, 7:28:38 PM No.105958589
>>105955766
typed pointer math is not UB, dunning-kruger
Replies: >>105958685
Anonymous
7/19/2025, 7:39:43 PM No.105958685
>>105958589
then explain this chud
https://godbolt.org/z/PraKvExdP
Anonymous
7/19/2025, 10:24:04 PM No.105960126
>>105957621
ub is fak enws made by crabs