Core Dump / Segmentation Fault

This is a discussion on Core Dump / Segmentation Fault within the Linux Programming forums, part of the Platform Specific Boards category; Ok, this code creates a segmentation falult where the comments indicate, i've tried everything to fix it... BUt i can't, ...

  1. #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 Attached Files
    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
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    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, 12:33 PM
  4. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 02:24 AM
  5. segmentation core dump - need help
    By knight101 in forum C++ Programming
    Replies: 1
    Last Post: 11-26-2001, 03:43 PM

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