C - deallocate dynamically allocated array struct get 'Invalid next size error (fast)'

I am a bit rusty on C programming I am trying to dynamically allocate an array of structures and I get "Error in '. / A.out": free (): invalid next size (fast) ". I went through it and looked at other posts in stackoverflow related to this error condition It seems obvious that I am splitting memory somewhere, but I cannot see what I am doing wrong.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <ctype.h>
#include <math.h>
#include <getopt.h>

#define sampleSize 1

typedef struct
{
    double basicTime;
    double spacialTime;
    double temporalTime;
    double spacialTemporalTime;
} TimeEntry;

int main(int argc, char **argv)
{
     TimeEntry* timeThings;
    int i;
    time_t startTime;
    time_t stopTime;

    //Allocate an array for storing all of the timeThings
    timeThings = (TimeEntry*)malloc(sampleSize * sizeof(TimeEntry));

    for (i = 0; i < sampleSize; i++);
    {

        time(&startTime);
        sleep(1);
        time(&stopTime);
        timeThings[i].basicTime = difftime(stopTime, startTime);

        time(&startTime);
        sleep(1);
        time(&stopTime);
        timeThings[i].spacialTime = difftime(stopTime, startTime);

        time(&startTime);
        sleep(1);
        time(&stopTime);
        timeThings[i].spacialTemporalTime = difftime(stopTime, startTime);

        time(&startTime);
        sleep(1);
        time(&stopTime);
        timeThings[i].temporalTime = difftime(stopTime, startTime);
    }

    free(timeThings);

    return 0;
}

      

When I run this program, I get the following error:

*** Error in `./a.out': free(): invalid next size (fast): 0x0000000001a93010 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7aa16)[0x7fc7ef743a16]
/lib/x86_64-linux-gnu/libc.so.6(+0x7b793)[0x7fc7ef744793]
./a.out[0x4007b1]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0x7fc7ef6ea995]
./a.out[0x400559]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:06 24248357                           /home/dnhandy/Projects/a.out
00600000-00601000 rw-p 00000000 08:06 24248357                           /home/dnhandy/Projects/a.out
01a93000-01ab4000 rw-p 00000000 00:00 0                                  [heap]
7fc7ef4b3000-7fc7ef4c8000 r-xp 00000000 08:05 1843221                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7fc7ef4c8000-7fc7ef6c8000 ---p 00015000 08:05 1843221                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7fc7ef6c8000-7fc7ef6c9000 rw-p 00015000 08:05 1843221                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7fc7ef6c9000-7fc7ef86c000 r-xp 00000000 08:05 1846555                    /lib/x86_64-linux-gnu/libc-2.17.so
7fc7ef86c000-7fc7efa6b000 ---p 001a3000 08:05 1846555                    /lib/x86_64-linux-gnu/libc-2.17.so
7fc7efa6b000-7fc7efa6f000 r--p 001a2000 08:05 1846555                    /lib/x86_64-linux-gnu/libc-2.17.so
7fc7efa6f000-7fc7efa71000 rw-p 001a6000 08:05 1846555                    /lib/x86_64-linux-gnu/libc-2.17.so
7fc7efa71000-7fc7efa75000 rw-p 00000000 00:00 0 
7fc7efa75000-7fc7efa96000 r-xp 00000000 08:05 1846537                    /lib/x86_64-linux-gnu/ld-2.17.so
7fc7efc68000-7fc7efc6b000 rw-p 00000000 00:00 0 
7fc7efc93000-7fc7efc96000 rw-p 00000000 00:00 0 
7fc7efc96000-7fc7efc97000 r--p 00021000 08:05 1846537                    /lib/x86_64-linux-gnu/ld-2.17.so
7fc7efc97000-7fc7efc99000 rw-p 00022000 08:05 1846537                    /lib/x86_64-linux-gnu/ld-2.17.so
7ffffbf1f000-7ffffbf40000 rw-p 00000000 00:00 0                          [stack]
7ffffbffe000-7ffffc000000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted

      

+3


source to share


1 answer


for (i = 0; i <sampleSize; i ++); this loop makes i = 1 and you go to unallocated space. Remove ';' here



0


source







All Articles