Thread: Spooky segmentation fault

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    2

    Unhappy Spooky segmentation fault

    Hi all,

    This is the code that was giving me trouble. It terminates during the first for loop for reasons that I don't understand. Debugging with gdb revealed a segmentation fault.

    Code:
    #include <stdio.h>
    
    int main() {
    
        int *arr,size,sum=0;
    
        printf("Enter the size of your array: ");
        scanf("%i",&size);
    
        for(int i=0;i<size;i++) {
            printf("Element %i: ",i+1);
            scanf("%i",arr+i);
        }
    
        for(int i=0;i<size;i++) {
            sum = sum + *(arr+i);
        }
    
        printf("The total size is %i",sum);
    
        return 0;
    }
    I was able to fix the segmentation fault by moving the sum calculation to a separate function. But I'd like to know why the first code block is failing. Does anybody have any insight? Thanks in advance.

    Fixed code:

    Code:
    #include <stdio.h>
    
    int main() {
    
        int *arr,size;
    
        printf("Enter the size of your array: ");
        scanf("%i",&size);
    
        for(int i=0;i<size;i++) {
            printf("Element %i: ",i+1);
            scanf("%i",arr+i);
        }
    
        printf("The total size is %i",sum(arr,size));
    
        return 0;
    }
    
    int sum(int *pt,int size) {
        int sum = 0;
    
        for(int i=0;i<size;i++) {
            sum = sum + *(pt+i);
        }
        return sum;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot to allocate space for the dynamic array, say by calling malloc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2020
    Posts
    2
    Quote Originally Posted by laserlight View Post
    You forgot to allocate space for the dynamic array, say by calling malloc.
    That worked. Thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  2. Segmentation fault
    By phoneix_hallows in forum C Programming
    Replies: 8
    Last Post: 08-27-2009, 05:56 AM
  3. spooky
    By Fountain in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 08-25-2003, 05:50 PM
  4. spooky
    By Fountain in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-12-2003, 09:39 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread