LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2004, 02:17 PM   #1
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Rep: Reputation: 30
segmentation fault in c++


Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

using namespace std;

char* newpwd(char* oldpwd)
{
	char* newpw;
	for (int i=0; i<8; i++)
		newpw[i] = (char) (97+rand()%26);
	newpw[8]='\0';
	strcpy(oldpwd, newpw);
	
	return oldpwd;
}

main ()
{
	char entered[80];
	char real[80];
	srand((unsigned int) time (NULL));
	printf("New password: %s\n", newpwd(real));
	for ( ; ; )
	{
	scanf("%s", entered);
	if (strcmp(entered, "new") == 0)
		printf("New password: %s\n", newpwd(real));
	else if (strcmp(entered, real) == 0)
		printf("Correct!\n");
	else
		printf("INCORRECT!\n");
	}
}
This program is to train me to use a new password, and to find one i can type easily.

however, i get a segmentation fault whenever i run it what is wrong with it?
 
Old 06-14-2004, 03:23 PM   #2
KneeLess
Member
 
Registered: May 2003
Distribution: Debian GNU/Linux 3.0 Sid, OpenBSD 3.5
Posts: 190

Rep: Reputation: 30
Well there are a few things to do, and note what you wrote is C not C++. (Subtle difference, ) To maintain compatibility with your main() you'll want to not make newpw a pointer, rather an array... like this
Code:
char* newpwd(char* oldpwd)
{
	char newpw[8]; /* See? */
	for (int i=0; i<8; i++)
		newpw[i] = (char) (97+rand()%26);
	newpw[8]='\0';
	strcpy(oldpwd, newpw);
	
	return oldpwd;
}
The problem was that char *newpw is just a pointer to a char, it does not automatically allocate space. if you wanted to do that you'd have to do this.
Code:
#include <malloc.h>

char* newpwd(char* oldpwd)
{
	char* newpw = calloc(8, sizeof(char*));
	for (int i=0; i<8; i++)
		newpw[i] = (char) (97+rand()%26);
	newpw[8]='\0';
	strcpy(oldpwd, newpw);
	
	return oldpwd;
}
And this program suffers from a buffer overflow too in main() from char entered[80]. What if I enter a password that is 1024 characters long? Think of a solution for that. Also note that a bash script that uses mkpasswd would probably be easier to write, more effient, and more secure.

Last edited by KneeLess; 06-14-2004 at 03:25 PM.
 
Old 06-14-2004, 03:54 PM   #3
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
thanks, it works now.

btw it doesnt compile as c because of hte for (int i=0..... so i have to use c++ to compile it so its c++ in my opinion.
 
Old 06-14-2004, 04:25 PM   #4
KneeLess
Member
 
Registered: May 2003
Distribution: Debian GNU/Linux 3.0 Sid, OpenBSD 3.5
Posts: 190

Rep: Reputation: 30
Okay, whatever.

One thing I forgot is if you're using the one with calloc() make sure to free() your pointer!
 
Old 06-14-2004, 06:16 PM   #5
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
you should allocate 9 chars if you're going to use an index like [8]. If you only allocate 8, it may segfault for some compilers, or overwrite stuff you don't want to overwrite etc.
 
Old 06-15-2004, 11:04 AM   #6
blanks
Member
 
Registered: Jun 2004
Location: West Virginia
Distribution: Gentoo, Debian
Posts: 52

Rep: Reputation: 15
Quote:
btw it doesnt compile as c because of hte for (int i=0..... so i have to use c++ to compile it so its c++ in my opinion.
just declare a variable, i, in that function, as in:
Code:
char* newpwd(char* oldpwd)
{
	char newpw[8]; 
        int i;   /**********here********/

	for (i=0; i<8; i++)
		newpw[i] = (char) (97+rand()%26);
	newpw[8]='\0';
	strcpy(oldpwd, newpw);
	
	return oldpwd;
}
now you have straight C and can compile with a C compiler.
 
Old 06-15-2004, 11:11 AM   #7
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
i know, but why bother?
 
Old 06-15-2004, 11:50 AM   #8
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Quote:
i know, but why bother?
portability. There are more C compilers out there than c++ compilers. Once upon a time the gnu c++ compiler generated slower code, even when invoked on valid C -- I don't know if that's still true. Still, if you are going to require a c++ compiler you might was well make use of the stl <string> class or at least switch to operator "new" and "delete" instead of malloc/calloc/free. If your code gets linked to other peoples' projects, it's doubly important for it to not be schizophenic about it's c or c++ness, as they link differently.
 
Old 06-15-2004, 12:43 PM   #9
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
im not gonna distribute it - who needs it - it only took me more than 2 mins to make cos of the seg fault.
 
Old 06-15-2004, 03:38 PM   #10
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
well sure, but I'm guessing part of the reason for writing it was to help move on to bigger and better things : )
 
Old 06-16-2004, 03:06 PM   #11
drigz
Member
 
Registered: Apr 2004
Distribution: Gentoo ~x86
Posts: 407

Original Poster
Rep: Reputation: 30
nope. i just needed a way to get a new password which i liked.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what does Segmentation Fault mean ? baronlynx Linux - Newbie 10 10-25-2009 04:32 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Help !!! Segmentation fault mola Linux - Software 3 06-23-2005 11:13 AM
Segmentation fault tejas15_10 Programming 9 06-20-2005 09:12 AM
Segmentation fault santhosh_o Programming 3 10-26-2004 05:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration