473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

malloc not NULL, still get segmentation fault

4
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.
Jul 5 '07 #1
6 2059
weaknessforcats
9,208 Expert Mod 8TB
You are accessing memory outside your allocation or you are using a null or uninitialized pointer.
Jul 5 '07 #2
Silent1Mezzo
208 100+
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.
Jul 5 '07 #3
chubbs
4
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!!
Jul 5 '07 #4
chubbs
4
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
Jul 5 '07 #5
Silent1Mezzo
208 100+
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.
Jul 5 '07 #6
chubbs
4
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.
Jul 6 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: afarah | last post by:
Under what condition will we see a segmentation fault in the malloc library routine? Specifically, a failure in call to "chunk_alloc()" from within malloc. I have a multi-threaded c++ program...
13
by: Steve Zimmerman | last post by:
Esteemed contributors to clc: Thank you for all the responses. Experiments 2 and 3 below are identical, except that experiment 2 does not call free(), while experiment 3 does. With such a...
1
by: Dawn Minnis | last post by:
Hey guys - this code when called with parameters: driver.o n n 12 12 12 12 12 12 2.6 3.2 is kicking back a segmentation fault. I've read the rest of the postings but am still confused. Can...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
6
by: I_have_nothing | last post by:
Hi! I am new in C. I try to use dynamical allocation fuction malloc( ) and realloc( ). I found something strange. After several calling realloc( ), the malloc( ) will give me a Segmentation...
14
by: Roka100 | last post by:
Hi all, I tried 2 programs : #include <stdio.h> #include <string.h> 1, int main(void){ char *str = NULL;
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
40
by: ramu | last post by:
Hi, what happens when i run the below code? main() { int *p; while(1) p= (int *)malloc(1000); } Do i get segmentation fault?
25
by: jbholman | last post by:
I am pretty new to C and doing my first project in C. I actually read almost the entire FAQ, but can't seem to figure out this problem. I have a structure. I have a list of these structures. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.