zeldor.biz

Linux, programming and more

Copyright © 2023
Log in

Segmentation fault example

March 6, 2012 by Igor Drobot Leave a Comment


A tiny Segmentation fault study example (Page Fault).

This tiny code will produce a SIGSEGV (Segmentation fault) after compiling and executing

Create file example.c and fill it with this code:

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
 
int main()
{
  char* text = "hello world";
 
  text = 13423;
  printf(text);
}
 
</stdio.h>

#include <stdio.h> int main() { char* text = "hello world"; text = 13423; printf(text); } </stdio.h>

Compile:

1
gcc -o example example.c

gcc -o example example.c

Run and get a Segmentation fault:

1
./example

./example

Debug Segmentation fault:

1
strace ./example

strace ./example

Filed Under: Linux, Programming Tagged With: C, compiling, GCC, Segmentation fault, strace

Categories

Archives

Tags

apache2 Apple arduino ARM Automation backup bash Cisco Cluster Corosync Database Debian Debian squeeze DIY DNS Fedora FTP Fun Icinga Ipv6 KVM Linux LVM MAC OS X Monitoring MySQL Nagios Nginx openSUSE OpenVPN PHP Proxy Python python3 qemu RAID rsync Samba security ssh Ubuntu virtualization Windows Windows 7 Wordpress

Leave a Reply

Your email address will not be published. Required fields are marked *