Thread: System.AccessViolationException

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    System.AccessViolationException

    This program crashes every time I run it. The debugger error is

    "An unhandled exception of type 'System.AccessViolationException' occurred in volumes.exe

    Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

    My program is just to find the volume of a cube if the user enters the string cube but I cannot get the if statement to start.
    Here is the program

    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    #include "string.h"

    int main()
    {
    int i;
    string shape, cube, box, cylinder;
    printf ("Please enter the shape:");
    shape=GetLine();
    i = strcmp( shape, cube );

    if (i==0)
    {
    double s, cvol;
    printf ("Enter side length");
    s=GetReal();
    cvol=s*s*s;
    printf ("The volme of a cube with side length= %lf is %lf\n", s, cvol);
    }
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    First of all, if you surround your code with [ code ] and [ /code ] (without space) it formats the text nicely - it also preserves your spacing.

    Code:
    string shape, cube, box, cylinder;
    printf ("Please enter the shape:");
    shape=GetLine();
    i = strcmp( shape, cube );
    Now, I'm betting that's where your problem is happening. Can you give us anymore details about how the string type is defined, and how GetLine() works? I'm betting you're assigning values to a pointer when you haven't allocated space for it.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    I am actually very novice when it comes to strings and just did this program for practice and better understanding. But I am still very unfamiliar with the string data type. I understand that the GetLine function comes from the simpio library and works similarly to scanf. It waits for the user to enter a value and assigns that to the variable. Sorry for my lack of understanding.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    #include <stdio.h>
    stdio is part of the standard C library - so everybody knows what this files is, and how to use the functions inside it.

    #include "genlib.h"
    #include "simpio.h"
    When the file is in quotes, it means that instead of using some precompiled headers the compiler already knows about (like the standard C library), that these files are in the same directory as your source files. Since they're not standard, almost nobody has these files, or knows much about them.

    #include "string.h"
    There's a standard header file named string.h, but you must have some other one. "string" is not a standard data type in C (although it has a few functions for working with arrays of chars). I'm betting the problem is in the way you're using one of the functions in this library. However, without any other details, it's impossible to tell.

    If you're doing this in a class, is there some library you've been given to work with? What compiler are you using (some compilers come with non-standard libraries)?

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    I am using Microsoft Visual C++ Express 2008
    The genlib and simpio libraries are the extended libraries from microsoft

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Since this is C++, it should be in the C++ forum, otherwise, if it's supposed to be C, your source code should end with .c not .cpp.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I am using Microsoft Visual C++ Express 2008
    The genlib and simpio libraries are the extended libraries from microsoft
    I can't seem to find any official looking documentation from Microsoft about these libraries, so I'm afraid I can't help much. Perhaps someone else here who uses MSVC can shed some light on the situation. Do you have any documentation on those libraries? How did you learn how to use them?

    Since this is C++, it should be in the C++ forum, otherwise, if it's supposed to be C, your source code should end with .c not .cpp.
    Indeed - though the code looks like C. kbikkasani - was this intended to be C or C++? They used to look a lot more like eachother a few years ago...

Popular pages Recent additions subscribe to a feed

Tags for this Thread