Thread: why DOS prompt crashes when I run this compiled code?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    why DOS prompt crashes when I run this compiled code?

    Hi

    The computer I'm using has Win Visa installed. I'm using Code::Blocks and run compiled from within the Code::Blocks.

    CODE 1 compiles and runs fine. CODE 2 compiles but when I try to run it the command prompt (DOS window) crashes without even running the compiled code. I have even tried Dev-C++ the same problem is encountered. At the bottom you can find the problem details shown by the crashed DOS window and also a linked screenshot of crashed window. Please help me to solve this issue.

    CODE 1
    Code:
    // calculating area of a circle using user-defined function
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    float area(float dummy);
    
    int main()
    
    {
        float r; float a;
    
    	cout << "enter radius: "; cin >> r;
    
    	a = area(r);
    
    	cout << a << endl;
    
    	system("pause");
    
    	return 0;
    }
    
    //------------------------------------
    // area(int), function definition
    
            float area(float dummy)
    
    		{
    			float Area;
    
    			Area = 3.1416*(dummy*dummy);
    
    			return Area;
    
    		}
    //--------------------------------------

    CODE 2
    Code:
    // average_weight.cpp
    // read 10 or n weights and then find average
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
    
            int n;
            float w[n];
            float total_weight=0;
    
            cout << "how many weights: "; cin >> n;
    
            for (int i=0; i<n; i++)
            {
                    cout << "enter weight #" << (i+1) << ": "; cin >> w[i];
            }
    
            cout << endl;
    
            for (int i=0; i<n; i++)
            {
                    cout << "entered weight #" << (i+1) << " is: " << w[i] << endl;
            }
    
            cout << endl;
    
    
            for (int i=0; i<n; i++)
            {
                    total_weight += w[i];
            }
    
            float average_weight = total_weight/n;
    
            cout << "average weight is: " << average_weight << endl;
    
            system("pause");
            return 0;
    
    }

    Problem details given by the crashed window:
    Code:
    Problem signature:
      Problem Event Name:	APPCRASH
      Application Name:	average_weight.exe
      Application Version:	0.0.0.0
      Application Timestamp:	4de7ebf8
      Fault Module Name:	average_weight.exe
      Fault Module Version:	0.0.0.0
      Fault Module Timestamp:	4de7ebf8
      Exception Code:	c00000fd
      Exception Offset:	000140ce
      OS Version:	6.0.6001.2.1.0.768.11
      Locale ID:	1033
      Additional Information 1:	6199
      Additional Information 2:	bcfd5cce14b739ebe66a3b520492755c
      Additional Information 3:	973d
      Additional Information 4:	a6b61f04fc8b13590bb3150313d0cc3d
    
    Read our privacy statement:
      http://go.microsoft.com/fwlink/?linkid=50163&clcid=0x0409

    Link:
    http://img855.imageshack.us/img855/4...shedwindow.jpg
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    int n;
    float w[n];
    No no, a thousand times no. You cannot use a variable as the length of a variable-sized array if you don't know what the value of that variable is. Well, you can, as you see, but it is a Very Very Very Bad Thing.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Actually, as you know, it's not standard C++.
    So there are two problems:
    - It's not standard C++. It's a GNU extension. You should not use it.
    - n's value at this point is undefined, and seeing as C++ is a "serial" language, all instructions are executed in turn, meaning that whatever value you hope to put into n later will not affect the creation of the array which happens at an earlier point in time.
    What you need to do is change
    float w[n] to
    std::vector<float> w;
    And instead of w[i] = something, do w.push_back(something).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    First and foremost use meaningful variable names in your programs, not a,w, etc...

    Secondly, it's bad practice to be calling system functions like that and is potentially a security risk. Search the board, i know Prelude explained it pretty well one time. Saying that, here is what's going on:

    what you want: What you're trying to do is create a variable length array and the way you've gone about that won't work.

    what's happening: Basically when your compiler parses your code and is going to compile it. it needs to know how big to make the w array. Since n has no value, as Elysia says it will result in undefined behaviour, most likely assigning garbege to the variable n. in which case then your trying to use memory you haven't allocated. this is why your program crashes.

    what you want to do: is get the value of n from the end user - at your own risk - and then make the array that size. so something like:
    Code:
    int n;
          
            float total_weight=0;
    
            cout << "how many weights: "; cin >> n;
            float w[n];
    caveat: I'm a C programmer and C99 allows variable length arrays so I'm assuming it should be allowed in C++ since it is a superset of C

    here is how it ran on my system using DevC++:
    Code:
    how many weights: 5
    enter weight #1: 1
    enter weight #2: 2
    enter weight #3: 3
    enter weight #4: 4
    enter weight #5: 5
    
    entered weight #1 is: 1
    entered weight #2 is: 2
    entered weight #3 is: 3
    entered weight #4 is: 4
    entered weight #5 is: 5
    
    average weight is: 3
    Press any key to continue . . .
    your other option - and the accepted way - is to make n a constant prior to calling main() in your code so:
    Code:
    define n 45
    
    .....
    int main(void){
    ...
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm pretty sure the answer is "If we're doing C++, then just use std::vector already."

  6. #6
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by tabstop View Post
    I'm pretty sure the answer is "If we're doing C++, then just use std::vector already."
    lol, I could be wrong but from the OP code, I doubt s/he is there yet. One core dump at a time
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you very much, tabstop, Elysia, caroundw5h. You guys are so nice.

    Best wishes
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by caroundw5h View Post
    what you want to do: is get the value of n from the end user - at your own risk - and then make the array that size. so something like:
    Code:
    int n;
          
            float total_weight=0;
    
            cout << "how many weights: "; cin >> n;
            float w[n];
    Like I explained already, this is not standard C++. This is a GNU extension. It is only standard in C99.
    The correct thing to do is use std::vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-12-2008, 12:10 AM
  2. Replies: 1
    Last Post: 01-11-2008, 09:34 AM
  3. Replies: 1
    Last Post: 03-01-2006, 03:07 AM
  4. C# Compiled to Machine Code
    By FwyWice in forum C# Programming
    Replies: 1
    Last Post: 12-06-2002, 04:22 AM