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) 992B0Total 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) 13690Total 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?
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.