Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

here is my problem, I malloc my array like that:

e->map = malloc(sizeof(int *) * e->map_y);
i = -1;
while (++i < e->map_x)
    e->map[i] = malloc(sizeof(int) * e->map_x);

where e->map_y = 14 and e->map_x = 10 The problem is I can't access (I have a segfault) elements after e->map[10][0] (included) I tough about I invert x and y but it doesn't seem to be the case here.

I can post my entire code if necessary, thx

hi, I added the entire project on github for more details: https://github.com/42-hbock/fdf

this part of the code is in src/default_reading.c, the malloc is in the function char *default_reading(char *file, t_env *e) and I have the segmentation fault while accessing in void create_int_map(char *cmap, t_env *e)

share|improve this question

closed as off-topic by juanchopanza, mafso, alk, robbrit, HaveNoDisplayName Mar 2 '15 at 2:30

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – juanchopanza, mafso, alk, robbrit, HaveNoDisplayName
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
The while should be checking for e->map_y, not e->map_x, because that is the size of the first array you allocated. – Jiminion Mar 1 '15 at 19:02
    
It is segfaulting because you didn't malloc 14 arrays, you only allocated 10! – Jiminion Mar 1 '15 at 19:05
    
Jiminion I checked e->map_y with a printf just before the first malloc, and it's equal to 14 – hboy Mar 1 '15 at 19:18
    
right, so you should allocate 14 arrays for it, not the 10 you are currently allocating.... – Jiminion Mar 1 '15 at 19:20
    
there are several errors in the code logic. here is one: in default_reading() this line: 'char *c_map;' is soon followed by this line: 'c_map = ft_reallocat(c_map, buf);' However, c_map contains some trash value, not NULL nor a pointer to some allocated memory, The results of performing a realloc function is undefined behaviourl leads to corruption of the heap and a seg fault event. suggest changing 'char *c_map'; to 'char *c_map = NULL;' In reality, it is always a good idea to initialize any variable when it is defined. – user3629249 Mar 1 '15 at 19:46

Should be:

e->map = malloc(sizeof(int *) * e->map_y);
i = -1;
while (++i < e->map_y)
    e->map[i] = malloc(sizeof(int) * e->map_x);

The change is having the while look at e->map_y instead of e->map_x. The rest of the code is the same.

share|improve this answer
    
I prefer a for loop. That -1 gives me the creeps. – pmg Mar 1 '15 at 19:00
    
Sorry but it is for a school project we have a norm and I can't use for loops, by the way I already try to change map_y and map_x – hboy Mar 1 '15 at 19:14
1  
Change the value that the while is comparing. The code you have written is incorrect. – Jiminion Mar 1 '15 at 19:19
    
I already change the variable with a row number, 14 – hboy Mar 1 '15 at 19:23
1  
Or even (how the heck can you format code in comments?) i = 0; while (i < e->map_y) { e->map [i] = ...; i++} Makes the intent MUCH clearer, and that makes finding the problem easier. – jamesqf Mar 1 '15 at 19:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.