Please can you help me?
I have the following code:
Expand|Select|Wrap|Line Numbers
- int** adjacencyOneMode;
- int *dim = 3800;
- int iv=0;
- /*initalise adjacency for the one mode projection*/
- if((adjacencyOneMode = (int **)malloc((*dim)*sizeof(int*))) == NULL){
- printf("\nERROR: Failure to initalize adjacency array\n");
- }else{
- printf("allocated memory for adjacency one mode first dime\n");
- }
- for(iv=0; iv<*dim; iv++){
- printf("Here = %d\n", iv);
- adjacencyOneMode[iv] = (int *)(malloc((*dim)*sizeof(int)));
- printf("allocated aom[%d]\n", iv);
- if(adjacencyOneMode[iv] == NULL){
- printf("ERORR - cannot allocate memory for aom[] = NULL\n");
- }
- }
- printf("Finished adjacency OneMOde\n");
Here 0
allocated aom[0]
Here 1
allocated aom[1]
Here 2
allocated aom[2]
Here 3
allocated aom[3]
Here 4
allocated aom[4]
Here 5
allocated aom[5]
Here 6
[/OUTPUT]
I run the program that contains this code a number of times for different conditions - sometimes it works sometimes it doesn't work. This is the segment of code where I get a segmentation fault when it doesn't work. I'm sorry but its really not obvious to me why its not working and I'm pulling my hair out. On the 7th iteration it is not outputting that malloc returned NULL.
Could it be an error somewhere else and the problem is only manifesting itself here? I do allocate a lot of memory in the program before this stage, although I always check that it was allocated correctly and free it when no longer needed.
Any help would be greatly appreciated.
Sorry if the answer is really obvious.
chubbs.