Why this C program is crashing?


 
Thread Tools Search this Thread
Top Forums Programming Why this C program is crashing?
# 1  
Old 04-26-2011
Why this C program is crashing?

Hi,

Why I am getting 'SIGSEGV' in the following code?
Code:
char* p="abcde";
printf("%s", 3[p]);     // Segmentation Fault (core dump)

Kindly help me to understand what exactly makes the program to crash or the reason for the crashing.
# 2  
Old 04-26-2011
Don't you mean p[3] instead of 3[p]? Also, p[3] will give you a single char, whereas you'll need a char * for the printf, so better use p+3 instead.
# 3  
Old 04-26-2011
Quote:
Originally Posted by royalibrahim
Kindly help me to understand what exactly makes the program to crash or the reason for the crashing.
Kindly help me understand what you were even trying to do here, I'm not sure. It's crashing because you're giving %s something that's not a string.
# 4  
Old 04-26-2011
Code:
char* p="abcde";
printf("%c", 3[p]);

should work fine. I believe that this is just a test of the fact that p[3] and 3[p] are supposed to behave the same way. He probably going to enter the obfuscated C contest or something like that. Smilie
# 5  
Old 04-26-2011
When you ask UNIX to reference memory you do not own, the OS generates a signal, SIGSEGV. This triggers a crash dump.

%s in printf assumes that what you give it is an address in memory that points to a nul-terminated array of characters. IT blindly goes ahead and tries to access a valid string. Instead it tried to access some unknown address outside of process memory, so it crashed.
This User Gave Thanks to jim mcnamara For This Post:
# 6  
Old 04-26-2011
Quote:
Originally Posted by Perderabo
Code:
char* p="abcde";
printf("%c", 3[p]);

should work fine. I believe that this is just a test of the fact that p[3] and 3[p] are supposed to behave the same way. He probably going to enter the obfuscated C contest or something like that. Smilie
Hehehe. I was thinking the same thing.

A bit more info for those interested in this "feature": Question 6.11

And ... it gets worse:

Code:
$ cat acomm.c
#include <stdio.h>

int
main(int argc, char **argv) {
	char *s = "char *s";
	char *a[1][2][3];
	a[0][1][2] = s;

	(void) printf("%s\n", a[0][1][2]);
	(void) printf("%s\n", 2[1[0[a]]]);
	(void) printf("%s\n", 2[0[a][1]]);
	(void) printf("%s\n", 1[a[0]][2]);
	/* etc */

	return 0;
}


$ cc -Wall -pedantic acomm.c 
$ ./a.out 
char *s
char *s
char *s
char *s
$ cc --version
i686-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5370)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 04-27-2011
Quote:
Originally Posted by jim mcnamara
When you ask UNIX to reference memory you do not own, the OS generates a signal, SIGSEGV. This triggers a crash dump.

%s in printf assumes that what you give it is an address in memory that points to a nul-terminated array of characters. IT blindly goes ahead and tries to access a valid string. Instead it tried to access some unknown address outside of process memory, so it crashed.
Awesome !! Thanks a ton Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Application keep on crashing need to troubleshoot

Hi experts, I am using RHEL as my distro and I have a very important application running on that server. That application is a third party application and I don't have control with the source. That application keeps on crashing or stop like every other day and it doesn't have any traces on... (1 Reply)
Discussion started by: cwiggler
1 Replies

2. Solaris

Solaris V440 keeps crashing

Hi, I have Sun Solaris V440, every two days or so, the system crashes and it's at OK prompt. After that I do the FSCK and clear all the bad sectors. Then system stays up for close to 48 hours. Then it crashes again. I checked the logs and I don't see anything under /var/adm/messages file. ... (1 Reply)
Discussion started by: samnyc
1 Replies

3. UNIX for Dummies Questions & Answers

Random Crashing

Over the last month or so my CentOS server has been crashing for reasons I do not know. It has been running for over a year with regular yum updates without problems. The load on the server is perfectly normal with CPU usage at 5-6% and RAM usage at less than half of 32GB of RAM (multiple smaller... (3 Replies)
Discussion started by: spinner0205
3 Replies

4. UNIX and Linux Applications

Firefox crashing

Firefox keeps on crashing every time I try to run it. I tried to create a new profile with no luck. When I try to open up the profile manager firefox crashes. I even deleted my profile folder and tried to start with a new profile. That did not work either. I don't know what the problem could be. I... (6 Replies)
Discussion started by: cokedude
6 Replies

5. Web Development

MySQL Server Crashing need Help

Hi, we have some problem with mysql high cpu , would like some help with MySQL Tuning here are the mysqltuner & tuning-primer details mysqltuner: # mysqltuner >> MySQLTuner 1.1.1 - Major Hayden <major@mhtx.net> >> Run with '--help' for additional options and output filtering --------... (1 Reply)
Discussion started by: cataplexy
1 Replies

6. Programming

C++ program is crashing on re-assigning const static member variable using an int pointer

Hi, Can any one tell me why my following program is crashing? #include <iostream> using namespace std; class CA { public: const static int i; }; const int CA::i = 10; int main() { int* pi = const_cast<int*>(&CA::i); *pi = 9; cout << CA::i << endl; } (6 Replies)
Discussion started by: royalibrahim
6 Replies

7. Red Hat

Fedora 11 crashing help

Using Fedora 11, just about every day the system started crashing. Becomes unresponsive to keyboard/mouse, nothing appears on screen but box is still running. Still responds to ping, arp address stays alive in the firewall, other than that its unresponsive Where can I look to find out... (1 Reply)
Discussion started by: ippy98
1 Replies

8. Solaris

Solaris 10 crashing

Hi, My system is crashing with following error .. i tried to boot from the network and unencapsulated the root disk from SVM .. but still not able to boot the box , can any one point me to some direction .. i do not want to build the box new as of now just want to troubleshoot if possible.. ... (3 Replies)
Discussion started by: fugitive
3 Replies

9. Programming

c++ class keeps crashing

i just started learning to prog in c++, im using netbeans. i tryed to make a class and i keep getting this error. what am i doing wrong ? ----------------------------- class test { public: int funtime(); private: int time; }; int test::funtime()... (4 Replies)
Discussion started by: akira300
4 Replies
Login or Register to Ask a Question