Access Violation Error

This is a discussion on Access Violation Error within the C Programming forums, part of the General Programming Boards category; I'm trying to write a program to help manage production tasks in a game I'm playing. For the most part ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    3

    Access Violation Error

    I'm trying to write a program to help manage production tasks in a game I'm playing. For the most part I can get the program to run fine; however I'm trying to write it so that I can use it for a longer period of time. To allow this I'm trying to set up a method that will increase the size of my storage array. I've managed to get my program working just fine if I DON'T try this and just set the original size to larger then the amount of data I'm trying to input, so I'm assuming the problem is contained within this method. When I am trying to use the method I get an Access Violation Error (compiles fine, only happens at run-time). I know this is somehow memory related, but I can't seem to figure it out. If anyone can come up with what might be wrong I'd greatly appreciate it.

    I find it kind of odd that this error doesn't present itself right away. It doesn't seem to happen until the 32nd permutation AFTER the array size is increased (332 overall).

    This is the structure
    Code:
    struct blueprint{
    	char *name; <-- memory for these is allocated elsewhere
    	char *category;
    
    	float wasteFactor;
    
    	// Production/Research(r) time (in seconds) //
    	int buildTime;
    	int rMinTime;
    	int rCopyTime;
    	int rProdTime;
    	//////////////////////////////////////////////
    
    	// Base Minerals Required //
    	int tritanium;
    	int pyerite;
    	int mexallon;
    	int isogen;
    	int nocxium;
    	int zydrine;
    	int megacyte;
    	////////////////////////////
    
    	int maxRuns; // Maximum number of runs per copy
    	int batch;	// Number of units made per batch
    };
    This is the actual method
    Code:
    void growArray(struct blueprint *array)
    {
    	struct blueprint *buffer;
    	int oldSize = size;
    	size *= 2;
    
    	buffer = (blueprint *)malloc(size * sizeof(struct blueprint));
    	
    	for (int x = 0; x < oldSize; x++)
    	{
    		buffer[x] = array[x];
    	}
    
    	array = buffer;
    }
    Since the problem MIGHT be located elsewhere I'm attaching the full code as well.
    Attached Files Attached Files

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm surprised it works that far. Count yourself lucky.
    Code:
    void growArray(struct blueprint *array)
    {
    	struct blueprint *buffer;
    	int oldSize = size;
    	size *= 2;
    
    	buffer = (blueprint *)malloc(size * sizeof(struct blueprint));
    	
    	for (int x = 0; x < oldSize; x++)
    	{
    		buffer[x] = array[x];
    	}
    
    	array = buffer;
    }
    This does not update the pointer outside of this function. You just think it does. You have two ways to fix this:
    Code:
    struct blueprint * growArray(struct blueprint *array)
    {
            ...
    	return buffer;
    }
    Return it, and assign the return to 'array'. Or, use a pointer to a pointer:
    Code:
    void growArray(struct blueprint **array)
    {
            ...
    	*array = buffer;
    }
    Of course, you have to update your other uses of 'array' in the function also.

    Oh, by the way, you've got a huge memory leak every single time this function happens, because you never free the old contents of 'array'. Consider correctly using realloc instead of all of this. But even then you'll have to do what I mentioned before, you just can skip the free inside this function (which you're doing already, which is wrong... )

    [edit]
    And just to prove the point...
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct a
    {
        int b;
    };
    
    void c( struct a * b )
    {
        struct a * d = malloc( sizeof( *d ) );
    	if( d )
    	    d->b = 10;
    		
    	b = d; /* doesn't work the way you think it does */
    }
    
    int main( void )
    {
        struct a * b = malloc( sizeof( *b ) );
    	if( b )
    	    b->b = 5;
    		
    	printf("b->b is %d\n", b->b );
    	c( b );
    	printf("b->b is %d\n", b->b );
    	printf("Oh, by the way, there's a memory"
    	       " leak in C since 'd' is lost.\n");
    		   
    	return 0;
    }
    
    /*
    gcc -o foo foo.c -Wall -pedantic -ansi
    ./foo
    
    b->b is 5
    b->b is 5
    Oh, by the way, there's a memory leak in C since 'd' is lost.
    */
    [/edit]

    Quzah.
    Last edited by quzah; 08-22-2005 at 06:15 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    3

    Memory Leak

    Yeah, I know I'm missing the free command... it was giving me another error that I've been unable to solve. I just wanted to get through this one problem first.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    3
    btw, thank you for the quick response... I REALLY appreciate it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  4. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21