Windows error dealing with long

This is a discussion on Windows error dealing with long within the C++ Programming forums, part of the General Programming Boards category; I have a function that takes a long and finds the factors of it. It works for small numbers (the ...

  1. #1
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870

    Windows error dealing with long

    I have a function that takes a long and finds the factors of it. It works for small numbers (the highest I've tested is 1234567), after which it gives me a windows error. The numbers are within the bounds of a long on my machine (2147483647), and I do not understand why it is giving me these errors. I'm using WinXP SP2, wxDev-C++ w/ MinGW. My code is attached.
    Attached Files Attached Files
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    I think because of this:

    Code:
    for (int i = n; i <=a; i++)
    Change int to the template argument type.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    #include <math.h>
    ->
    Code:
    #include <cmath>
    ? You've already included <cstdlib> . . .

    Oh and toupper() is in <cctype>.

    Code:
    template <typename T>
    void findoverrange (T n, T a)
    {
        for (int i = n; i <=a; i++)
        {
            cout << "Factors for " << i << endl;
            findfactors<T>(i);
        }
    }
    Should that int be of type T? [edit] Beaten again. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    However, that alone is not the problem... Currently looking at findfactors()...

    That is a big array you are creating there manutd. If I input 234.567.896 just for fun for both start and end
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I did what you said (stupid error ), yet the errors still display. Any ideas? Will this work on your systems?
    EDIT: Thanks Mario F., any ideas to work around this? I would cout when I got the number, but I don't want duplicates.
    Last edited by manutd; 11-18-2006 at 04:55 PM.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    Well, all of a sudden what occurs to me is thatyour code is correct. But I would do this just for int.

    Over 200 million unsigned longs fills my stack. That si what I think the error is all about. Or try to do it on the heap
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Use a vector. A non-constant array size isn't standard C++ anyway.

  8. #8
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Thanks so much! I've fixed it: make this:
    Code:
    T arraynums[n];
    This:
    Code:
    T* arraynums = new T[n];
    And add these at the end:
    Code:
    delete [] arraynums;
    arraynums = 0;
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Why not use a vector? That is exactly what it is for.

  10. #10
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I probably should, but I'm a C diehard and I like to keep my code relatively interchangable. Also, I like array syntax better.
    EDIT: This runs extremely slow for large numbers, is this a fact of life or would a vector be more optimized?
    Last edited by manutd; 11-18-2006 at 05:17 PM.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    Yeah. I was about to post too... It works fine on the heap as I was suspecting
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> is this a fact of life or would a vector be more optimized?
    I doubt vectors would be faster in this simple case.

    >> I like to keep my code relatively interchangable.
    You're using templates (and now new/delete as well).

  13. #13
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Relatively In this case I would just take out the T's and replace them, then replace new and delete. Any ideas for optimization of this code? Any obvious areas that are slowing the program down? Also, when I input values seperated by a lot (1 to 100000), the program quits immediatly, with no windows errors. What have I done?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    > EDIT: This runs extremely slow for large numbers, is this a fact of life or would a vector be more optimized?

    You are traversing 3 times an array containing hundreds of millions of indexes. Doing a % on each element on two of those occasions, and assigning on all occasions. On my aged laptop it actually performs better than could probably be expected.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    Don't flush until the very end... that's one...

    Someone else can come up with better. I don't think I can
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Peculiar Problem with Printf
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-02-2002, 12:34 AM
  2. Peculiar Problem with Printf
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 07-02-2002, 12:03 AM
  3. Background-process app
    By philip in forum C++ Programming
    Replies: 14
    Last Post: 03-26-2002, 11:22 AM
  4. need help
    By emperor in forum C Programming
    Replies: 1
    Last Post: 03-04-2002, 11:26 AM
  5. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 01:13 PM

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