seg fault Need help

This is a discussion on seg fault Need help within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main(void) { char* ch; int max = 1000; int i = 0; /* read character by ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    seg fault Need help

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char* ch;
      int max = 1000;
      int i = 0;
      /* read character by character from stdin */
    
    
        while (fgets(ch, max, stdin) != NULL){
    
          printf("%s ",ch);
          i++;
        }
      return 0;
    }
    THIS is my program and when i do
    program < README

    Code:
    XXXXXXX XXXXXX
    XXXXXX XXXXX
    CS101 
    Assignment 1
    
    List.java
    This program is to manipulate a LIST ADT giving it multiple functions 
    either ways to adjust a list by adding nodes or deleting them. It will
    change pointers like make current go to the top of the list or bottom or
    even move it to go a space up or down. List also contain boolean functions
    like it would check to see if the list is empty or where current is. The
    main purpose of List.java is to create a list and make it a certain way 
    for user to make.
    
    ListTest.java 
    This was just multiple function calls to make it work or see if 
    List.java actually works many test trials over each function.
    
    Shuffle.java
    This program is to manipulate a list to do Permutaion alignment like 
    giving a list of permutation, then create a List going for n to n-1. 
    The Permutation list will then adjust teh new list to arrange the certain
    numbers to the order of the permutation list. This is the main function 
    that calls on List.java.
    I get a seg fault at the very very end wit the line
    that calls on List.java why is that happening

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    57
    It gives segmentation fault because you are not allocating the memory for the string.
    Try this code.
    Code:
    #include <stdio.h>
    #include<malloc.h>
    
    int main(void)
    {
      char* ch;
      int max = 1000;
      int i = 0;
      /* read character by character from stdin */
    
    
      ch=(char *)malloc(max);
        while (fgets(ch, max, stdin) != NULL){
    
          printf("%s ",ch);
          i++;
        }
        free(ch);
      return 0;
    }

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    "ch" doesn't point to anything meaningful, it's initialised with whatever.

    Perhaps you mean:
    Code:
    char buffer[1000];
    
    while(fgets(buffer, sizeof buffer, stdin) != NULL)
    {
       ...
    ?

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: seg fault Need help

    Normally, If the pointer points something no need to allocate the memory. It will take the memory from which it is points.
    Ex:
    Code:
    char arr[max];
    char *ch;
    ch=arr; // It points the memory from arr.
    But If you are using pointer alone, that is it needs to point the new memory not the some other memory. So in this case you need to allocate the memory.

    Code:
    char *ch;
    ch = (char *)malloc(max);// It points new memory that is return by malloc.
    Segmentation fault error will come when the pointer tries to point illegal or non-allocated or not-allowed or read-only location.
    Last edited by sganesh; 03-02-2010 at 10:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf seg fault
    By mercuryfrost in forum C Programming
    Replies: 19
    Last Post: 08-20-2009, 01:22 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM

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