Thread: Program crashing when user orders more than 13 arrays.

  1. #1
    Novice.
    Join Date
    Oct 2005
    Posts
    88

    Program crashing when user orders more than 13 arrays.

    I´ve rewritten my array program:

    Code:
    #include <stdio.h>
    
    int main()
    
    {
    int a;
    int b;
    int c;
    int array[10];
    printf("Enter an amount of slots to produce: ");
    scanf("%d", &c); //Will request c, to which the other integers will be compared to see if the loop has to run another time.
    for (a = 0; a != c; a++) //This for loop generates numbers to put in the array.
        {
         array[a] = a;
        }
    
    for (b = 0; b != c; b++)//This for loop will print out every nummer which is present in the array.
           {
            printf("%d\n",array[b]);
           }
    
    getchar();
    getchar();
    return 0;
    }
    If you input more than 13 for c, it will crash. Does anyone know what the cause of this is, and how I can fix it. I'm using the Bloodshed Dev C++ compiler.

    Thank You,

    OmnificienT

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you input more than 13 for c, it will crash.
    Actually, I think that if you input more than 10 for c...

    Does anyone know what the cause of this is, and how I can fix it.
    With c > 10, the array would be accessed out of bounds. What you can do is check that c <= 10 and only then run the loops.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Yes, I notice it now. Only it will accept 13 or lower as a legal value. Strange.

    I've solved it this way:

    Code:
    #include <stdio.h>
    
    int main()
    
    {
    int a;
    int b;
    int c;
    int array[c];
    printf("Enter an amount of slots to produce: ");
    scanf("%d", &c); //Will request c, to which the other integers will be compared to see if the loop has to run another time.
    for (a = 0; a != c; a++) //This for loop generates numbers to put in the array.
        {
         array[a] = a;
        }
    
    for (b = 0; b != c; b++)//This for loop will print out every nummer which is present in the array.
           {
            printf("%d\n",array[b]);
           }
    
    getchar();
    getchar();
    return 0;
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You don't seem to understand the problem.
    You have 10 elements in the array and you try to assign values to 13 of them. How are you going to assign a value to something that doesn't exist? Think about it.

    Your second code is even worse. Variable c has an undefined value and you try to create an array from that. Clever. You should should ask the number of elements BEFORE you create the array. Besides that, I don't think your code will even compile seeing how old your compiler is. You can't allocate arrays like that, unless you use C99, which your compiler probably doesn't support. You need to change compiler and probably use dynamic allocation with malloc.

    Oh and indent properly. All code between each { and } should be indented once (yes, that includes the code inside main!).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You did not solve it. You are using variable length arrays, except that the length of the array is rather unknown since c was not initialised. This probably resulted in c being set to a sufficiently large garbage value, thus giving you the illusion that the out of bounds error was fixed. From what I see you do not want variable length arrays.

    I would suggest something along these lines:
    Code:
    #include <stdio.h>
    
    #define NUM_SLOTS 10
    
    int main(void)
    {
        int slots[NUM_SLOTS];
        int amount;
    
        /* Determine how many slots to produce. */
        printf("Enter an amount of slots to produce: ");
        scanf("&#37;d", &amount);
        if (amount >= 0 && amount <= NUM_SLOTS)
        {
            int i;
    
            /* Set the slots. */
            for (i = 0; i < amount; ++i)
            {
                slots[i] = i;
            }
    
            /* Print the slots. */
            for (i = 0; i < amount; ++i)
            {
                printf("%d\n", slots[i]);
            }
        }
        else
        {
            printf("Unable to produce %d slots.", amount);
        }
    
        getchar();
        getchar();
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    "You have 10 elements in the array and you try to assign values to 13 of them."

    Look, that was not the point. The point was that, with the original code you were able to input 13 without the program crashing. I do not know why, it just worked even though it's not supposed to.

    "You did not solve it."

    It seems I did, since my code works fine (is the syntax not right?), but if you'd be so kind please clarify what's not good about it. What do you mean by c not being initialised?

    Code:
    int c;
    int array[c];
    printf("Enter an amount of slots to produce: ");
    scanf("&#37;d", &c); //Will request c, to which the other integers will be compared to see if the loop has to run another time.
    I thought that
    Code:
    int c;
    would initialize an integer, named c. I did not give it a value, but I gave c a value later in
    Code:
     scanf("%d", &c);
    .

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by omnificient View Post
    Look, that was not the point. The point was that, with the original code you were able to input 13 without the program crashing. I do not know why, it just worked even though it's not supposed to.
    So you wanted a lesson in how memory works?
    The behavior was undefined. You did write memory beyond that of the array. Your program might own that memory, or it might not. It might belong to another variable, as well, in case you'd overwrite that memory. Your program might work or it might not. That's what we call undefined.

    "You did not solve it."

    I think I did, since my code works fine (is the syntax not right?), but if you'd be so kind please clarify what's not good about it. What do you mean by c not being initialised?
    You certainly didn't solve it.
    By uninitialized, it means that you did not give c a value at all. So it can contain any given value within its range, from 0 up to several million. And that value it just happens to contain is the range of your array.

    Code:
    int c;
    int array[c];
    printf("Enter an amount of slots to produce: ");
    scanf("%d", &c); //Will request c, to which the other integers will be compared to see if the loop has to run another time.
    I thought that
    Code:
    int c;
    would initialize an integer, named c. I did not give it a value, but I gave c a value later in
    Code:
     scanf("%d", &c);
    .
    You define or create the variable, yes, but you don't initialize it and you do not give it a value. You also create your array before you actually give it a value, because the scanf is after you create the array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Look, that was not the point. The point was that, with the original code you were able to input 13 without the program crashing. I do not know why, it just worked even though it's not supposed to.
    You just got lucky (or unlucky, as the case may be).

    I think I did, since my code works fine (is the syntax not right?), but if you'd be so kind please clarify what's not good about it.
    Ah, but just because your code works fine when you tested it does not mean it is correct.

    What do you mean by c not being initialised?
    Well, quick question. Assuming that the compiler supports variable length arrays, what is the length of the array x in the code below?
    Code:
    int n;
    int x[n];
    The best answer you can give me is n, since you do not know what is n. The reason you do not know is that n is not initialised. The value it takes is some garbage value.

    I did not give it a value, but I gave c a value later
    By then that is too late. I suggest avoiding variable length arrays entirely, as in my example code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Well okay then. The thing is that I do want the user to be able to influence the length of the array. I can't declare the array after the user has input c now can I? What would be a good way to modify the array length?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Dynamic allocation - malloc and free!
    Code:
    long length;
    char temp[300];
    int* myarray;
    
    printf("Enter length: ");
    fgets(temp, sizeof(temp), stdin);
    length = strotl(temp);
    
    myarray = malloc(length * sizeof(int));
    /* Do something with array */
    free(myarray);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    Code:
    printf("Enter length: ");
    fgets(temp, sizeof(temp), stdin);
    length = strotl(temp);

    arent you complicating the situation here... why in this world do you need to input a string and then convert it to a number????
    cant you rather simply nput the number itself......
    "i think the question was how to input from a user"

    hence the requirement

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    you also use calloc also to allocate contiguous memory space in the case of arrays...
    but remember to free to the memory space before exiting the code....

    remember to check the validity of num_terms before creating a memeory spce...
    and ELYSIA you forgot to check for the completion <successful> of malloc if i am right.....

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by ElemenT.usha View Post
    Code:
    printf("Enter length: ");
    fgets(temp, sizeof(temp), stdin);
    length = strotl(temp);
    arent you complicating the situation here... why in this world do you need to input a string and then convert it to a number????
    cant you rather simply nput the number itself......
    "i think the question was how to input from a user"

    hence the requirement
    I use fgets instead of scanf because the poor function leaves crap in the input buffer. Hence I don't need to use input buffer cleaning code or worry about the input buffer. That's why I like fgets. Simple as powerful.
    And I don't have to worry about clearing the buffer and crap if the user enters a non-integer.

    And the question was how to allocate an array with the desired number of elements, not how to get input. That was just a sample to collect the input from the user.

    Quote Originally Posted by ElemenT.usha View Post
    you also use calloc also to allocate contiguous memory space in the case of arrays...
    but remember to free to the memory space before exiting the code....
    Or malloc. But you're just echoing what I mentioned in the my last reply.

    remember to check the validity of num_terms before creating a memeory spce...
    and ELYSIA you forgot to check for the completion <successful> of malloc if i am right.....
    Yes, I left out error checking to keep it simple and make it less to type. I also didn't check if the user actually entered a number or not.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    I think things are becoming a bit too complicated here actually. But thanks anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Need help with 'C' Program (arrays)
    By thephreak6 in forum C Programming
    Replies: 1
    Last Post: 10-24-2002, 12:36 AM