C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-08-2003, 05:30 PM   #1
Rebooted
 
Inquirer's Avatar
 
Join Date: Apr 2002
Posts: 281
Core Dump / Segmentation Fault

Ok, this code creates a segmentation falult where the comments indicate, i've tried everything to fix it... BUt i can't, and i have no idea hpow to fix it. I've seen things about allocating memory for this, but i'm not sure how this works.

Code:
int main(int argc, char *argv[]) {
	char   *sFileName,		// This is the name of the file to pass to gcc/g++
	       *sFileExt,		//   and its file extension
	       *sOutName,		// This is the name of the output file for gcc/g++ (-o)
...

	cout << "Makefile name: ";
	cout << "DEBUG:: Breakpoint 1" << endl;
	scanf("%s", &sOutName);				// THIS IS [EDIT] NOT [\EDIT] WHERE THE SEGMENTATION FAULT IS
	cout << "DEBUG:: Breakpoint 2" << endl;
	sOutName = substr(sOutName, 0, (strchr(sOutName, '.')-sOutName));	// strip all extensions // THIS IS WHERE THE SEG. FAULT IS
Attached Files
File Type: cpp epile.cpp (6.6 KB, 36 views)
__________________
Compilers:
GCC on Red Hat 8.1 (Primary)
GCC on Mac OS X 10.2.4 (Secondary)

Others:
MinGW on XP

Last edited by Inquirer; 04-08-2003 at 05:36 PM.
Inquirer is offline   Reply With Quote
Old 04-08-2003, 06:00 PM   #2
Rebooted
 
Inquirer's Avatar
 
Join Date: Apr 2002
Posts: 281
When i do that, i get these:
Quote:
epile.cpp: In function `int main(int, char **)':
epile.cpp:64: incompatible types in assignment of `char *' to `char[256]'
Code:
  59:           cout << "Makefile name: ";
  60:           cout << "DEBUG:: Breakpoint 1" << endl;
  61:           //scanf("%s", &sOutName);
  62:           fgets( sOutName, sizeof( sOutName ), stdin );
  63:           cout << "DEBUG:: Breakpoint 2" << endl;
  64:           sOutName = substr(sOutName, 0, (strchr(sOutName, '.')-sOutName));       // strip all extensions
__________________
Compilers:
GCC on Red Hat 8.1 (Primary)
GCC on Mac OS X 10.2.4 (Secondary)

Others:
MinGW on XP
Inquirer is offline   Reply With Quote
Old 04-08-2003, 08:24 PM   #3
Comment your source code!
 
Lynux-Penguin's Avatar
 
Join Date: Apr 2002
Posts: 533
Dont use pointers to strings!

If you're using C++ why not just write a string class
or steal one from the college boards or something

steal apstring
I wrote one for my system
CString (original isn't it?)

Automatic resizing etc. Checks for outofbounds indexing and bad pointers (but I added HUGE amounts of debug code to actually check the whole program, not just where CString is used)
Lynux-Penguin is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What am I doing worng? Segmentation fault, core dump cookie C Programming 4 06-08-2007 09:59 AM
Segmentation fault, core dumped dweenigma C Programming 2 05-21-2007 03:50 PM
Locating A Segmentation Fault Stack Overflow C Programming 12 12-14-2004 01:33 PM
Segmentation fault (core dumped) JYSN C Programming 1 02-21-2002 03:24 AM
segmentation core dump - need help knight101 C++ Programming 1 11-26-2001 04:43 PM


All times are GMT -6. The time now is 04:49 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22