[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
int main(int argc, char *argv[]) { *(int *) 0; return
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 17
Thread images: 2
File: 20160304120538.png (13 KB, 663x376) Image search: [Google]
20160304120538.png
13 KB, 663x376
int main(int argc, char *argv[])
{
*(int *) 0;

return 0;
}


Before you compile and run this, take a guess at what it will do first.
>>
>>53368328
uhhh. NULL?
>>
segfault?
>>
>>53368347
>>53368365
Now compile and run it to see what actually happens.
>>
>>53368386
pretty neat. but i'm not that deep into C to understand.
>>
all it did was move some shit around

00000000004004ed <main>:
int main(int argc, char *argv[])
{
4004ed: 55 push %rbp
4004ee: 48 89 e5 mov %rsp,%rbp
4004f1: 89 7d fc mov %edi,-0x4(%rbp)
4004f4: 48 89 75 f0 mov %rsi,-0x10(%rbp)
*(int *) 0;

return 0;
4004f8: b8 00 00 00 00 mov $0x0,%eax
}
4004fd: 5d pop %rbp
4004fe: c3 retq
4004ff: 90 nop
>>
>>53368386
Explain what it does op.
>>
>>53368328
what does this do? Move the memory address of the primitive 0?
>>
>>53368328
I thought the image said "reptiles only" nm.
>>
>>53368328
It's undefined behavior. It could do anything depending on the compiler, version of it and compile flags.
>>
>>53368507
>compile it
>run it
>it just returns zero

seems pretty well defined to me
>>
>>53368328
Probably nothing. Compiler will notice we do nothing with the value and won't even compile that line.
>>
Aren't you trying to reference a null pointer that way?
>>
>>53368527
Probably because your compiler removed that part. It would be perfectly acceptable if it crashed instead.

It's pretty similar to this: http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html
>>
$ gcc main.c -o main -Wall
main.c: In function ‘main’:
main.c:3:2: warning: statement with no effect [-Wunused-value]
*(int *) 0;
^
$
>>
File: kim-jong-un1.jpg (26 KB, 399x359) Image search: [Google]
kim-jong-un1.jpg
26 KB, 399x359
Unlock a terrorist iphone and simultaneously swap the content of 2 registers.
>pic related. My serious face.
>>
>>53368328
alexis@VMCS-V5:/tmp$ gcc -g -Wall main.c
main.c: In function ‘main’:
main.c:3:5: warning: statement with no effect [-Wunused-value]
*(int *) 0;
^


(gdb) disass main
Dump of assembler code for function main:
0x00000000004004a6 <+0>: push %rbp
0x00000000004004a7 <+1>: mov %rsp,%rbp
0x00000000004004aa <+4>: mov %edi,-0x4(%rbp)
0x00000000004004ad <+7>: mov %rsi,-0x10(%rbp)
0x00000000004004b1 <+11>: mov $0x0,%eax
0x00000000004004b6 <+16>: pop %rbp
0x00000000004004b7 <+17>: retq
End of assembler dump.
(gdb)
Thread replies: 17
Thread images: 2

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.