Malloc 未定义行为 - 丢失数据
Malloc Undefined Behavior - Losing data
所以,我正在处理一些内存受限的应用程序,并且我有:
1 - 在垂直数据库上模拟 tables 的两个结构数组。其中一个只有密钥(1.5M 32 位整数密钥),另一个具有整数密钥和双有效负载(150k 元组)。然后动态分配的两个
2 - 2^15 个 64 位无符号整数数组
3 - 2^10 个 32 位无符号整数数组
而且我需要动态分配一个 32 位整数数组,我会在运行时知道它的大小。
问题是:我已经能够使用 malloc 分配这个数组,但是当我将值初始化为零时,它只订阅了 150k 元组的值 table。这意味着,我正在丢失数据。数据库研究人员可能发生的最糟糕的事情。
分配 "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);
分配有问题的数组
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;
因此,在这一点之后,table c_customer 的一半丢失,由零订阅。
我该怎么做才能避免这种情况?
编辑:结构定义:
/******** 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定义:
typedef struct CHT
{
uint64_t bitmap[CHT_BMP_SIZE];
bucket OHT[CHT_OHT_SIZE];
bucket *HT;
uint32_t tamHT;
} CHT;
而且s pretty much the function where it occurs. This is not a small application and I
一直专注于这个问题,以至于我现在无法正确思考(抱歉)。
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);
}
您可能离开了您分配的 cht->HT
数组的末尾。
bucket *HT;
...
...
cht->HT = malloc(sizeof(uint32_t)*cht->tamHT);
试试 sizeof(bucket)
。
所以,我正在处理一些内存受限的应用程序,并且我有:
1 - 在垂直数据库上模拟 tables 的两个结构数组。其中一个只有密钥(1.5M 32 位整数密钥),另一个具有整数密钥和双有效负载(150k 元组)。然后动态分配的两个
2 - 2^15 个 64 位无符号整数数组
3 - 2^10 个 32 位无符号整数数组
而且我需要动态分配一个 32 位整数数组,我会在运行时知道它的大小。
问题是:我已经能够使用 malloc 分配这个数组,但是当我将值初始化为零时,它只订阅了 150k 元组的值 table。这意味着,我正在丢失数据。数据库研究人员可能发生的最糟糕的事情。
分配 "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);
分配有问题的数组
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;
因此,在这一点之后,table c_customer 的一半丢失,由零订阅。
我该怎么做才能避免这种情况?
编辑:结构定义:
/******** 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定义:
typedef struct CHT
{
uint64_t bitmap[CHT_BMP_SIZE];
bucket OHT[CHT_OHT_SIZE];
bucket *HT;
uint32_t tamHT;
} CHT;
而且s pretty much the function where it occurs. This is not a small application and I
一直专注于这个问题,以至于我现在无法正确思考(抱歉)。
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);
}
您可能离开了您分配的 cht->HT
数组的末尾。
bucket *HT;
...
...
cht->HT = malloc(sizeof(uint32_t)*cht->tamHT);
试试 sizeof(bucket)
。