-2

I'm trying to assign random numbers between a certain amount and assign them to an array. My current method of doing that involves for loops but when I run the program after compiling, my computer just freezes and there are no errors by gcc so I can't figure out what the issue is.

cl_Zone **SetUp_Zones(){
/*Keep within x = 1-100, y = 1-50 */
/*room_Create(x, y, max_Y, max_X)*/

    cl_Zone **array_Zones;
    array_Zones = malloc(sizeof(cl_Zone)*9);
    int array_Zone_Select[9];

    int counter; int counter2; int x;
    int max_active_zones; int active_zone;
    x = 0;

    max_active_zones = rand() % 4 + 6;
    /*need something to designate zones as active up to the active_zones amount*/
    for (counter = 0; counter < max_active_zones; counter++){

        active_zone = rand() % 1 + 9;

        /*if first instance, just assign to array*/
        if (counter == 0){array_Zone_Select[counter] = active_zone;}

        /*else check if active zone already taken in array_zone_select*/
        else{
            for (counter2 = 0; counter2 < counter; counter2++){
                if(array_Zone_Select[counter2] == counter){x++;}
            }

        }
        /*if already taken, redo loop, otherwise assign*/
        if(x > 0){
        counter--;
        x = 0;
        }
        else{array_Zone_Select[counter] = active_zone;}
    };

    x = 1;
    for (counter = 0; counter < 9; counter+3){
        array_Zones[counter] = zone_Create(1,x,16,33);
        array_Zones[counter + 1] = zone_Create(34,x,16,33);
        array_Zones[counter + 2] = zone_Create(67,x,16,33);
        x = x + 16;
    }


}

cl_Zone *zone_Create(int x, int y, int max_X, int max_Y){
    cl_Zone *p_Zone;
    p_Zone = malloc(sizeof(cl_Zone));

    p_Zone->Position.x = x;
    p_Zone->Position.y = y;
    p_Zone->max_Y = max_Y;
    p_Zone->max_X = max_X;

    return p_Zone;
    }
ayy llama
  • 1
  • 2
  • I haven't looked at this question that was posted previously yet which might be relevant: https://stackoverflow.com/questions/47700193/c-random-numbers-array-function, However I am also just curious what is causing the code to freeze in addition to learning how to assign random numbers to an array. – ayy llama Sep 19 '20 at 20:48
  • `array_Zones = malloc(sizeof(cl_Zone)*9);` wrong (but almost certainly not the problem and very subtle). – EOF Sep 19 '20 at 20:51
  • 1
    What do you expect `rand() % 1 + 9` to do? – EOF Sep 19 '20 at 20:55
  • And finally `p_Zone = malloc(sizeof(cl_Zone))` is wrong (exactly like `array_Zones = malloc(sizeof(cl_Zone)*9)`, but here the error *will* be relevant). – EOF Sep 19 '20 at 20:57
  • `if(array_Zone_Select[counter2] == counter)` also seems wrong. – 1201ProgramAlarm Sep 19 '20 at 21:05
  • Does rand() % 1 + 9 provide a random number from 1 to 9? I have a separate function running that does srand(time(NULL)); based on the time.h header file. According to this website (https://www.tutorialspoint.com/rand-and-srand-in-c-cplusplus): "Like we want to generate a random number between 1-6 then we use this function like − Num = rand() % 6 + 1;" Oh hey it's backwards, I guess that was it.. – ayy llama Sep 19 '20 at 21:35

1 Answers1

2

there are no errors by gcc so I can't figure out what the issue is.

Unfortunately, C compilers will accept piles of incorrect code without producing an error. Compiler warnings are very useful and will warn you about a great many problems, but they're generally off by default. Be sure to turn them on.

However, it's not as simple as -Wall to turn on all warnings; nothing in C is simple nor straightforward. You have to come up with a mix of warning flags that suit you. Here's a good start to catch many common mistakes.

gcc -Wall -Wshadow -Wwrite-strings -Wextra -Wconversion -std=c11 -pedantic

You can look up what they mean in the docs and add your own to the mix.


for (counter = 0; counter < 9; counter+3){ is an infinite loop which will suck down as much CPU as your operating system will allow. You probably meant for (counter = 0; counter < 9; counter+=3){. I'm still surprised that would make your computer freeze.


cl_Zone **SetUp_Zones fails to return anything. It should return array_Zones;.


array_Zones = malloc(sizeof(cl_Zone)*9); is incorrectly allocating too much memory. That would be if you're storing 9 cl_Zones. But cl_Zone **array_Zones says you're storing 9 pointers to cl_Zone. Instead you need array_Zones = malloc(sizeof(cl_Zone*) * 9);


active_zone = rand() % 1 + 9; will always produce 9. Anything % 1 is 0, so this is just active_zone = 0 + 9;.

Schwern
  • 127,817
  • 21
  • 150
  • 290
  • I'm trying to get the hang of pointers and I haven't done too much with C in the past and only a little bit of VBA so this is all very helpful – ayy llama Sep 19 '20 at 21:34
  • I think the faulty `for` loop (`for (counter = 0; counter < 9; counter+3){ `) which does not increment the loop counter is the primary issue. I'd probably put that at the top of the answer and then add the rest. – Jonathan Leffler Sep 20 '20 at 03:40