Segmentation Fault Again

This is a discussion on Segmentation Fault Again within the C Programming forums, part of the General Programming Boards category; I dont see anything different, if anything this code is easier than it was Code: char str1[1000] = ""; char ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    21

    Segmentation Fault Again

    I dont see anything different, if anything this code is easier than it was

    Code:
      	char str1[1000] = "";
      	char str2[1] = "^";
    	char str4[1001] = "";
    
            	printf("Enter : ");
    		
    	fgets (str1, sizeof str1, stdin);
    
      	strcpy(str4,str1);
    	strcat(str4,str2);		
    	
    
                     FILE *fp = fopen ("news.dat", "a"); 
     			
    	fprintf(fp,"%s",str4);                    
    
    	fclose(fp);

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > char str2[1] = "^";
    This is not a 'C' string - it has no \0 terminating it.

    As such, your code just roams through memory until it either crashes or finds a \0
    Either way, it's broken

    Code:
    char str2[] = "^";
    This has a \0, and it saves all that tedious counting

    This is even better, the run-time overhead is much less since the string itself isn't copied into an array.
    Code:
    char *str2 = "^";
    The down side is, you can't modity such strings with say str2[0] = '!';

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    21
    Still has Segmentation Fault even with changes you have suggested. I had this problem and fixed it by entering the sizes of my strings i've made some changes and its back, thing is this code does work, its writes to the file just what i want, but then Segmentation Fault and the program closes

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    1) Where does it seg fault?
    2) Check to make sure fopen() didn't return NULL

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Posting an actual complete program as well would help - random snippets of where you think the problem is doesn't work.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >thing is this code does work, its writes to the file just what i want,
    >but then Segmentation Fault and the program closes
    You have a slightly skewed understanding of working code. You're also not off to a good start here. We make it very clear what we need to help you solve your problem, but you seem to be fighting us the entire way. In the end, nobody will want to help you because it's just not worth the effort.
    My best code is written with the delete key.

  7. #7
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    I concur with Prelude's statement.

    Though, I have had run-ins with Segmentation Fault's and Stack Overflow's many times. With only a glimpse of your code, here is my advice.

    Look for any pointers that may be un-initialized, modified as a literal or constant, de-referenced while a NULL value, or accessing memory that isnít yours.

    Things that segmentation faults/violations are typically out-of-bounds array references and/or references through un-initialized or mangled pointers. Look very closely in your program for bizarre things like that.

    There are a number of methods for finding out where the program went out of bounds. One method is to use printf() statements to determine how far the program is getting before it crashes, and to print out the contents of interesting variables.

    All in all, this comes down to where it generally means that your program tried to access memory it shouldn't have, invariably as a result of stack corruption or improper pointer use.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

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