Home > Software engineering >  program doesen't return nothing if there isn't printf in for
program doesen't return nothing if there isn't printf in for

Time:04-05

I am trying to generate random arrays in my program. If in the for i put printf("%d", i); my programs run and take his time to printf all of the values, and then print "end", but if i comment the printf in the for, when i execute the program after 1-2 sec it end without giving any type of result back (no error, no printf).

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



//Costanti
static double E = 0.001; // Errore relativo massimo ammissibile

static int nMin = 100; // A = nMin
static int nMax = 5000000;

 double B;

double duration(struct timespec start, struct timespec end) {
    return end.tv_sec - start.tv_sec
          ((end.tv_nsec - start.tv_nsec) / (double)1000000000.0);
}
double getResolution() {
    struct timespec start, end;
    clock_gettime(CLOCK_MONOTONIC, &start);
    do {
        clock_gettime(CLOCK_MONOTONIC, &end);
    } while (duration(start, end) == 0.0);
    return duration(start, end);
}
int main() {

    // Inizializzazione variabili per il calcolo del tempo

    double tMin = getResolution() * ((1 / E)   1);
    B = exp((log(nMax) - log(nMin)) / 99);

    srand(time(NULL));

    // Generazione Input per l'algoritmo
    //struct timespec inizio, fine;

    //clock_gettime(CLOCK_MONOTONIC, &inizio);

    for (int j = 0; j < 100; j  ) {
        int n = nMin * pow(B,j);
        
        int array[n];

        for (int i = 0; i < n; i  ) {
            array[i] = rand();
            //printf("%d", i);
        }

    }
    //clock_gettime(CLOCK_MONOTONIC, &fine);

    //double quantodura = duration(inizio, fine);

    //printf("generation time: %f", quantodura);
    printf("ciao");
    return 0;
}

Even if I comment all of the struct timespec inizio,fine; clock_gettime ecc. it doesen't work

CodePudding user response:

It didn't return nothing. A program always returns an exit code. (It can be obtained using echo $? if using "sh".) I get 139, indicating a segmentation violation on my system. Using -fsanitize=address identifies a stack overflow as the cause.

AddressSanitizer:DEADLYSIGNAL
=================================================================
==1==ERROR: AddressSanitizer: stack-overflow on address 0x7fff91751728 (pc 0x00000040175f bp 0x7fff92031910 sp 0x7fff91751730 T0)
    #0 0x40175f in main /app/example.c:46
    #1 0x7f5eafe2c0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6 0x240b2)
    #2 0x40117d in _start (/app/output.s 0x40117d)

SUMMARY: AddressSanitizer: stack-overflow /app/example.c:46 in main
==1==ABORTING

Line 46 is int array[n];. This line is creating ever bigger arrays. Eventually, the array to create is so large that it can't be accommodated by the stack. (This happened when n was 2,326,588 in my test. B was 1.115,487, and j was 92.) You'll need to allocate such large arrays on the heap (e.g. using malloc) instead of the stack.

  • Related