Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8013381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:37:07+00:00 2026-06-04T19:37:07+00:00

I have a simple test program in C to scramble an array of values

  • 0

I have a simple test program in C to scramble an array of values on the heap. Sidenote: I know the random logic here has a flaw that will not allow the “displaced” value to exceed RAND_MAX, but that is not the point of this post.

The point is that when I run the code with N = 10000, every once in a while it will crash with very little information (screenshots posted below). I’m using MinGW compiler. I can’t seem to reproduce the crash for lower or higher N values (1000 or 100000 for example).

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

const int N = 10000;

int main() {
    int i, rand1, rand2, temp, *values;

    /* allocate values on heap and initialize */
    values = malloc(N * sizeof(int));
    for (i = 0; i < N; i++) {
        values[i] = i + 1;
    }

    /* scramble */
    srand(time(NULL));
    for (i = 0; i < N/10; i++) {
        rand1 = (int)(N*((double)rand()/(double)RAND_MAX));
        rand2 = (int)(N*((double)rand()/(double)RAND_MAX));
        temp = values[rand1];
        values[rand1] = values[rand2];
        values[rand2] = temp;
    }


    int displaced = 0;
    for (i = 0; i < N; i++) {
        if (values[i] != (i+1)) {
            displaced++;
        }
    }
    printf("%d numbers out of order\n", displaced);

    free(values);
    return 0;
}

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:37:08+00:00Added an answer on June 4, 2026 at 7:37 pm

    it may be because rand() generates a random number from 0 to RAND_MAX inclusive so (int)(N*((double)rand()/(double)RAND_MAX)) can be N, which exceeds the array boundary. however, i don’t see why that would vary with array size (it does explain why it only crashes sometimes, though).

    try /(1+(double)RAND_MAX) (note that addition is to the double, to avoid overflow, depending on the value of RAND_MAX) (although i’m not convinced that will always work, depending on the types involved. it would be safer to test for N and try again).

    also, learn to use a tool from Is there a good Valgrind substitute for Windows? – they make this kind of thing easy to fix (they tell you exactly what went wrong when you run your program).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple test program, running on Solaris 5.8: #include <stdio.h> #include
I have a simple RabbitMQ test program randomly enqueuing messages, and another reading them,
I have a simple unit test case (extensive question here ) on a configuration
I have a strange build problem. I have a simple test program that sends
I have a simple test program that when I run I get: ./hello: error
I have a simple test program which is designed to consume the Apache Felix
I have a fairly simple program based largely on a simple bluetooth test client
I have made a simple, test program to experiment using classes. It creates a
I have been trying to get a simple networking test program to run with
I have created a simple WPF test program, based on what Visual Studio gives

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.