Results 1 to 9 of 9
  1. #1
    hobosrock696's Avatar
    Join Date
    Aug 2010
    Gender
    male
    Posts
    45
    Reputation
    9
    Thanks
    1
    My Mood
    Mellow

    Help with some c++ runtime errors

    I wrote some code and so far all it should do is allocate memory and then free it up however if I try to allocate A LOT of memory it doesn't throw an error instead it just locks up....

    Now the program will accept 999999999 and throw the error I coded in but if you try adding 1 more 9 so there are 10 digits it freezes up and does not throw an error. The last message on the screen when it freezes is

    "Allocating memory for operation, please wait..."

    So I would think it literally freezes outside of my code in one of the defines?

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <new>
    using namespace std;
    
    
    
    int* makelist (int a)
    {
         cout << "Initializing memory for operation, please wait... \n";
         int * lenghth;
         lenghth = new (nothrow) int [a];
         if (lenghth == 0)
         {
              cout << "ERROR ALLOCATING MEMORY!\n";
              return 0;
         }
         cout << "Done!\n";
         return(lenghth);
    }
    
    
    
    void clearmem (int * addy, int lenghth)
    {
         cout << "Freeing memory please wait...\n";
         delete[lenghth] addy;
         cout << "Done!";
    }
    
    
    
    void pause()
    {
         cin.ignore();
         cin.get();
    }
    
    
    
    int main()
    {
    	int a;
    	int a_const;
    	int * list_location;
    	cout << "Enter the number of numbers you want to find in the Fibonaci sequence.\n";
    	cin >> a;
    	a_const = a;
    	if (a == 0)
    	{
    		cout << "Ummmm so you started this program to not use it.... im not going\nto calculate the 0th number -.-\npress enter to close...";
    		pause();
    		return 0;
    	}
    	list_location = makelist (a);
    	clearmem(list_location, a_const);
    	pause();
    	return 0;
    }

  2. #2
    AeroMan's Avatar
    Join Date
    Dec 2008
    Gender
    male
    Location
    Hell
    Posts
    3,294
    Reputation
    189
    Thanks
    3,049
    My Mood
    Busy
    im not sure, but try add add System("PAUSE");
    but you did not add "Clearmem" & Pause into your main functions, try to add that.
    im not a pro, dont be angry if its uncorrect, i just try

  3. #3
    hobosrock696's Avatar
    Join Date
    Aug 2010
    Gender
    male
    Posts
    45
    Reputation
    9
    Thanks
    1
    My Mood
    Mellow
    Im not sure what your saying exactly but I do hoave the two functions used in main I know that it freezes in the makelist function and I tried to avoid using System("pause") because I want to keep it as easy to compile on multiple platforms as possible. Please do elaborate if I misunderstood and ANY help is welcome

    EDIT: I tried to throw in a pause directly after the cout command that displays the last text on screen and strangely it skips this pause command and any other pause I put in anywhere be it the pause function I wrote or system("pause")
    Last edited by hobosrock696; 09-03-2010 at 03:35 AM.

  4. #4
    hobosrock696's Avatar
    Join Date
    Aug 2010
    Gender
    male
    Posts
    45
    Reputation
    9
    Thanks
    1
    My Mood
    Mellow
    Bump, dont want to start a new thread :/ sorry

  5. #5
    Toxic Waltz's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Posts
    114
    Reputation
    14
    Thanks
    18
    this is a guess...

    INT_MIN


    Minimum value for a variable of type int.


    –2147483648

    INT_MAX


    Maximum value for a variable of type int.


    2147483647


    999999999 is smaller than int max
    9999999999 is larger than int max and give an error.

    try the values 214783647 and 214783648
    if I'm right the last one gives an error.

    is it bigger than or bigger then?

  6. The Following User Says Thank You to Toxic Waltz For This Useful Post:

    why06 (09-09-2010)

  7. #6
    hobosrock696's Avatar
    Join Date
    Aug 2010
    Gender
    male
    Posts
    45
    Reputation
    9
    Thanks
    1
    My Mood
    Mellow
    See theres the funny thing... neither give an error.... both freeze so what Im going to try is getting an input as a string and pulling the number out into a variable.... Will post back and thanks for the attempt anything it welcome


    EDIT: So it turns out that using stringstream(stringvar) >> integervar DOES NOT help.... hmm... time to get sidetracked and write code to check for integers in a string.....

    EDIT 2: So even at 1 billion it throws an error but at a certain point it just freaks and freezes..... Otherwise it will throw an error.... anyone ever done this? allowed the user to allocate a bunch of memory based on a number they put in? Im definitely missing something....
    Last edited by hobosrock696; 09-04-2010 at 03:04 AM.

  8. #7
    hobosrock696's Avatar
    Join Date
    Aug 2010
    Gender
    male
    Posts
    45
    Reputation
    9
    Thanks
    1
    My Mood
    Mellow
    Bump..... I'm seeing this problem with tutorial code too.... When you try to allocate too much memory it just freezes....

    I learned from cplusplus website tutorial section on dynamic memory...

    Compile the below code and tell it you want to enter 1000000000 numbers and it will error out but tell it you want to enter 99999999999 and it will just freeze. Im using vs 2008

    The folowing code IS NOT MINE [cplusplus.com]
    Code:
    // rememb-o-matic
    #include <iostream>
    #include <new>
    using namespace std;
    
    int main ()
    {
      int i,n;
      int * p;
      cout << "How many numbers would you like to type? ";
      cin >> i;
      p= new (nothrow) int[i];
      if (p == 0)
        cout << "Error: memory could not be allocated";
      else
      {
        for (n=0; n<i; n++)
        {
          cout << "Enter number: ";
          cin >> p[n];
        }
        cout << "You have entered: ";
        for (n=0; n<i; n++)
          cout << p[n] << ", ";
        delete[] p;
      }
      return 0;
    }
    Im thinking this has to do with a 2gb limit on 32 bit executables no?
    Last edited by hobosrock696; 09-09-2010 at 02:58 PM.

  9. #8
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    Quote Originally Posted by hobosrock696 View Post
    Bump..... I'm seeing this problem with tutorial code too.... When you try to allocate too much memory it just freezes....

    I learned from cplusplus website tutorial section on dynamic memory...

    Compile the below code and tell it you want to enter 1000000000 numbers and it will error out but tell it you want to enter 99999999999 and it will just freeze. Im using vs 2008

    The folowing code IS NOT MINE [cplusplus.com]
    Code:
    // rememb-o-matic
    #include <iostream>
    #include <new>
    using namespace std;
    
    int main ()
    {
      int i,n;
      int * p;
      cout << "How many numbers would you like to type? ";
      cin >> i;
      p= new (nothrow) int[i];
      if (p == 0)
        cout << "Error: memory could not be allocated";
      else
      {
        for (n=0; n<i; n++)
        {
          cout << "Enter number: ";
          cin >> p[n];
        }
        cout << "You have entered: ";
        for (n=0; n<i; n++)
          cout << p[n] << ", ";
        delete[] p;
      }
      return 0;
    }
    Im thinking this has to do with a 2gb limit on 32 bit executables no?
    Check what toxic waltz posted >.<
    There's a limit to what can be stored in an int, so if you want numbers above 2147483647 consider using unsigned int(goes to roughly double of that, but will make it unable to go negative)
    Ah we-a blaze the fyah, make it bun dem!

  10. The Following User Says Thank You to Hell_Demon For This Useful Post:

    why06 (09-09-2010)

  11. #9
    hobosrock696's Avatar
    Join Date
    Aug 2010
    Gender
    male
    Posts
    45
    Reputation
    9
    Thanks
    1
    My Mood
    Mellow
    Hmmm well i have tried to do so but even numbers that should fit in an integer don't work. Perhaps this could be resolved faster if i posted a link to an executable with all those virus scans that are required so you have the compiled program to see how it behaves?

Similar Threads

  1. [Help Request] Need Help With A.V.A Error
    By ch1025 in forum Alliance of Valiant Arms (AVA) Help
    Replies: 7
    Last Post: 05-14-2011, 09:15 AM
  2. Need help with some codes - Vb8
    By NatureSkillz in forum Visual Basic Programming
    Replies: 9
    Last Post: 09-18-2009, 08:22 AM
  3. Need help with Client MFC Application error....
    By lyabccba in forum CrossFire Hacks & Cheats
    Replies: 2
    Last Post: 06-06-2009, 05:14 PM
  4. need help with some quick codes
    By ravinghippie in forum Anti-Cheat
    Replies: 1
    Last Post: 05-13-2009, 09:24 AM
  5. i need help with some HARD Adresses
    By shanky1 in forum WarRock - International Hacks
    Replies: 10
    Last Post: 08-19-2007, 04:30 PM