Tech Off Thread

6 posts

Newbie (quite possibly) problem with C++

Back to Forum: Tech Off
  • User profile image
    JeanValjean

    Hi everyone,

    I'm a bit of a beginner to programming and was wondering whether anyone could help me with this snippet of some code I've been working on. I want it to play with a large array but at the moment, the program worked fine for smaller values of the num_nodes variable but is crashing completely at the creationg of the larger nodes[num_nodes] array. It's very frustrating, and no, as you can see from my very archaic debugging tools, I don't have the luxury of installing visual studio, and have been using gcc based dev-cpp (mingw version) compiler.

    The bizarre thing is that it works fine for larger values when compiled in Linux.

    Here's the code.

    wrote:

     #include <iostream>
     #include <fstream>
     
     using namespace std;
     
     
     int main(){
      ofstream debug_file("debug.txt");
     
      struct vertex {
       int element_id;
       int element_type; // could be a very large value
       int corner;
      };
     
      struct element_aware_node {
       float x, y;
       int node_type;
       int row_below;
      int num_elements;
      vertex vertex_of[10];
      int fem_pos;
      };
     
      cout << "input the number of nodes to create..." << endl;

      int num_nodes= 0;

      while ((!(cin >> num_nodes)) || (num_nodes <= 0)){
       cin.clear();
       cin.ignore(1000, '\n'); //ignore first 1000- an arbitrary large value- characters or characters uptill the first carriage return, whichever occurs first.
     
       cout << "input the number of nodes to create, number must be a positive integer!" << endl;
       debug_file << "\nNumber of nodes entered by user is " << num_nodes << endl;
      }
      element_aware_node node1;
     
      vertex v;
      debug_file << "sizeof(vertex)= " << sizeof(v) << endl << endl;
     
      debug_file << "\nNow creating " << num_nodes << " nodes..." << endl;
     
      debug_file << "sizeof(a node)= " << sizeof(node1) << " or " << num_nodes << " nodes take up " << (num_nodes*sizeof(node1)) << " bytes." << endl;
       element_aware_node nodes[num_nodes];
      debug_file << "\n\n" << num_nodes << " nodes were successfully created." << endl;
      return 1;
     }



    I'd appreciate any help. mem/c yields the following:

    wrote:

    Conventional Memory :

      Name                Size in Decimal       Size in Hex
    -------------      ---------------------   -------------
      MSDOS              12352      ( 12.1K)       3040
      KBD                 3296      (  3.2K)        CE0
      HIMEM               1248      (  1.2K)        4E0
      COMMAND             3728      (  3.6K)        E90
      KB16                6096      (  6.0K)       17D0
      FREE                 112      (  0.1K)         70
      FREE                 944      (  0.9K)        3B0
      FREE              627376      (612.7K)      992B0

    Total  FREE :       628432      (613.7K)

    Upper Memory :

      Name                Size in Decimal       Size in Hex
    -------------      ---------------------   -------------
      SYSTEM            196592      (192.0K)      2FFF0
      MOUSE              12528      ( 12.2K)       30F0
      MSCDEXNT             464      (  0.5K)        1D0
      REDIR               2672      (  2.6K)        A70
      DOSX               34848      ( 34.0K)       8820
      FREE                 928      (  0.9K)        3A0
      FREE               79504      ( 77.6K)      13690

    Total  FREE :        80432      ( 78.5K)

    Total bytes available to programs (Conventional+Upper) :      708864   (692.3K)
    Largest executable program size :                             627376   (612.7K)
    Largest available upper memory block :                         79504   ( 77.6K)

       1048576 bytes total contiguous extended memory
             0 bytes available contiguous extended memory
        941056 bytes available XMS memory
               MS-DOS resident in High Memory Area



    Is mem/c relevant? Because I've got 256 MB RAM, and those 692.3K are much closer to the small values of num_nodes causing the crash.

    Is the solution declaring the matrix array into dynamic memory? How do I do that ("new element_aware_nodes..." doesn't work) and how do I de-allocate it from memory?

    Thanks, everyone...

    J.

    PS: Sorry for the "[quote user=""]" cheat. What tags are allowed and where do I find them?

  • User profile image
    Sven Groot

    You are creating the array on the stack. Stack memory is limited. This limitation is determined by the compiler, typically it defaults to 1MB. This number might be different on the compilers you used on Linux, explaining why it worked there.

    You could try increasing the stack size (there's usually a command-line switch for it), but you're probably better off allocating the array on the heap (using the new[] keyword, don't forget to delete[]!), or better yet, use the std::vector class.

    Edit: you can use [ code language="C++" ] [ /code ] (without the added spaces) for code fragments.

  • User profile image
    JeanValjean

    Brillant! thanks, Sven. I feel very stupid for not having looked this up before attempting the whole Linux port. Idiot (me)!

    I wrote


    element_aware_node* nodes = new element_aware_node[num_nodes];
    // ...
    delete[] nodes;


    It seems that does the trick! <rant> And to think I spent so much... &^%$$$ </rant>

    Thanks ever so much Sven Groot!!! Only hope that I have enough time to put it to good use.

  • User profile image
    senthilkumar

    But how did the OP get the code to compile? I thought array sizes must be compile time constants?

  • User profile image
    cravikiran

    While it is possible that a compiler designer could potentially place some restriction on array size, the problem above is with the stack size as Sven mentions.  Since the stack will grow/shrink with function calls, local vars, etc., there is no way (the compiler won't check at compile time anyway) to know when you can have gone over your stack size.  At run time, the operating  system will generate a signal which the compiler puts code for to perform the boundary check.

  • User profile image
    The​Programmer

    cravikiran wrote:
    the problem above is with the stack size as Sven mentions.


    In VS.Net, the array size cannot exceed ffffffff bytes; but for the problem above, it has nothing to do with the size. The problem was the array size was unknown at compile time.

    [code] element_aware_node nodes[num_nodes]; [/code]
    //size of num_nodes unknown at compile time




    senthilkumar wrote:
    I thought array size must be compile time constants?


    The behavior of how GCC handles array is a little bit odd. It doesn't complain the following code, while VS.Net will generate an error.

    [code]void matrix_input(double [][]);[/code]

    //VS will generate an error saying missing subscript, but it compiles fine under GCC even with -Wall switch turned on

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.