Like Tree2Likes
  • 1 Post By CommonTater
  • 1 Post By manasij7479

Newbie questions - Segmentation fault? - programming on a Mac

This is a discussion on Newbie questions - Segmentation fault? - programming on a Mac within the C Programming forums, part of the General Programming Boards category; Hi everyone! This should be simple for mostly everyone here, but I'm just starting out my learning curve of C ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Lisbon, Portugal
    Posts
    8

    Newbie questions - Segmentation fault? - programming on a Mac

    Hi everyone!

    This should be simple for mostly everyone here, but I'm just starting out my learning curve of C programming.
    I'm using an editor called "GNU nano 2.0.6" that I open through a Terminal shell in MacOS X (Lion). I was planning on using XCode from Apple, but it's a bit overwhelming. I mainly want to concentrate on the programming part right now (leave the bells and whistles for later).

    I've been compiling the following simple code with g++ and gcc, and I get two different types of errors.

    Code:
    #include <stdio.h>
    
      main() {
          
        // Program that prints a random number between 1 and 10 on the screen
          
        int iRandomNum = 0; 
        srand(time());
        
        iRandomNum = (rand() % 10) + 1;
    
        printf("This is a random number between 1 and 10: %d", iRandomNum);
    
      } // end main()
    When I try to compile this via g++, I get the following messages:

    8randomnumbers.c:9: error: ‘time’ was not declared in this scope
    8randomnumbers.c:9: error: ‘srand’ was not declared in this scope
    8randomnumbers.c:11: error: ‘rand’ was not declared in this scope
    When I use gcc, apparently it compiles, but then when I run the program I get a "Segmentation fault" message.
    From what I read in this forum, the "segmentation fault" has something to do with memory corruption, but I'm still way in the beginning to realize how to solve that...

    Could someone please just point me to the right direction on what I need to be doing?

    This code was mostly copied and pasted from the book I'm reading to learn (C Programming for the absolute beginner, by Michael Vine, page 76).
    Should I maybe get a new compiller? Get a new book?
    Any suggestions? I feel a bit alone using Mac to program (everybody keeps telling me Windows and Linux is better for that, but you use what you have).

    Thanks! I'm looking forward to be an active member of this forum for some time to come.

    Toscano

  2. #2
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    If you are writing C code, then my suggestion is to compile using gcc. A few tips:

    -Since you have a bash shell, when you don't know how a function works, use manual (eg. type "man srand")
    -Segmentation fault tells you that your program accessed some invalid location in memory. To track the code line which generates the fault compile with the -g option, run the program under gdb ("gdb a.out" and then "run") and print the stacktrace ("bt").

    In your case I would look at the manual for the time() function ("man time.h"), I believe it should get a parameter... try NULL (you will have to include stdlib.h).
    Goog luck!

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Lisbon, Portugal
    Posts
    8
    Hi flexo87.

    Thanks for your help.
    You were right. Inserting "NULL" as a parameter for time() solved it! (and also inserting the <stdlib.h> . Weird as why this isn't mentioned in the book... maybe it's time to browse the recommended readings section of this forum and get a new one...)

    Anyway, what's apparent to me right now is that I'm lost on the basic Unix syntax and architecture. I'll have to figure out some basic commands, know where things are via command line, move and copy some stuff around, etc. Otherwise I'll keep getting stuck with the most basic stuff that doesn't even have anything to do with the C programming itself.

    Any good reading (online or in a book form) that you recommend for someone who never used Unix before? (bear with me... just started out last Saturday...).

    Thanks!

    Toscano

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Toscano View Post
    Anyway, what's apparent to me right now is that I'm lost on the basic Unix syntax and architecture. I'll have to figure out some basic commands, know where things are via command line, move and copy some stuff around, etc. Otherwise I'll keep getting stuck with the most basic stuff that doesn't even have anything to do with the C programming itself.
    The problem was that you didn't have #include <time.h> and #include <stdlib.h> at the top of your source file.

    So from this I gather you're trying to figure out how to know when to include something or what libraries are needed...

    The answer to this is: Learn how to look stuff up!

    For example... I just popped open the Pelles C (on Windows) help file and double checked which header NULL was defined in...

    When programming, I do this *constantly*, sometimes hundreds of times a day. The C Standard Library and Windows (or Mac) API are so horrifically big that nobody can hope to memorize even a small percentage of it.... leaving us with no choice but to be constantly looking stuff up in help files, online references, wherever we have to go to get good information...

    For a certainty, programming is no place for someone shy about using help files or asking for help.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Lisbon, Portugal
    Posts
    8
    Quote Originally Posted by CommonTater View Post
    The answer to this is: Learn how to look stuff up!

    For example... I just popped open the Pelles C (on Windows) help file and double checked which header NULL was defined in...

    When programming, I do this *constantly*, sometimes hundreds of times a day. The C Standard Library and Windows (or Mac) API are so horrifically big that nobody can hope to memorize even a small percentage of it.... leaving us with no choice but to be constantly looking stuff up in help files, online references, wherever we have to go to get good information...

    For a certainty, programming is no place for someone shy about using help files or asking for help.
    Hi Tater.

    That's good advice, thanks!
    Of course, I'm not shy about asking for help. That's why I got here in the first place.
    I'm just puzzled as to why a book called "C programming for absolute beginners" includes incomplete code... that's another life lesson right there: never trust a single source of information. I'm already browsing through the C tutorials on this website to get a different perspective.

    As for looking up help files, I'm all for it, I just need to learn how to look up that stuff first in my Mac/Terminal shell. In other words, I need to learn how to speak before I ask for a grammar book.

    Thanks again.

    Toscano

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by Toscano View Post
    Any good reading (online or in a book form) that you recommend for someone who never used Unix before?
    I'd say... don't think about that now....as most of those books tend to be somewhat hardcore...(even a Dummies one I looked at !)
    You'd totally confuse C with bash scripting and the resulting mess would take years to be cleaned from your brain.

    Not that you shouldn't learn anything... but only read up on what you'd actually need at this point...
    Search for common commands or something like that...and you'll be fine for now.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Lisbon, Portugal
    Posts
    8
    Quote Originally Posted by manasij7479 View Post
    I'd say... don't think about that now....as most of those books tend to be somewhat hardcore...(even a Dummies one I looked at !)
    You'd totally confuse C with bash scripting and the resulting mess would take years to be cleaned from your brain.

    Not that you shouldn't learn anything... but only read up on what you'd actually need at this point...
    Search for common commands or something like that...and you'll be fine for now.
    Thanks, Manasij.

    Really, what I need right now is to learn a few basic commands so that when people tell me stuff like "compile with the -g option" or "run the program with gdb" or even "look for the help file", at least then I know what to do. Just the basic Unix syntax, as I've used graphical interfaces all my life and never command lines.

    No books then. Just a Google search for "common basic Unix commands" and we'll see what happens.
    Thanks.

    Toscano

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Toscano View Post
    Hi Tater.

    That's good advice, thanks!
    Of course, I'm not shy about asking for help. That's why I got here in the first place.
    I'm just puzzled as to why a book called "C programming for absolute beginners" includes incomplete code... that's another life lesson right there: never trust a single source of information. I'm already browsing through the C tutorials on this website to get a different perspective.

    As for looking up help files, I'm all for it, I just need to learn how to look up that stuff first in my Mac/Terminal shell. In other words, I need to learn how to speak before I ask for a grammar book.

    Thanks again.

    Toscano
    Something to keep in mind is that these books are written by people, edited by people, transcribed by people and then studied by people. Everyone of them is going to have errors and omissions. It speaks well of you that you are spotting them and getting the examples and quizzes to work inspite of them.

    The best way to learn C (or anything else where precise understanding is required) is to take the traning manual (of choice), start on page 1, read every word (skimming does not work), try the examples, do the exercises, mess with the code, break it, fix it, understand how it works, then turn to page 2... repeating the process until you completely finish the book.

    Programming languages like C are relatively simple, a couple of dozen keywords and some gramatical rules, most people can do that in a day... Programming itself is hard; the examples and exercises in these books generally take you through the concepts in a nicely progressive manner. Jumping around or not completing the book, might well give you the grammar (called syntax in programming) but that does not give you basic programming concepts that will be crucial to your future in programming.

    Once again, that tired old line of mine... Nobody has ever solved a problem they don't understand.
    Toscano likes this.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    Lisbon, Portugal
    Posts
    8
    Quote Originally Posted by CommonTater View Post
    Once again, that tired old line of mine... Nobody has ever solved a problem they don't understand.
    You got that right!
    I'm new to programming, but if there's one thing I've learned throughout my long life (well... maybe 36 is not that long...) is that if you don't understand the premise of the problem, forget about finding the solution.

    Best regards.
    Toscano

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by Toscano View Post
    ....is that if you don't understand the premise of the problem, forget about finding the solution.
    Unless you're a politician.
    Toscano likes this.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    Registered User
    Join Date
    Nov 2011
    Location
    Lisbon, Portugal
    Posts
    8
    Quote Originally Posted by manasij7479 View Post
    Unless you're a politician.
    Well... yeah! Which is why politicians are generally very bad computer programmers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to programming, Segmentation Fault?
    By dc210 in forum C Programming
    Replies: 3
    Last Post: 09-14-2010, 01:05 AM
  2. Replies: 4
    Last Post: 04-20-2010, 11:55 PM
  3. Segmentation Fault
    By SterlingM in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2010, 10:27 AM
  4. C Programming question (segmentation fault)
    By trustno1fox in forum C Programming
    Replies: 2
    Last Post: 05-01-2009, 05:55 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

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