11 Replies - 911 Views - Last Post: 11 January 2010 - 09:20 AM Rate Topic: -----

#1 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Why does it crash?

Posted 11 January 2010 - 08:14 AM

I have just a simple question: why does this program crash at the end of the execution? (The crash happens when it reaches 'getch' after I hit a key.)
#include <iostream>
#include <cstring>
#include <conio.h>
using namespace std;

void ex1() {
	char s[20];
	strcpy(s, "ideale");
	for (int i = 0, n = strlen(s); i < n; i++) {
		if (s[i] == 'e') cout << '*';
		else cout << 'e' << s[i];
	}
}

void ex2() {
	char s[20];
	strcpy (s, "absdefg");
	strcpy(s+2, s+4);
	cout << s << " " << strlen(s);
}

void ex3() {
	char s[50], x[50];
	strcpy (s, "abcdefg");
	strcpy(x, s+4);
	strcpy(s+4, "123");
	strcat(s, x);
	cout << s;
}

void ex4() {
	char s[50];
	strcpy(s, "abbacdde");
	int i = 0;
	while (i < strlen(s)-1) {
		if (s[i] == s[i+1]) strcpy(s+i, s+i+1);
		else i++;
	}
	cout << s;
}

void ex5() {
	char s[12];
	strcpy(s, "abracadabra");
	int i = 0;
	cout << strlen(s);
	while (i < strlen(s)) {
		if (s[i] == 'a') strcpy(s+i, s+i+1);
		else i++;
	}
	cout << s;
}

void ex6() {
	char s[12];
	strcpy(s, "abcdefghoid");
	cout << strlen(s);
	for (int i = 0, z = strlen(s); i < z; i++) {
		if (strchr("aeiou", s[i])) s[i] = s[i] - 'a' + 'A';
	}
	cout << s;
}

void ex7() {
	char s[12];
	strcpy(s, "abcdefghoid");
	cout << strlen(s) << " ";
	int i = 0;
	while (i < strlen(s)) {
		if (strchr("aeiou", s[i])) strcpy(s+i, s+i+1);
		else i++;
	}
	cout << s;
}

void ex8() {
	char s[10];
	strcpy(s, "abcduecda");
	int i = 0, j = strlen(s) - 1;
	cout << strlen(s) << " ";
	while (i < j) {
		if (s[i] == s[j]) {
			strcpy(s+i, s+i+1);
			strcpy(s+j, s+j+1);
		} else {
			i++;
			j--;
		}
	}
	cout << s;
}

void ex9() {
	char s[10];
	strcpy(s, "abameiut");
	cout << strlen(s) << " ";
	int i = 0;
	while (i < strlen(s)) {
		if (strchr("aeiou", s[i])) {
			strcpy(s+i, s+i+1);
			i++;
		} else i += 2;
	}
	cout << s;
}

void ex10() {
	char s[100];
	int k = 'a' - 'A';
	strcpy(s, "clasa a-XII-a A");
	cout << s;
	for (int i = 0, j = strlen(s); i < j; i++) {
		if ((s[i] >= 'a') && s[i] <= 'z') s[i] = s[i] - k;
	}
	cout << s;
}

void ex11() {
	char s[100];
	strcpy(s, "bacalaureat");
	cout << strlen(s) << " ";
	for (int i = 0, j = strlen(s); i < j; i++) {
		if (strchr("aeiou", s[i])) cout << '*';
	}
}

void ex12() {
	char s[100];
	strcpy(s, "bac2008");
	cout << s << " ";
	for (int i = 0, j = strlen(s); i < j; i++) {
		if (strchr("0123456789", s[i])) cout << s[i];
	}
}

void ex13() {
	char s[4];
	strcpy(s, "arac");
	s[1] = 't';
	cout << s;
}

void ex14() {
	char s[100];
	strcpy(s, "tamara");
	for (int i = 0, j = strlen(s); i < j; i++) {
		char *p = strchr(s, 'a');
		cout << p - s;
	}
}

void ex15() {
	char s1[20], s2[20], s3[20];
	strcpy (s1, "algoritm");
	strcpy(s2, "bioritm");
	strcpy(s3, "ritm");
	if (strlen(s1) < strlen(s2)) strcat (s3, s1);
	else strcat(s3, s2);
	cout << s1 << s2 << s3;
}

void ex16() {
	char c[20];
	strcpy(c, "abracadabra");
	int i = 6;
	cout << c[i] << c[i+1];
	while (i > 0) {
		cout << c[i];
		i--;
	}
}

int main()
{
	cout << "1. ";  ex1();  cout << endl;
	cout << "2. ";  ex2();  cout << endl;
	cout << "3. ";  ex3();  cout << endl;
	cout << "4. ";  ex4();  cout << endl;
	cout << "5. ";  ex5();  cout << endl;
	cout << "6. ";  ex6();  cout << endl;
	cout << "7. ";  ex7();  cout << endl;
	cout << "8. ";  ex8();  cout << endl;
	cout << "9. ";  ex9();  cout << endl;
	cout << "10. "; ex10(); cout << endl;
	cout << "11. "; ex11(); cout << endl;
	cout << "12. "; ex12(); cout << endl;
	cout << "13. "; ex13(); cout << endl;
	cout << "14. "; ex14(); cout << endl;
	cout << "15. "; ex15(); cout << endl;
	cout << "16. "; ex16(); cout << endl;
	getch();
	return 0;
}

This post has been edited by diego_pmc: 11 January 2010 - 08:46 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Why does it crash?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: Why does it crash?

Posted 11 January 2010 - 08:20 AM

Doesn't even compile for me...you're missing an include.
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Why does it crash?

Posted 11 January 2010 - 08:24 AM

I am pretty sure that some of these functions to not respect array boundaries. ex8() for example.
Was This Post Helpful? 0
  • +
  • -

#4 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: Why does it crash?

Posted 11 January 2010 - 08:29 AM

View PostJackOfAllTrades, on 11 Jan, 2010 - 07:20 AM, said:

Doesn't even compile for me...you're missing an include.

I added #include <string> the result is the same.
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Why does it crash?

Posted 11 January 2010 - 08:39 AM

the header you need would be <cstring>

Here is how I would proceed to troubleshoot... comment out the second half of tests from main()... if it still crashes, comment out half of the remaining... if it does not, uncomment the first half and comment out the bottom half...

hopefully there is only 1 bounds violation... if the cash is caused by multiple reasons than this bracketing may not work...

you might also put the getch() after each test...
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: Why does it crash?

Posted 11 January 2010 - 08:41 AM

A hint from valgrind:
==4797== Source and destination overlap in strcpy(0x7ff0005f0, 0x7ff0005f1)
==4797==	at 0x4C25348: strcpy (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==4797==	by 0x40114C: ex9() (diego.cpp:100)
==4797==	by 0x4016AD: main (diego.cpp:183)
==4797== 


which occurs in quite a few of your examples and is undefined behavior. From here:

Quote

If copying takes place between objects that overlap, the behavior is undefined.

Was This Post Helpful? 0
  • +
  • -

#7 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: Why does it crash?

Posted 11 January 2010 - 08:46 AM

View PostNickDMax, on 11 Jan, 2010 - 07:24 AM, said:

I am pretty sure that some of these functions to not respect array boundaries. ex8() for example.

I guess you are talking about the first two times the strcpy(s+j, s+j+1) line is read.
That's what we've been given in class.

View PostNickDMax, on 11 Jan, 2010 - 07:35 AM, said:

the header you need would be <cstring>


Okay.
Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Why does it crash?

Posted 11 January 2010 - 08:50 AM

Yea I would avoid things like this: strcpy(s+2, s+4); typically if you want the memory spaces to overlap you would use memmove

Quote

That's what we've been given in class.

Well I think you might be able to get some bonus points if you can show that this is undefined by the standard...
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: Why does it crash?

Posted 11 January 2010 - 08:57 AM

So, I'm gathering we just did your homework for you :(
Was This Post Helpful? 0
  • +
  • -

#10 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Why does it crash?

Posted 11 January 2010 - 09:00 AM

see here -- gives a good starting point on where to look in the standard -- you can find a links to standards documents here
Was This Post Helpful? 0
  • +
  • -

#11 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: Why does it crash?

Posted 11 January 2010 - 09:15 AM

View PostJackOfAllTrades, on 11 Jan, 2010 - 07:57 AM, said:

So, I'm gathering we just did your homework for you :(

Don't worry. The homework was to say what the output was for each of the examples. I first did it on paper and then wrote the code to see if I got anything wrong (I didn't :P). What I'm doing here is extracurricular.

View PostNickDMax, on 11 Jan, 2010 - 07:50 AM, said:

Quote

That's what we've been given in class.

Well I think you might be able to get some bonus points if you can show that this is undefined by the standard...

Yeah, maybe I should. I wanted to talk with the teacher about something else anyway (in the courses we got it said that there is no type made specifically for strings of characters in C++ and that's why we have to use char* or char[], but that's obviously wrong).


So, thanks for the help and the links.

This post has been edited by diego_pmc: 11 January 2010 - 09:16 AM

Was This Post Helpful? 0
  • +
  • -

#12 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Why does it crash?

Posted 11 January 2010 - 09:20 AM

So the "correct" answer to those questions would be: Undefined / implementation dependent -- does not conform to C standards.

Trust me -- that is what I would turn in. And I would get full credit. (and the teacher would probably hate me -- which would only make the class tougher... but I would still pass with an A -- I am very tough on teachers, I absolutly HATE when a teacher gives false information, so I keep them on thier toes -- some teacher trying to pass off Turbo C in a programming class would hate me daily).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1