Search code examples
cvisual-studiorandomvisual-studio-2017srand

visual studio 2017 srand() crashes (online compiler works)


I tried these 3 types of code.

1: Visual Studio 2017 Output: application crashed.

 time_t = t;
 srand((unsigned)time(&t)); 

2: Visual Studio 2017 Output: application crashed.

srand (time(NULL));

3: Visual Studio 2017 Output: normal print followed by a crash.

long t;
time(&t);
srand((unsigned)t);

The third variant output/error and build warning prints: Debug Error! Run-TimeCheck Failure #2 -Stack around the variable 't' was corrupted

warning C4333: 'function': incompatible types - from 'long *' to 'time_t *const' (tried to fix using time_t but the application just crash)

All examples works on online compiler!! onlinegdb.com. All the test were done with that code below.

#define _CRT_SECURE_NO_WARNINGS
#define CON 10

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



float maxB(float v[], int n);

float maxB(float v[], int n) {
    float est;

    for (int i = 0; i < n-1; i++)
    {
        printf("[%d]: %.2f\n", i, v[i]);
        if (v[i] >= v[i + 1]) {
            est = v[i];
        }
        else
        {
            est = v[i + 1];
        }
    }
    return est;
}

main() {

    srand(time(NULL));

    float v[CON];
    int a;
    printf("how many vector positions to test?\n");
    scanf("%d", &a);
    while (a < 0 || a > CON ){
        printf("invalid input!how many vector positions to test?\n");
        scanf("%d", &a);
    }

    for (int i = 0; i < CON; i++){
        v[i] = (float)(rand() % 10);
    }
    printf("Biggest number %f", maxB(v, a));
} 

So... what should i do to use random function with visual studio? Should i use another version?


Solution

  • Solved, the problem was the computer i was using, when i did the test with another machine, it worked!.