Thread: Structs and dynamic memory allocation

  1. #1
    Registered User
    Join Date
    Nov 2010

    Structs and dynamic memory allocation


    I'm quite inexperienced as far as C programming goes and I have some problems regarding memory allocation. Those are the relevant code segments:

    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
    1143 [main] pushrelabel 2856 open_stackdumpfile: Dumping stack trace to pushr

    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..


  2. #2
    Registered User
    Join Date
    Sep 2006
    C doesn't guarantee to crash a bad program. I am confident you've ID'd the problem (using variables which have no memory given to them).

    As a program becomes moderately complex, the really simple program errors that you could get away with, such as going past the end of a char or int array, are sharply reduced (naturally).

  3. #3
    Registered User
    Join Date
    Nov 2010
    Yes, I found the mistake, it turned out to be in a completely different struct and the access violation occured later in the code. Why the printf command didn't work remains a mystery to me, but anyways, I'm done and everything works fine. Thanks anyways!

  4. #4
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    Without seeing the printf statements that didn't execute, I can't say for sure, but the printf may have failed due to a "missing" newline character. That is to say, printf uses stdout, which is buffered and only displays output when it gets a newline. Thus, if you print something, but there's no newline, then your program crashes, you wont see the output. stderr is unbuffered and the output arrives immediately. Regular program output should still use stdout, but any debug or error output should use stderr. You can use fprintf(stderr, ...) to achieve this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs, dynamic memory, and phone book entries
    By wkfcs in forum C Programming
    Replies: 5
    Last Post: 10-09-2009, 03:57 PM
  2. what is dynamic memory allocation
    By dbzx in forum C Programming
    Replies: 7
    Last Post: 06-08-2009, 08:11 AM
  3. dynamic memory allocation problem
    By firyace in forum C Programming
    Replies: 4
    Last Post: 05-23-2007, 09:57 PM
  4. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM
  5. Replies: 7
    Last Post: 05-06-2002, 11:40 AM