Menu

Sementation Error

Alex
2008-11-27
2012-09-26
  • Alex

    Alex - 2008-11-27

    Hey Everyone hows it going?
    I am using Dev-c++4.9.9.2 in conjunction with Windows Vista, home premium

    I am fairly new to programming, only in first part of an engineering course, just been playing with strings,
    every time I try to compile the below program i end up with a 'Segmentation Error'

    include <windows.h>

    include <stdio.h>

    include <stdlib.h>

    include <string.h>

    int main()
    {
    char name[20], course[25];
    int year;

    printf("Before we get started what is your name?\n");
    gets(name);
    fflush(stdin);
    printf("Which year of UWE are you in?\n");
    scanf("%d,&year");
    fflush(stdin);
    printf("And which course are you on?\n");
    gets(course);
    fflush(stdin);
    printf("Just to confirm your name is %s, your in year %d of studying %s\n",strlen(name), year, strlen(course));
    system("PAUSE");
    }

    When I debug the run the program via cursor to the end, the mishap seems to occur on the 3rd to last line,
    though i can't see why?

    I would post the error log, but there are apparently 5000 :P which all occur in the last 3 lines :P
    any pointers would be apreciated

    cheers

    Alex

     
    • Wayne Keen

      Wayne Keen - 2008-11-28

      "I used strlen as that was what the lecturer suggested"

      Programming is not, despite what some might think, about stringing magic words together to get a result. When you really engage your brain, you see that just using strlen because some said so makes no sesnse at all

      What is the strlen function? (If you don't know, you can easily google and find what it is) It is a function that returns the length of a string. This is a number that will be an integer.

      The length of a string is not the same as the string itself, anymore than saying your height is the same as you.

      Turn off the "magic word" processor, and turn on your brain. You can do it, its not magic.

      And don't be afraid to try things. I suggested that you did not want to use strlen - so why didn't you just try that? What's the worst that could happen? You get an error message?

      There is a reason little kids are better aat learning puters than old farts like me - they try stuff without fear.

      Wayne

       
    • Wayne Keen

      Wayne Keen - 2008-11-28

      Post from the start of the error log through the first 5 or so errors please.

      You mention you are operating under Vista. Did you take the steps neccessary to make Dev work under Vista?

      Wayne

       
    • Wayne Keen

      Wayne Keen - 2008-11-28

      Take a look at that last printf - why are you using strlen on the strings? What are you not simply printing the strings themselves?

      Wayne

       
    • Alex

      Alex - 2008-11-28

      I did take the steps to make it work on vista, i looked at the forum for that some time ago, have made quite a few working programs since then, this is my first atempt at useing strings though, I used strlen as that was what the lecturer suggested, if you know a better way please give an example.

      Cheers

      Below is the first section of the compile log:

      Compiler: Default compiler
      Executing gcc.exe...
      gcc.exe "C:\Users\Alex\Documents\Universtiy, UWE\Programming\Laptop Programing\Playing for test 2\Practise.c" -o "C:\Users\Alex\Documents\Universtiy, UWE\Programming\Laptop Programing\Playing for test 2\Practise.exe" -ansi -traditional-cpp -g3 -fmessage-length=0 -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3 -fmessage-length=0
      In file included from C:/Dev-Cpp/include/windows.h:48,
      from C:\Users\Alex\Documents\Universtiy, UWE\Programming\Laptop Programing\Playing for test 2\Practise.c:1:
      C:/Dev-Cpp/include/windef.h:263: error: syntax error at '##' token
      C:/Dev-Cpp/include/windef.h:263: error: syntax error before '{' token
      C:/Dev-Cpp/include/windef.h:264: error: syntax error at '##' token
      C:/Dev-Cpp/include/windef.h:264: error: syntax error before '{' token
      C:/Dev-Cpp/include/windef.h:265: error: syntax error at '##' token

       
    • cpns

      cpns - 2008-11-28

      If you have errors in the log, the code will not compile, so you could not run it, and could not get a seg fault. There is something wrong in your story somewhere!

      The format specifier %s requires a char* argument but strlen() return a size_t (an integer type) value. That is what is causing the problem. Because variadic arguments are of undefined type, the compiler cannot determine if you have done anything wrong. However GCC does have a facility to check format specifiers against arguments for standard library functions. You should add the following compiler options -Wall -Wformat -Werror then the compiler will catch this error.

      Other issues:

      gets() should always be avoided, it is prone to buffer overrun when the user enters more characters that you have allowed for in the buffer, and is a common software flaw used as a malicious exploit. You should never use this function. Instead of:

      gets(name);

      use:

      fgets( name, stdin, sizeof(name) ) ;

      Note however that unlike gets(), fgets() included the newline character at the end of the string.

      The ISO standard only defines fflush() for output streams, calling fflush(stdin) is non portable. Avoid it. Also if you do use it, know why you are doing it. In two of the three places you have done it it is in fact not necessary gets() and fgets() remove all characters from the buffer up to and including the newline.

      After the scanf() replace fflush() with:

      while( getchar() != '\n' ) ;

      Clifford

       
    • cpns

      cpns - 2008-11-28

      > I used strlen as that was what the lecturer suggested,
      > if you know a better way please give an example.

      You are attempting to print the string, not the string length. You have obviously misunderstood your lecturer, and not really thought about what you are doing (or what people are saying).

       
    • cpns

      cpns - 2008-11-28

      Read the "PLEASE READ BEFORE POSTING A QUESTION" on this forum. It is full of useful information about common problems, including the bit about not using spaces in project paths! such as "Universtiy, UWE\Programming\Laptop Programing\Playing for test 2\&quot;.

      How is it in Bristol today?

      > I did take the steps to make it work on vista,

      Are you certain? When you do that the log should show the full path to gcc.exe (I think).

      Do not fiddle with command line/project dialog options when you don't know what they are for. The "error: syntax error at '##' token" message is caused by having the -traditional-cpp option set. Why woyld you want that? The windows header files rely upon modern C-Preprocessor features that are disabled when using -traditional-cpp. Here cpp stands for C-Preprocessor, not C++ and you may have perhaps thought.

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.