Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 26

Thread: C++ program crashes

  1. #11
    Join Date
    Feb 2009
    Beans
    1,469

    Re: C++ program crashes

    It might not crash if you create it with new, but that's just avoiding the real problem, which is located between your keyboard and chair.

    If and when you someday need to handle a very large array, you'll probably be suitably equipped to deal with the consequences at that point. Right now you should focus on learning to program and not get involved in the details of one implementation's built-in limits.

  2. #12
    Join Date
    Jun 2010
    Beans
    92

    Re: C++ program crashes

    Quote Originally Posted by trent.josephsen View Post
    It might not crash if you create it with new, but that's just avoiding the real problem, which is located between your keyboard and chair.

    If and when you someday need to handle a very large array, you'll probably be suitably equipped to deal with the consequences at that point. Right now you should focus on learning to program and not get involved in the details of one implementation's built-in limits.
    You probably didn't mean to sound harsh, but your answer could be construed as a little off the mark, don't you think?

    Lymphocyte asked why his code didn't work and others with more experience pointed out his attempted use of the stack.

  3. #13
    Join Date
    Feb 2009
    Beans
    1,469

    Re: C++ program crashes

    Harsh, yes. Off the mark, no.

    It's true that the (most likely) immediate cause of the problem is trying to create a data structure larger than the stack can hold. But that's an implementation detail that programmers shouldn't have to worry about under normal circumstances. It's really irrelevant because practically, the solution is to not depend on very large data structures.

    It's a bit like debating whether to use an int or a float to store some numeric amount. Yes, there are built-in limits that constrain what you can do with those types. But when a new programmer asks "Should I use an int or a float to store amounts of money?" the best response isn't "Well, the maximum value of an int is 2147483647, and..." The best response is "What makes sense?"

    Let's not confuse giving information with teaching.

  4. #14
    Join Date
    Oct 2011
    Location
    Chicago, IL
    Beans
    419
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: C++ program crashes

    Quote Originally Posted by trent.josephsen View Post
    But when a new programmer asks "Should I use an int or a float to store amounts of money?" the best response isn't "Well, the maximum value of an int is 2147483647, and..." The best response is "What makes sense?"
    Obviously the answer is to use integers because fixed-point arithmetic is usually faster than floating-point. Beginners should always remember to optimize first, and think later

    On a more serious note, I understand your intent, but I'd hate to see a beginner leave this community because they mistook your teaching for an insult.

  5. #15
    Join Date
    Feb 2009
    Beans
    1,469

    Re: C++ program crashes

    I take your point and I will moderate my vitriol in the future. OP, I apologize for the offensiveness in my tone.

  6. #16
    Join Date
    Jul 2012
    Beans
    22

    Re: C++ program crashes

    I had to comment on this..... 30 million elements? Damn, that's a lot of memory usage. I'm interested in knowing why you needed that many. When I learned about arrays it was to display my name or similar.

    I also concur with allocating it onto the heap but even then.... 30 million....

  7. #17
    Join Date
    May 2007
    Beans
    251

    Re: C++ program crashes

    Quote Originally Posted by SirWhy View Post
    I had to comment on this..... 30 million elements? Damn, that's a lot of memory usage. I'm interested in knowing why you needed that many. When I learned about arrays it was to display my name or similar.

    I also concur with allocating it onto the heap but even then.... 30 million....
    30M may SOUND a lot, but in terms of memory usage, it entirely depends on what is it that's being stored.

    For example, 30M of long longs (8bytes) amounts to only 240MBytes of memory - which is not all that much considering the address space of a typical process on 32-bit platform is 4GBytes. You could easily accommodate 240MBytes on heap.

    I'm sure there are use-cases that gobble up memory in GBs at a time - like CAD apps or video editors or even some games.
    The Unforgiven

  8. #18
    Join Date
    Oct 2011
    Location
    Chicago, IL
    Beans
    419
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: C++ program crashes

    As has been pointed out before, OP's superficial mistake was declaring a large sequential array on the stack, but I can't think of a solution where using 30e6 element array is useful. Of course a large, complex program might command larger amounts of memory, but the point that people on this thread have made is that declaring a such a large array is probably a poor way to solve any problem

  9. #19
    Join Date
    Jun 2012
    Beans
    10

    Re: C++ program crashes

    i wasnt insulted, it was probably a dumb question outside of real world usage. I was just wondering why the program crashed

  10. #20
    Join Date
    Jan 2007
    Location
    Location: Location:
    Beans
    1,246
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: C++ program crashes

    I see a lot of people being snobby, sure, it's not good practice to create an array that large but that's how people learn new things. Telling OP the problem exists between the keyboard and chair only shows your arrogance, not OP's ignorance. I will lmao when he/she invents a new method for memory management and you look like a simpleton.
    Last edited by |{urse; July 11th, 2012 at 04:31 PM.
    clear && echo paste url and press enter; read paste; (youtube-dl $paste) | zenity --progress --title="" --text "Downloading, please wait" --auto-close --pulsate && ans=$(zenity --file-selection); gnome-terminal -x mplayer "$ans"

Page 2 of 3 FirstFirst 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •