Thread: long problem

  1. #16
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    oops. my fault. lack of attention, as usual.
    you had
    Code:
    result[ires-1]++;
    which does not overflow the array.
    my apologies.

  2. #17
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by valenok
    yes you did. maximum index you had was 12.
    for array of size 12 maximum index is 11.


    either declare it static, which implicitely zeroes the array,
    or call memset().

    ires is decremented by one so i wouldnt write out of bounds.
    Code:
    result[ires-1]++;

  3. #18
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by valenok
    oops. my fault. lack of attention, as usual.
    you had
    Code:
    result[ires-1]++;
    which does not overflow the array.
    my apologies.

    NM that, thnks for the help

  4. #19
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by loko
    NM that)
    I am not able to reproduce your problem, since I don't have TurboC. However, why don't you try to get to the bottom of things yourself for your particular compiler? Make the program tell you what is really happining. I claim that debugging your own stuff is faster than posting a request for help and waiting for a helpful response. You also get to learn a really, really, really important part of program implementation: debugging.

    For example, you could do something like this:

    Code:
    #define TOTAL_ROLL 10
    ..
    ..
    ..
            dice1 = ( rand() % 6 ) + 1;
            dice2 = ( rand() % 6 ) + 1;
            printf("rollnum = %d: die1 = %d, die2 = %d\n", rollnum, dice1, dice2);
    Now what happens?

    Regards,

    Dave
    Last edited by Dave Evans; 07-21-2005 at 08:32 AM.

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by valenok
    1. you did not initialize result array properly
    No. You did not initialize the array properly.
    Quote Originally Posted by valenok
    This is better code:
    [code]
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>

    #define NITER 36000
    #define NRES 12

    int main(void)
    {
    long result[NRES];
    See? They did it correctly, and when you rewrote it, you broke it:
    Quote Originally Posted by loko
    PHP Code:
    #include<stdio.h>
    #include<time.h>
    #include<stdlib.h>

    #define TOTAL_ROLL 36000

    int main()
    {
        
    long result[12] = { }; 
    That was correctly initializes the array to all zeros. An array with any members initialized at the time of declaration initializes all elements not specificly initialized to zero.
    Quote Originally Posted by valenok
    either declare it static, which implicitely zeroes the array,
    or call memset().
    Or just do what they did in the first place.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    Just re-read C99, section 6.7.8.12 states:
    If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
    That proves that I was wrong saying that array was not initialized.
    Still, quzah, you're wrong saying I did not initialize the array.
    I did it by calling memset().

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So you did.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #23
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    Ok, I already know I'm going to regret this for one reason or another... The initialization is OK, not that it needs initialization, at least in the code that follows. I tried to take the OP's code and minimally change it to what 'I think' the OP wanted to do. If not I hope you'll set me straight and tell me about the 36000 iteration for loop

    Regards,
    Brian

    Code:
    #include<stdio.h>
    #include<time.h>
    #include<stdlib.h>
    
    #define NUM_ROLLS   12
    
    int main()
    {
        long result[NUM_ROLLS] = { 0 };
        long rollnum = 0, ipos;
        int dice1, dice2, ires;
    
        srand( time( 0 ) );
    
        for( rollnum = 0; rollnum < NUM_ROLLS; rollnum++ ){
            dice1 = ( rand() % 6 ) + 1;
            dice2 = ( rand() % 6 ) + 1;
            ires = dice1 + dice2;
            result[rollnum] = ires;
        }
    
        for( ipos = 0; ipos < NUM_ROLLS; ipos++ ){
            printf( "(%d) Total: %d\n", ipos+1, result[ipos] );
        }
    
        getchar();
        return 0;
    }

  9. #24
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by loko
    2. Whats the proper way to initialize an array?
    I almost always use this:

    long array[12] = {0};

    much cleaner than memset IMO.

    Also if you have this:

    Code:
    long *array[12]; // array of pointers
    memset(array, 0, sizeof array);
    it yields undefined behaviour

    but long *array[12] = {0}; doesn't

  10. #25
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    Laserve, how comes that memset leads() to an undefined behaviour?
    Make it clear please.

  11. #26
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It isn't undefined.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #27
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >how comes that memset leads() to ...
    The all-bits-zero pointer values may not be null pointers.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #28
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That still doesn't mean it's 'undefiend' though.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #29
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    note that laserve himself invented array of pointers to long.
    it was array of long-s.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: double free or corruption
    By dsc in forum C Programming
    Replies: 3
    Last Post: 04-03-2008, 09:26 AM
  2. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  3. Knight's Tour Recursion Problem
    By thephreak6 in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2003, 09:18 AM
  4. Peculiar Problem with Printf
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-02-2002, 12:34 AM
  5. Peculiar Problem with Printf
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 07-02-2002, 12:03 AM