0

So I initialize my array like this:

    struct LevelPoint list[30];

This is LevelPoint:

typedef struct LevelPoint {
    Material ground_material;
    int ground_height;
} LevelPoint;

I have another list points:

LevelPoint **points;

I then use a for loop to add items to points:

for(int j =0;j<level->width;j++){
        for(int i = 0;i< level->height;i++){
            //bitsOmzetter return a value from 0-3
            k = bitsOmzetter(&l,ptr,&w);
            if(k == 3){
                if(i==0){
                    list[i] = level->points[j-1][level->height-1];
                }else{
                    list[i] = list[i-1];
                }
            }else{
                //level ->points[j] ->ground_material = k;
                //level -> points[j] ->ground_height =  bitsOmzetter(&j,ptr,&w)+1;
                LevelPoint point;
                point.ground_material = k;
                point.ground_height = bitsOmzetter(&l,ptr,&w)+1;;
                list[i] = point;
                //list[j][i] -> ground_material =  k;
                //list[j][i] -> ground_height =  bitsOmzetter(&j,ptr,&w)+1;
            }
        } 
        level->points[j] = list;
    }

but this always gives me an error of:

==12788==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x559c5edaa671 bp 0x7ffda2a59db0 sp 0x7ffda2a57ce0 T0)
==12788==The signal is caused by a WRITE memory access.
==12788==Hint: address points to the zero page.
    #0 0x559c5edaa671 in levelloader_load_binary_level /home/student/Desktop/Systeemprogrammeren2022-project-groep-22/game/level/c_levelloader.c:66

line 66 is level->points[j] = list;. I tried debugging but that shows that my list is empty : list[0] is all it shows.

I tried initialzing the array in other ways but it always failed.

bott
  • 13
  • 4
  • 3
    Where did you `malloc` `level->points`? – Elliott Frisch Nov 06 '22 at 15:05
  • In the struct level. – bott Nov 06 '22 at 15:08
  • Why aren't you allocating `list` each time you need a new one (each time through your loop)? Maybe that's part of your problem? Though also, what do you want to happen the first time through your loops? (i=j=0) – Dan Getz Nov 06 '22 at 15:10
  • I tried already, it doesn't work either. – bott Nov 06 '22 at 15:13
  • @DanGetz the loop just fills the list with LevelPoints (at least it should but it doesn't). – bott Nov 06 '22 at 15:14
  • I meant to say, you have a bug if i and j are zero, but k is 3. But what does "it doesn't work either" mean? If you don't allocate new lists there's no possible way your code could do what you want it to do. – Dan Getz Nov 06 '22 at 15:20
  • @DanGetz that won't ever be a problem because the function is structured in a way that the k will never be 3 when i=j=0. And to add to your point, I allocated the list in the for loop as well but I still get the same problem. – bott Nov 06 '22 at 15:22
  • Well, I don't see it in your code. If your question doesn't have your code in it, you can [edit] to add it. You should also add the code that allocated `level->points`. – Dan Getz Nov 06 '22 at 15:26
  • 1
    You're trying to write through a null pointer. Given the contents of the line, most likely the problem is that `level->points == NULL`. The problem is in the code that should have allocated some memory for `level->points`, which you didn't post. Always post **complete** code that reproduces the problem. – Gilles 'SO- stop being evil' Nov 06 '22 at 15:52
  • 1
    *Assigning* this array `struct LevelPoint list[30];` (as in `level->points[j] = list;`) to anything is a [lifetime](https://en.cppreference.com/w/c/language/lifetime) issue waiting to happen, i.e. probable [undefined behaviour](https://en.cppreference.com/w/c/language/behavior) (unless `list` is defined at file scope). Arrays [decay](https://stackoverflow.com/q/1461432/2505965) to a pointer to their first element - assigning an array does not create a copy of the entire array, only a copy of this pointer value. – Oka Nov 06 '22 at 16:47

0 Answers0