Thread: Core Dump / Segmentation Fault

  1. #1

    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
    Last edited by Inquirer; 04-08-2003 at 05:36 PM.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  2. #2
    When i do that, i get these:
    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

  3. #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)

Popular pages Recent additions subscribe to a feed

Similar Threads

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