Hi

I have a problem with using free() which causes a "Segmentation Fault".
What I am trying to do is:
1) Save string from the struct into maloced memory
2) Save the size of each string (incl. \0)

Later not every string from the struct should be saved but for testing I made it easier. Also the strings in partlist are assigned to the struct by a file later.
The problem occurs when num reaches an amount of 7. I have marked the positon where the error occurs in the code.

OS: Unix/Linux

So here is my code:
Code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

typedef struct{
    char kind[30];
    char ident[30];
    char desc[100];
    char detail[100];
    char distri[30];
    char ordnr[30];
    char price[4];
    char num[4];
    char mount[30];
    char pdf[100];

} PARTS;

int main(int args, char* argv[])
{
    PARTS partlist[13];

    strcpy( partlist[0].kind , "1st" );
    strcpy( partlist[1].kind , "2nd" );
    strcpy( partlist[2].kind , "3rd" );
    strcpy( partlist[3].kind , "4th" );
    strcpy( partlist[4].kind , "5th" );
    strcpy( partlist[5].kind , "6th" );
    strcpy( partlist[6].kind , "7th" );
    strcpy( partlist[7].kind , "8th" );
    strcpy( partlist[8].kind , "9th" );
    strcpy( partlist[9].kind , "10th" );
    strcpy( partlist[10].kind , "11th" );
    strcpy( partlist[11].kind , "12th" );
    strcpy( partlist[12].kind , "13th" );

    int i;
    int n;
    int num = 1;

    char *kind = (char *) malloc( strlen( partlist[0].kind ) +1 );
    int *size = (int*) malloc( 1 );

    strcpy( kind , partlist[0].kind );
    *size = strlen( partlist[0].kind ) +1;

    for( i=1 ; i<13 ; i++ ){
        //if( ... ){ //will be implemented later but is the reason for using num
            int mal_size = 0;
            num++;

            printf("%d.1\tnum: %d\n",i ,num);

            int *size_tmp = (int *) malloc( num );
            if( size_tmp == NULL )
                printf("size_tmp = NULL!\n");

            for( n=0 ; n<num-1 ; n++)
                size_tmp[n] = size[n];
            size_tmp[num-1] = strlen( partlist[i].kind ) +1;

            free( size );
            size = NULL;
            size = (int *) malloc( num );

            for( n=0 ; n<num ; n++ ){
                size[n] = size_tmp[n];
            }

            free( size_tmp );//SEGMENTATION FAULT!!!!  @num=7
            size_tmp = NULL;

            printf("%d\n",num);

            for( n=0 ; n<num ; n++)
                mal_size += size[n];

            char *kind_tmp = (char *) malloc( mal_size );
            memcpy( kind_tmp , kind , mal_size - size[num-1] );
            memcpy( kind_tmp + mal_size - size[num-1] , partlist[i].kind , size[num-1] );

            free( kind );
            kind = NULL;
            kind = (char *) malloc( mal_size );

            memcpy( kind , kind_tmp , mal_size );
            free( kind_tmp );
            kind_tmp = NULL;
        //}END OF if
    }

    free( kind );
    free( size );

    return( 0 );
}
I hope somebody could help me!

Kind regards
michael