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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Boardy
  • 35,417
  • 104
  • 256
  • 447
  • I can't see where you are incrementing `insertCount` in your `while` loop. Have I missed it? Also, could you add the code that does the query and the bit that sets `resultCount`, thanks – Jimbo Jun 26 '13 at 12:22
  • @Jimbo Sorry had a bit of a type, I've corrected the code – Boardy Jun 26 '13 at 12:45
  • No probs. Could you add the code that does the query and the bit that sets resultCount, thanks – Jimbo Jun 26 '13 at 12:56
  • @Jimbo I've added the code for the query and where resultCount is set – Boardy Jun 26 '13 at 13:02
  • `insertCount` initialised to zero? – Jimbo Jun 26 '13 at 13:16
  • @Jimbo, yea it is, its initialised at the top of the function using ``int insertCount = 0;`` as is resultCount also – Boardy Jun 26 '13 at 13:17
  • Sorry Boardy, can't spot anything. I noticed in the mysql help page their example checks their equivalent of `rowReport[i]` for `NULL`. Is it possible one of these could be NULL in your table? – Jimbo Jun 26 '13 at 13:21
  • @Jimbo, I have just added a check to make sure its not null but not made any difference. Thanks for the help though – Boardy Jun 26 '13 at 13:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32422/discussion-between-jimbo-and-boardy) – Jimbo Jun 26 '13 at 14:07

0 Answers0