Hi,
I'm quite inexperienced as far as C programming goes and I have some problems regarding memory allocation. Those are the relevant code segments:
Code:
typedef struct allowedEdge_t {
int e;
unsigned char res;
struct allowedEdge_t *left;
struct allowedEdge_t *right;
} allowedEdge;
typedef struct node_t {
int edgeCount1;
int* edges1;
int arLength1;
int edgeCount2;
int* edges2;
int arLength2;
int psi;
int ex;
unsigned char active;
struct allowedEdge_t *alE;
} node;
...
int main(int argc, char *argv[]) {
const int delta = 32;
int nodeCount;
int edgeCount;
edge* edges;
node* nodes;
...
nodes = (node*) malloc(sizeof(struct node_t) * nodeCount);
edges = (edge*) malloc(sizeof(struct edge_t) * edgeCount);
for (i = 0; i < nodeCount; i++) {
nodes[i].psi = 0;
nodes[i].ex = 0;
nodes[i].active = 0;
nodes[i].edges1 = (int*) malloc(sizeof(int) * delta);
nodes[i].edges2 = (int*) malloc(sizeof(int) * delta);
nodes[i].edgeCount1 = 0;
nodes[i].edgeCount2 = 0;
nodes[i].arLength1 = delta;
nodes[i].arLength2 = delta;
}
...
}
The whole code compiles just fine with gcc, but when I execute the program, I get the following error:
0 [main] pushrelabel 2856 exception::handle: Exception: STATUS_ACCESS_VIOL
ATION
1143 [main] pushrelabel 2856 open_stackdumpfile: Dumping stack trace to pushr
elabel.exe.stackdump
Note that nodeCount and edgeCount are valid integers (50 and 100, to be precise).
I assume that it has something to do with the node structure containing the dynamic arrays edges1 and edges2, because I compiled a rather similar code some weeks ago with simpler structs and I didn't get any errors.
Also, it might be worth mentioning that not even one for loop is executed: If I put some printf command at the end of one loop, the program crashes before anything is displayed. If, however, I put "return 0" at the end of the loop, I don't get the access violation mentioned above, which is kind of weird..
Greetings,
Paul