Storing value in int array from mysql of type mediumint

0

I am new to C and working on a project at the moment but I am having an issue.

I have a int array which is defined as below:

int* SwitchIDs = malloc(sizeof(int));
int* SeizeUTC = malloc(sizeof(int));
int* CorrelationIDs = malloc(sizeof(int));

I run a query which retrieves values from the database, these values are something like below

 SwitchID | CorrelationID | SeizeUTC   |
+----------+---------------+------------+
|    14356 |      10355062 | 1305534091 |
|    14356 |      10366006 | 1305536411 |
|    14354 |      12283158 | 1305537718 |

In the database, the SwitchID is a mediumint(8) unsigned, the CorrelationID is of type int(10) unsigned and the SeizeUTC is of type int(10) unsigned.

This query that retrieves the results is executed several times so different values returned which need to be added to each of the arrays.

Once the query has executed I get the value of how many rows were returned and then I realloc each array as below and once realloc I then try adding the data from the database into each array as below. All of this is also done after checking that the result count > 0 Below is the code for the query and insert the data into the arrays.

sqlLen = asprintf(&sql, "SELECT SwitchID, CorrelationID, SeizeUTC FROM TblCallLog "
                "USE INDEX(indCalled) WHERE SeizeUTC BETWEEN %lu AND %lu AND CalledNumberID='%i'"
                "AND Direction = 1 %s",
                DateToUTC(reportParameterArray[P_DATESTART], *(int*)&reportParameterArray[P_TIMESTART]), 
                DateToUTC(reportParameterArray[P_DATEEND], *(int*)&reportParameterArray[P_TIMEEND]),
                NumberIDs[i], reportRestrictions->PResultContraint);

 if ((mysql_real_query(HandleDB, sql, sqlLen))) return 1;
        resultReport = mysql_store_result(HandleDB);
        resultCount = mysql_num_rows(resultReport);
        printf("***Result Count: %i***\n", resultCount);
        if (resultCount != 0)
        {
                SwitchIDs = realloc(SwitchIDs, resultCount*sizeof(int));
                CorrelationIDs = realloc(CorrelationIDs, resultCount*sizeof(int));
                SeizeUTC = realloc(SeizeUTC, resultCount*sizeof(int)); 

    while((rowReport = mysql_fetch_row(resultReport)))
            {
                SwitchIDs[insertCount] = atoi(rowReport[0]);
                CorrelationIDs[insertCount] = atoi(rowReport[1]);
                SeizeUTC[insertCount] = atoi(rowReport[2]);
                            printf("SwitchID[%i]: %i\n", insertCount, SwitchIDs[insertCount]);
                printf("CorrelationIDs[%i]: %i\n", insertCount, CorrelationIDs[insertCount]);
                printf("SeizeUTC[%i]: %i\n\n", insertCount, SeizeUTC[insertCount]);
                insertCount++;
            }
}

This seems to work for the first 5 inserts but then when it loops for the 6th time, it core dumps with a seg fault.

The stacktrace contains the following:

#0  0x400f24de in malloc_consolidate () from /lib/tls/libc.so.6
(gdb) bt
#0  0x400f24de in malloc_consolidate () from /lib/tls/libc.so.6
#1  0x00000038 in ?? ()
#2  0x401a88d8 in main_arena () from /lib/tls/libc.so.6
#3  0x080646c0 in ?? ()
#4  0x401a88c4 in main_arena () from /lib/tls/libc.so.6
#5  0x401a88a8 in main_arena () from /lib/tls/libc.so.6
#6  0x401a88a0 in __malloc_initialize_hook () from /lib/tls/libc.so.6
#7  0x401a7f0c in __elf_set___libc_thread_subfreeres_element___rpc_thread_destroy__ () from /lib/tls/libc.so.6
#8  0x401a88a0 in __malloc_initialize_hook () from /lib/tls/libc.so.6
#9  0xbfffeb04 in ?? ()
#10 0xbfffe474 in ?? ()
#11 0x400f4117 in _int_malloc () from /lib/tls/libc.so.6
Previous frame inner to this frame (corrupt stack?)

I'm guessing this is because the mysql database is of type mediumint but I when I realloc I am saying sizeof(int) so its not big enough for the number and crashing.

I'm not sure how I can fix this as C doesn't have any concept of mediumint, at least, not as far as I am aware of or been able to find.

c
arrays
malloc
realloc
asked on Stack Overflow Jun 26, 2013 by Boardy • edited Apr 21, 2018 by halfer

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0