Annoying crash..

This is a discussion on Annoying crash.. within the C++ Programming forums, part of the General Programming Boards category; I am wondering why does this program crash: Code: #include <stdlib.h> #include <iostream> using namespace std; int main(int argc,char **argv) ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163

    Annoying crash..

    I am wondering why does this program crash:

    Code:
    #include <stdlib.h>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc,char **argv)
    {
        int *num=NULL; //Array for storing numbers..
        int i=0;       //Counter
    
        cout<<"--Program for finding number average--\n"<<
              "------------Input 0 to stop-----------\n\n";
    
        while(1)
        {
            int n; //Dummy variable
    
            cout<<"Enter number "<<i+1<<": ";
            cin>>n;
    
            if(n==0) break;
    
            num=new int; //Allocate space for number
            num[i]=n;
            ++i;
        }
    
        int aver=0;
    
        for(int x=0;x<i;++x) aver+=num[x];
    
        cout<<"Average of inputed numbers is: "<<aver/i;
        cin.get();
    
        for(int x=0;x<i;++x) delete num;
    
        return 1;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Becuase you only allocate space for one number (many times, but still only one number at a time), and then use the pointer as if you have space for multiple numbers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    So that is not how C++ new works? I thought it can "detect" if it needs to malloc or realloc, or something.. Can you give me an example?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,915
    Quote Originally Posted by hauzer
    I thought it can "detect" if it needs to malloc or realloc, or something.
    new certainly cannot do that, and since you want a dynamically allocated array, you should be using new[] instead of new.

    But wait. The standard containers have just want you wanted, and in this case you can use std::vector, e.g.,
    Code:
    std::vector<int> numbers;
    while (1)
    {
        int n; //Dummy variable
    
        cout << "Enter number " << (numbers.size() + 1) << ": ";
        cin >> n;
    
        if (n == 0) break;
    
        numbers.push_back(n);
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    I see that vectors are useful but can you show me how can I do it with new? Also, I tried compiling with vectors but it seems that they are not a member of std.

    EDIT: I researched a bit and I see that vectors are commonly used. Thank you both.
    Last edited by hauzer; 10-27-2008 at 06:10 AM.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  6. #6
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    Okay, I made it this time and it works great, except one thing. cin.get() doesn't work at the end:

    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main(int argc,char **argv)
    {
        vector<int> num;     //Vector for storing numbers..
        unsigned int i=0;   //Counter
    
        cout<<"--Program for finding number average--\n"<<
              "------------Input 0 to stop-----------\n\n";
    
        while(1)
        {
            int n=0; //Dummy variable
    
            cout<<"Enter number "<<i+1<<": ";
            cin>>n;
    
            if(n==0) break;
    
            num.push_back(n);
            ++i;
        }
    
        int aver=0;
    
        for(unsigned int x=0;x<i;++x) aver+=num.at(x);
    
        cout<<"Average of inputed numbers is: "<<aver/i;
        cin.get();
    
        num.clear();
    
        return 1;
    }
    EDIT: problem solved, just use cin.get() twice or _getch()
    Last edited by hauzer; 10-27-2008 at 06:27 AM.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,784
    That is because sometimes there is crap left in the input buffer and so cin.get() "eats" that something and returns immediately.
    You should not really use it to stop the console from disappearing. A better approach is using a proper IDE or a debugger to break.
    But these are platform dependant... what OS are you using?

    Also, every new must be matched with a delete; otherwise you get a memory leak.
    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.

  8. #8
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    It's windows. I will just use _getch().
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hooking Crash?
    By Elysia in forum Windows Programming
    Replies: 9
    Last Post: 03-15-2008, 01:13 PM
  2. Can not debug a crash
    By hannibar in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2007, 10:02 AM
  3. Dynamic array sizing causes crash
    By Mithoric in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2003, 06:46 AM
  4. FYI: asctime(gmtime(&mytime)) = crash!
    By anonytmouse in forum C Programming
    Replies: 2
    Last Post: 09-29-2003, 02:24 AM

Tags for this Thread


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