Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Please help debug this C declaration [C]

  1. #1
    Join Date
    Apr 2007
    Beans
    Hidden!

    Please help debug this C declaration [C]

    I have tried playing with some Project Euler problem (decided to do it in C), but was stopped in the beginning by the rather strange Segmentation Fault:


    Code:
    #include <stdio.h>
    
    main()
    {
        unsigned long int searched_prime, max, i, j;
        max = 1000000000L;
        unsigned long int nums[max];
        printf("%s", "success");
    }
    Can you please help me debug why this prints Segmentation Fault? Is there an error with the declarations? I have ran Valgrind on it, ran it through gdb, but wasn't able to find my error. I have tried compiling it with:
    gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)

  2. #2
    Join Date
    May 2010
    Location
    uk
    Beans
    9,249
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Please help debug this C declaration [C]

    Hi

    Code:
    max = 1000000000L;     unsigned long int nums[max];
    Try creating your array on the heap, using malloc or some such, and then consider what you have done to your stack

    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

    Thinking about becoming an Ubuntu Member?

  3. #3
    Join Date
    Apr 2007
    Beans
    Hidden!

    Re: Please help debug this C declaration [C]

    Thanks. I haven't used C so far, and it's a humbling experience after using other languages. Thanks for your help.
    Last edited by cdiem; June 4th, 2011 at 07:17 PM.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Beans
    2,185
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Please help debug this C declaration [C]

    I hope you realize how much memory this is going to take: you want to allocate a billion unsigned long values. An unsigned long is usually 64 bits (8 bytes) so that statement will allocate 8 GB memory. Do you have that much memory in your computer?

    On a 32-bit system this will certainly fail (because a process is limited to a 4 GB address space), and even on a 64-bit system it is likely to fail unless you have a LOT of RAM (even if you have more than 8 GB RAM it might fail because there isn't a consecutive block of 8 GB free RAM).
    Ubuntu 12.04

  5. #5
    Join Date
    May 2010
    Location
    uk
    Beans
    9,249
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Please help debug this C declaration [C]

    Hi

    I hope you realize how much memory this is going to take


    I'm hoping the OP figured that out

    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

    Thinking about becoming an Ubuntu Member?

  6. #6
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: Please help debug this C declaration [C]

    Ooh, Euler. Not done one of those for ages. I've done quite a few in C and had the odd foray deep into my swap partition.

    Which problem is it?

  7. #7
    Join Date
    Apr 2007
    Beans
    Hidden!

    Re: Please help debug this C declaration [C]

    Quote Originally Posted by r-senior View Post
    Ooh, Euler. Not done one of those for ages. I've done quite a few in C and had the odd foray deep into my swap partition.

    Which problem is it?
    It's the problem 7; my first try was an Euler sieve on a big array, for learning C better (hence the topic). My code was 30 lines long, but also nonworking I'll try to rework it to use less memory. Thanks!
    Last edited by cdiem; June 5th, 2011 at 01:48 PM.

  8. #8
    Join Date
    May 2007
    Location
    East Yorkshire, England
    Beans
    Hidden!

    Re: Please help debug this C declaration [C]

    Why are you trying to allocate such a gigantic array?
    Website | Blog | The Arch Hurd Project

    If you want to ask about something I posted, send a PM, as I don't watch many threads

  9. #9
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: Please help debug this C declaration [C]

    An Erasthones Sieve?

    Why don't you try working your way up - find the 101st prime, then the 1001st prime and so on? You'll get a feel for whether you need (a) such a large sieve and (b) whether you need long integers.

    It's a good learning exercise doing Euler in C. When you look at the solutions other people have done, especially in Python and J (which is a "program" of just 7 characters for this problem ) , you'll feel that you've had to work hard but it's good learning.

    It's worth putting your prime number functions into a library and looking at ways to tune them. You'll use them a lot. You'll learn how big a sieve is worthwhile and how to go beyond the sieve. Functions to test whether a number is prime, generate sequences of primes, etc.
    Last edited by r-senior; June 5th, 2011 at 02:05 PM. Reason: just punctuation

  10. #10
    Join Date
    Apr 2007
    Beans
    Hidden!

    Re: Please help debug this C declaration [C]

    Quote Originally Posted by Barrucadu View Post
    Why are you trying to allocate such a gigantic array?
    I was trying to follow the algorithm as said in Wikipedia:
    1. Create a list of consecutive integers from two to n: (2, 3, 4, ..., n)
    2. ...

    But I set n too high ;]

    Why don't you try working your way up - find the 101st prime, then the 1001st prime and so on? You'll get a feel for whether you need (a) such a large sieve and (b) whether you need long integers.

    It's a good learning exercise doing Euler in C. When you look at the solutions other people have done, especially in Python and J (which is a "program" of just 7 characters for this problem ) , you'll feel that you've had to work hard but it's good learning.

    It's worth putting your prime number functions into a library and looking at ways to tune them. You'll use them a lot. You'll learn how big a sieve is worthwhile and how to go beyond the sieve. Functions to test whether a number is prime, generate sequences of primes, etc.
    Awesome! Thanks very much!

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •