Connecting Tech Pros Worldwide Forums | Help | Site Map

malloc not NULL, still get segmentation fault

Newbie
 
Join Date: Jul 2007
Posts: 4
#1: Jul 5 '07
Hi,
Please can you help me?

I have the following code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.   int** adjacencyOneMode;
  4.   int *dim = 3800;
  5.   int iv=0;
  6.  
  7.   /*initalise adjacency for the one mode projection*/
  8.   if((adjacencyOneMode = (int **)malloc((*dim)*sizeof(int*))) == NULL){
  9.     printf("\nERROR: Failure to initalize adjacency array\n");
  10.   }else{
  11.     printf("allocated memory for adjacency one mode first dime\n");
  12.   }
  13.  
  14.   for(iv=0; iv<*dim; iv++){
  15.     printf("Here = %d\n", iv);
  16.     adjacencyOneMode[iv] = (int *)(malloc((*dim)*sizeof(int)));
  17.     printf("allocated aom[%d]\n", iv);
  18.     if(adjacencyOneMode[iv] == NULL){
  19.       printf("ERORR - cannot allocate memory for aom[] = NULL\n");
  20.     }
  21.   }
  22.   printf("Finished adjacency OneMOde\n");
  23.  
  24.  
[OUTPUT]

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.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#2: Jul 5 '07

re: malloc not NULL, still get segmentation fault


You are accessing memory outside your allocation or you are using a null or uninitialized pointer.
Familiar Sight
 
Join Date: Feb 2007
Location: Toronto Ontario
Posts: 211
#3: Jul 5 '07

re: malloc not NULL, still get segmentation fault


Umm, generally you should actually post some useful information (maybe the code that's seg faulting or something like that). weaknessforcats is totally correct though, you're probably accessing memory outside of the allocation.

If you post more information we can help you further.
Newbie
 
Join Date: Jul 2007
Posts: 4
#4: Jul 5 '07

re: malloc not NULL, still get segmentation fault


Quote:

Originally Posted by weaknessforcats

You are accessing memory outside your allocation or you are using a null or uninitialized pointer.

Yes I know, but when I code:

Expand|Select|Wrap|Line Numbers
  1. if((adjacencyOneMode = (int **)malloc((*dim)*sizeof(int*))) == NULL){
  2.     printf("\nERROR: Failure to initalize adjacency array\n");
  3.   }else{
  4.     printf("allocated memory for adjacency one mode first dime\n");
  5.   }
  6.  
and this allocates memory for adjacencyOneMode, does this not allocate enough memory for?:

Expand|Select|Wrap|Line Numbers
  1.  for(iv=0; iv<*dim; iv++){
  2.     printf("Here %d\n", iv);
  3.     adjacencyOneMode[iv] = (int *)(malloc((*dim)*sizeof(int)));
  4.     printf("allocated aom[%d]\n", iv);
  5.     if(adjacencyOneMode[iv] == NULL){
  6.       printf("************ERORR - cannot allocate memory for aom[] = NULL\n");
  7.     }
  8.  }
  9.  
So I guess what I am trying to say is - I thought I had initalized the pointer and I am accessing memory within the allocation. Am I not? It only does 6 iterations of the for loop - when I set the *dim = 3000? If I am not allocating them correctly can you tell from this snippet where I am going wrong?

Help is very much appreciated!!
Newbie
 
Join Date: Jul 2007
Posts: 4
#5: Jul 5 '07

re: malloc not NULL, still get segmentation fault


Expand|Select|Wrap|Line Numbers
  1.  for(iv=0; iv<*dim; iv++){
  2.     printf("Here %d\n", iv);
  3.     adjacencyOneMode[iv] = (int *)(malloc((*dim)*sizeof(int)));
  4.     /** THE CODE IS SEG FAULTING HERE**/
  5.     printf("allocated aom[%d]\n", iv);
  6.     if(adjacencyOneMode[iv] == NULL){
  7.       printf("************ERORR - cannot allocate memory for aom[] = NULL\n");
  8.     }
  9.  }
  10.  
Output:

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
Familiar Sight
 
Join Date: Feb 2007
Location: Toronto Ontario
Posts: 211
#6: Jul 5 '07

re: malloc not NULL, still get segmentation fault


Ok I'm confused as to why you're dereferencing dim.

Expand|Select|Wrap|Line Numbers
  1. int main() {
  2.     int dim = 10;
  3.     int iv;
  4.     int **adjacencyOneMode;
  5.     adjacencyOneMode = (int **)malloc((dim)*sizeof(int*)) ;
  6.  
  7.      if(adjacencyOneMode == NULL){
  8.           printf("\nERROR: Failure to initalize adjacency array\n");
  9.         }else{
  10.           printf("allocated memory for adjacency one mode first dime\n");
  11.         }
  12.  
  13.     for(iv=0; iv<dim; iv++){
  14.         printf("Here %d\n", iv);
  15.         adjacencyOneMode[iv] = (int *)(malloc((dim)*sizeof(int)));
  16.         printf("allocated aom[%d]\n", iv);
  17.         if(adjacencyOneMode[iv] == NULL){
  18.         printf("************ERORR - cannot allocate memory for aom[] = NULL\n");
  19.         }
  20.     }
  21.     return (1);
  22. }
  23.  

I used this and it compiles/runs find.
Newbie
 
Join Date: Jul 2007
Posts: 4
#7: Jul 6 '07

re: malloc not NULL, still get segmentation fault


Hi,

Thanks for your help. I was using "dim" as a pointer because I was reading the value in from another program.
It turns out this isn't were the problem was after all.
Before this point in the program, I was allocating alot of memory to store data read in from a number of large files. It seems at this point in the program I reached the memory limit. I've rewritten the program more efficently, and no longer get the error.

Thanks.
Reply


Similar C / C++ bytes