carraysmallocdynamic-allocation

Malloc Undefined Behavior - Losing data


So, I'm working with some memory bound applications and I have:

1 - Two arrays of structs that simulates tables on a vertical database. One of them just with keys (1.5M 32-bits integer keys) and another one with integer keys and double payloads (150k tuples). The two of then dynamically allocated

2 - An array of 2^15 64-bits unsigned integers

3 - An array of 2^10 32-bits unsigned integers

And I need to allocate dynamically an array of 32-bits integers which I will know the size just on runtime.

The problem is: I've been able to allocate this array using malloc, BUT when I initialize values to zero, it just subscribes the values of the 150k tuples table. Which means, I`m losing data. The worst thing that could happen to a databases researcher.

Allocation of the "tables"

tamCustomer = countLines("customer.tbl");
c_customer = malloc(tamCustomer*sizeof(column_customer));
readCustomerColumn("customer.tbl", c_customer);

tamOrders = countLines("orders.tbl");
c_orders = malloc(tamOrders*sizeof(column_orders));
readOrdersColumn("orders.tbl", c_orders, sel);

Allocation of the problematic array

cht->tamHT = actualPopCounter;
cht->HT = malloc(sizeof(uint32_t)*cht->tamHT); 
if (cht->HT == NULL) 
       printf("deu merda\n");
for (int i=0; i<cht->tamHT; i++) 
       cht->HT[i] = 0; 

So, after this point, half of the table c_customer gets lost, subscribed by zeros.

What can I do to avoid that?

EDIT: structs definitions:

/******** VETOR DE STRUCTS COLUMN CUSTOMER *********/
typedef struct customer_c
{
    unsigned int C_CUSTKEY;
    float C_ACCTBAL;
} column_customer;

column_customer *c_customer;

/******** VETOR DE STRUCTS COLUMN ORDERS ***********/
typedef struct orders_c
{
    unsigned int O_CUSTKEY;
} column_orders;

column_orders *c_orders;

CHT definition:

typedef struct CHT
{
    uint64_t bitmap[CHT_BMP_SIZE];
    bucket OHT[CHT_OHT_SIZE];
    bucket *HT;
    uint32_t tamHT;
} CHT;

And thats pretty much the function where it occurs. This is not a small application and Ive been so focused on this problem that I can`t think properly right now (sorry).

inline void generateCHT(column_customer *c_customer, int tamCustomer, CHT * cht)
{
    uint32_t ohtOcc=0;
    uint32_t chtOcc=0;
    uint32_t ohtOccBMP=0;
    uint32_t chtOccBMP=0;

    uint64_t actualPopCounter;
    uint64_t oldPopCounter;

    //Allocate CHT
    cht->tamHT = 0;

    //Initialize OHT and bitmap
    for (int i=0; i<CHT_OHT_SIZE;i++)
    {
        cht->OHT[i]=0;
        cht->bitmap[i]=0;
    }

    for (int i=0; i<tamCustomer; i++)
    {
        switch (chtInsertBitmap(c_customer[i].C_CUSTKEY, tamCustomer, cht))
        {
            case 0:
                printf("ERROR: Something went wrong while inserting the key %u on the CHT\n", c_customer[i].C_CUSTKEY);
                break;
            case 1:
                chtOccBMP++;
                break;
            case 2:
                ohtOccBMP++;
                break;
        }
    }

    //count Population
    actualPopCounter = 0;
    for (int i=0; i<CHT_BMP_SIZE;i++)
    {
        oldPopCounter = popCount(cht->bitmap[i]>>32);
        cht->bitmap[i] = cht->bitmap[i] | actualPopCounter;
        actualPopCounter = actualPopCounter + oldPopCounter;
    }

    cht->tamHT = actualPopCounter;

    cht->HT = malloc(sizeof(uint32_t)*cht->tamHT);
    if (cht->HT == NULL)
        printf("deu merda\n");

    for (int i=0; i<cht->tamHT; i++)
        cht->HT[i] = 0;

    for (int i=0; i<tamCustomer; i++)
    {
        if (chtInsertConciseTable(c_customer[i].C_CUSTKEY, cht, tamCustomer) == 0)
            ohtOcc++;
        else
            chtOcc++;
    }
    printf("OHT has %d occupied buckets and %d on the bitmap \n", ohtOcc, ohtOccBMP);
    printf("CHT has %d occupied buckets and %d on the bitmap \n", chtOcc, chtOccBMP);
}

Solution

  • You're possibly walking off the end of the cht->HT array you allocated.

    bucket *HT;
    
    ...
    ...
    
    cht->HT = malloc(sizeof(uint32_t)*cht->tamHT);
    

    Try sizeof(bucket) instead.