segmentation fault

This is a discussion on segmentation fault within the C Programming forums, part of the General Programming Boards category; hi everyone . i am new to the fourm and i was trying to compile a file using gcc to ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    segmentation fault



    hi everyone . i am new to the fourm and i was trying to compile a file using gcc to catenate two string but keep getting a segmentation fault
    this is the source code
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
         int main()
    {
       int x;
       char *pring,*ding;
       printf("\n enter any string ");
       scanf("%s",pring);
       printf("\n enter another string");
       scanf("%s",ding);
      
       x=sizeof(pring)+sizeof(ding)+22;
       pring=realloc(pring,x);
       strcat(pring,ding);
     
       printf("\n%s",pring);
       return 0;
    }
    This is the message i get when i try to debug it with gdb :
    Program received signal SIGSEGV, Segmentation fault.
    0x00007ffff7aade97 in _IO_vfscanf () from /lib/libc.so.6


    can anyone help me out with this ??

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    You should find a good tutorial on malloc() and how it should be used. That'd explain away all the issues you're having here.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    char *pring,*ding;
    You haven't allocated memory for those char pointer, but still your trying read in a string. Wghcih is wrong. Try allocate memory before reading any value. For example

    Code:
    if( ( pring = malloc( 80 ) ) != NULL )
         scanf("%s", pring);
    ...
    ...
    free(pring);
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The problem is that you have not allocated space for pring and ding before reading data into them.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Safer should be

    Code:
    if( ( pring = malloc( 80 ) ) != NULL )
         if( 1==scanf("%79s", pring) ) puts("input was OK");
    ...
    ...
    free(pring);

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    in fact its better to use fgets instead of scanf function reading string avoid buffer overfollow and stdin standoff of '\n' char.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  2. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  4. Annoying Segmentation Fault
    By Zildjian in forum C++ Programming
    Replies: 7
    Last Post: 10-08-2004, 02:07 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