-3

While answeing this question I tried to run this code (for generating random numbers in a given range);

#include<stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>

int main()
{

    int max_range, i = 0, rand_num;
    bool digit_seen[max_range + 1]; // VLAs For C99 only 

    srand((unsigned)time(NULL));
    printf("Enter your maximum of range: ");
    scanf("%d", &max_range);

    for (int i = 1; i <= max_range; i++)
        digit_seen[i] = false;  

    for (;;)
    {  
         rand_num = rand() % max_range + 1;
         if(rand_num !=3)
             if(!digit_seen[rand_num])
             {
                printf("%d ", rand_num);
                digit_seen[rand_num] = true;
                i++;
             }
         if( i == (max_range - 1) )
             exit(0);
    }    
    return 0;
}

It worked fine upto a max_range of 47 but after that I got this error on debugging

enter image description here

Any idea why I am getting this?

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
  • 3
    When you declare `bool digit_seen[max_range + 1];` a value for `max_range` isn't defined yet. So `digit_seen` has an unknown amount of space allocated to it. – lurker Sep 18 '13 at 19:59

2 Answers2

5

You are creating an array based on the uninitialized value of max_range:

bool digit_seen[max_range + 1]; // VLAs For C99 only 

That could have any size imaginable. Why it works for some values and doesn't work for others is part of the magic of undefined behavior; anything can happen.

haccks
  • 104,019
  • 25
  • 176
  • 264
Chris Hayes
  • 11,471
  • 4
  • 32
  • 47
  • 4
    @haccks I don't understand why you made the edit you did here. Italicizing a term seems to be the very definition of a *too minor* edit. – Chris Hayes Jan 10 '14 at 20:55
  • To be honest, I wanted to delete this question. For that, first I unaccepted the accepted answer (I don't know whether that was your's or not) and removed my upvotes and then tried to delete, but can't be able to do that. (I suspect that this edit might be for removing the up vote). My apologies for that. – haccks Jan 10 '14 at 21:11
4

When you declare bool digit_seen[max_range + 1]; a value for max_range isn't defined yet. So digit_seen has an unknown amount of space allocated to it.

You could do it this way (using your existing code, and assuming a C99 compliant compiler):

int main()
{
    int max_range, i = 0, rand_num;

    srand((unsigned)time(NULL));
    printf("Enter your maximum of range: ");
    scanf("%d", &max_range);

    if (max_range <= 0) {
        // probably want to scold the user here and exit
    }

    bool digit_seen[max_range + 1]; // VLAs For C99 only 
    ...

Alternatively, you could use dynamic memory allocation:

int main()
{
    int max_range, i = 0, rand_num;
    bool *digit_seen;

    srand((unsigned)time(NULL));
    printf("Enter your maximum of range: ");
    scanf("%d", &max_range);

    if (max_range <= 0) {
        // probably want to scold the user here and exit
    }

    digit_seen = malloc(max_range+1); 
    ...

    // use *digit_seen or digit_seen[i], where i >= 0 and <= max_range

    ...
    free(digit_seen);
lurker
  • 56,987
  • 9
  • 69
  • 103