C段故障 Raspberry Pi2
C segmentation fault Raspberry Pi2
我在嵌入式平台上使用 C 相当有经验,但我并没有在 OS 中使用 C。我目前正在研究 Raspberry Pi 2.
我正在使用 C 语言工作,我需要制作一个实用程序,用于从二进制文件的一部分创建 CSV 文件。二进制文件包含许多小时的数据,格式为一系列 'blocks',每个包含约 2000 毫秒的数据。程序是遍历每个block,拉取数据,直到到达结束时间。
当我尝试进行相对较小的二进制到 csv 的转换时,该程序可以正常工作,但没有理由不能处理我可以识别的较大的转换。当我用 MAX_TIME_SAMPLE_TO_CONVERT 将程序 运行 设置为 180000 时,正常 运行 和 valgrind 都没有问题。当我将 "MAX_TIME_SAMPLE_TO_CONVERT" 更改为 200000 时,即出现分段错误。这只需要 60kB 内存的 malloc,这应该是轻而易举的事。当我从命令行 运行 "free" 时,我有超过 500MB 的可用空间。
当我 运行 valgrind 时,我得到了一个有点神秘的输出,但它告诉我遇到问题的行号(我正在使用 -g 选项构建)并且这些行号是正是使用 malloc 的变量:
/* save the samples into the arrays
* that will become the CSV files */
int32_t time = 0;
for(int j = 0; (j < numOfSamples) && (time < endTime); j++){
time = networkTime + (j * SAMPLE_INTERVAL_MS);
timeArray[sampleIndex] = time;
sampleArray[sampleIndex] = uncompressedBlockDataArray[j];
sampleIndex++;
}
完整代码:
int32_t startTime = getStartTime(argc, argv);
int32_t endTime = getEndTime(argc, argv);
/* calculate the amount of memory required to construct each array,
* limiting the maximum amount in order to conserve memory */
uint32_t timeWindow = endTime - startTime;
if(verbose)
printf("time window: %dms\n", timeWindow);
if(timeWindow > MAX_SAMPLE_TIME_TO_CONVERT){
timeWindow = MAX_SAMPLE_TIME_TO_CONVERT;
endTime = startTime + MAX_SAMPLE_TIME_TO_CONVERT;
if(verbose){
printf("warning: specified time window results in too many samples\n");
printf("\ttime window truncated to %dms\n", timeWindow);
printf("\tnew end time: %d\n", endTime);
}
}
uint32_t numOfSamples = timeWindow/SAMPLE_INTERVAL_MS;
if(verbose)
printf("each CSV file will contain up to %d samples (maximum of %dms)\n", numOfSamples, MAX_SAMPLE_TIME_TO_CONVERT);
/* allocate memory to temporarily store the
* data from the binary file as it is read */
int32_t *timeArray = (int32_t *)malloc(sizeof(uint32_t) * numOfSamples);
uint16_t *sampleArray= (uint16_t *)malloc(sizeof(uint16_t) * numOfSamples);
if(verbose)
printf("Allocating %d bytes for time and %d for samples\n", sizeof(uint32_t) * numOfSamples, sizeof(uint16_t) * numOfSamples);
if((timeArray == NULL) || (sampleArray == NULL)){
printf("Not enough RAM, exiting...\n");
return -1;
}
/* iterate through the SN array, saving each binary section to a CSV file */
for(int i = 0; serialNumbersToExport[i] > 0; i++){
uint32_t sampleIndex = 0;
if(verbose)
printf("\nAttempting binary-to-csv export of serial number %d...\n", serialNumbersToExport[i]);
/* create the source file paths */
char strSrcPath[DEFAULT_STR_LENGTH];
snprintf(strSrcPath, DEFAULT_STR_LENGTH, "/home/updsys/data/SN%d.ubin", serialNumbersToExport[i]);
if(verbose)
printf("\tAttempting to access '%s'...\n", strSrcPath);
/* open the source file */
FILE *sourceF;
sourceF = fopen(strSrcPath, "rb");
if(sourceF != NULL){
if(verbose)
printf("\tSource binary found, proceeding...\n");
/* find the starting point in the file, begin writing to the file
* until you reach the end of the file or the end time specified */
int32_t networkTime = 0;
uint32_t fileByteOffset = 0;
uint8_t blockHeaderArray[COMPRESSION_BLOCK_HEADER_LENGTH];
uint8_t blockDataArray[MAX_BLOCK_SIZE_IN_BYTES];
/* while time is less than end time OR we have reached the end of the file */
while(networkTime < endTime){
if(verbose)
printf("\tbinary file offset: %d\n", fileByteOffset);
fseek(sourceF, fileByteOffset, SEEK_SET); // set read pointer to beginning of file
/* when fread returns 0, break the loop */
if(fread(blockHeaderArray, 1, COMPRESSION_BLOCK_HEADER_LENGTH, sourceF) == 0)
break;
fileByteOffset += COMPRESSION_BLOCK_HEADER_LENGTH;
fseek(sourceF, fileByteOffset, SEEK_SET);
networkTime = (uint32_t)blockHeaderArray[0]
+ (((uint32_t)blockHeaderArray[1]) << 8)
+ (((uint32_t)blockHeaderArray[2]) << 16)
+ (((uint32_t)blockHeaderArray[3]) << 24);
uint16_t numOfSamples = blockHeaderArray[4];
uint16_t compressedWidth = blockHeaderArray[6];
uint16_t numBytesToRead = getBlockNumOfBytes16(compressedWidth, numOfSamples);
fread(blockDataArray, 1, numBytesToRead, sourceF);
fileByteOffset += numBytesToRead;
/* if the start time is less/equal to than the time at
* the end of the current block, then decompress and
* save the data */
int32_t timeAtEndOfBlock = networkTime + (int32_t)(numOfSamples * SAMPLE_INTERVAL_MS);
if(startTime <= timeAtEndOfBlock){
if(verbose)
printf("\tstart time (%d) within block end time (%d), decompressing...\n", startTime, timeAtEndOfBlock);
/* use to save single-block data to */
uint16_t uncompressedBlockDataArray[(MAX_BLOCK_SIZE_IN_BYTES/2)] = {0};
/* prepare to decompress */
CompressionDataStruct16 compressionDataStruct;
compressionDataStruct.sampleCount = numOfSamples;
compressionDataStruct.compressedWidth = compressedWidth;
compressionDataStruct.compressedData = blockDataArray;
compressionDataStruct.uncompressedData = uncompressedBlockDataArray;
decompressTo16(&compressionDataStruct);
/* save the samples into the arrays
* that will become the CSV files */
int32_t time = 0;
for(int j = 0; (j < numOfSamples) && (time < endTime); j++){
time = networkTime + (j * SAMPLE_INTERVAL_MS);
timeArray[sampleIndex] = time;
sampleArray[sampleIndex] = uncompressedBlockDataArray[j];
sampleIndex++;
}
}
}
if(verbose){
printf("\t%d samples found, closing source binary file...\n", sampleIndex);
}
fclose(sourceF);
/* if data was found, then write to CSV; otherwise move on */
if(sampleIndex > 0){
/* save the variables to '~/data/nodeNum.csv' */
char strDestPath[DEFAULT_STR_LENGTH];
snprintf(strDestPath, DEFAULT_STR_LENGTH, "/home/updsys/data/SN%d.csv", serialNumbersToExport[i]);
FILE *f;
f = fopen(strDestPath, "w"); // overwrite
for(uint16_t j = 0; j < sampleIndex; j++){
fprintf(f, "%d,%d\n", timeArray[j], sampleArray[j]);
}
fclose(f);
if(verbose){
printf("%d samples found, saving to %s\n", sampleIndex,strDestPath);
}
}else{
}
}else{
if(verbose)
printf("Source binary not found, moving on to next file...\n");
}
}
/* free the memory */
free(timeArray);
free(sampleArray);
if(verbose)
printf("\nfreeing memory...\n");
if(verbose)
printf("program execution complete\n");
sampleIndex
可以大于 numOfSamples
因为它在内部循环中没有重新初始化为 0 while(networkTime < endTime)
解决方案
确保 sampleIndex
永远不会大于内部 for 循环中的 numOfSamples
。
我在嵌入式平台上使用 C 相当有经验,但我并没有在 OS 中使用 C。我目前正在研究 Raspberry Pi 2.
我正在使用 C 语言工作,我需要制作一个实用程序,用于从二进制文件的一部分创建 CSV 文件。二进制文件包含许多小时的数据,格式为一系列 'blocks',每个包含约 2000 毫秒的数据。程序是遍历每个block,拉取数据,直到到达结束时间。
当我尝试进行相对较小的二进制到 csv 的转换时,该程序可以正常工作,但没有理由不能处理我可以识别的较大的转换。当我用 MAX_TIME_SAMPLE_TO_CONVERT 将程序 运行 设置为 180000 时,正常 运行 和 valgrind 都没有问题。当我将 "MAX_TIME_SAMPLE_TO_CONVERT" 更改为 200000 时,即出现分段错误。这只需要 60kB 内存的 malloc,这应该是轻而易举的事。当我从命令行 运行 "free" 时,我有超过 500MB 的可用空间。
当我 运行 valgrind 时,我得到了一个有点神秘的输出,但它告诉我遇到问题的行号(我正在使用 -g 选项构建)并且这些行号是正是使用 malloc 的变量:
/* save the samples into the arrays
* that will become the CSV files */
int32_t time = 0;
for(int j = 0; (j < numOfSamples) && (time < endTime); j++){
time = networkTime + (j * SAMPLE_INTERVAL_MS);
timeArray[sampleIndex] = time;
sampleArray[sampleIndex] = uncompressedBlockDataArray[j];
sampleIndex++;
}
完整代码:
int32_t startTime = getStartTime(argc, argv);
int32_t endTime = getEndTime(argc, argv);
/* calculate the amount of memory required to construct each array,
* limiting the maximum amount in order to conserve memory */
uint32_t timeWindow = endTime - startTime;
if(verbose)
printf("time window: %dms\n", timeWindow);
if(timeWindow > MAX_SAMPLE_TIME_TO_CONVERT){
timeWindow = MAX_SAMPLE_TIME_TO_CONVERT;
endTime = startTime + MAX_SAMPLE_TIME_TO_CONVERT;
if(verbose){
printf("warning: specified time window results in too many samples\n");
printf("\ttime window truncated to %dms\n", timeWindow);
printf("\tnew end time: %d\n", endTime);
}
}
uint32_t numOfSamples = timeWindow/SAMPLE_INTERVAL_MS;
if(verbose)
printf("each CSV file will contain up to %d samples (maximum of %dms)\n", numOfSamples, MAX_SAMPLE_TIME_TO_CONVERT);
/* allocate memory to temporarily store the
* data from the binary file as it is read */
int32_t *timeArray = (int32_t *)malloc(sizeof(uint32_t) * numOfSamples);
uint16_t *sampleArray= (uint16_t *)malloc(sizeof(uint16_t) * numOfSamples);
if(verbose)
printf("Allocating %d bytes for time and %d for samples\n", sizeof(uint32_t) * numOfSamples, sizeof(uint16_t) * numOfSamples);
if((timeArray == NULL) || (sampleArray == NULL)){
printf("Not enough RAM, exiting...\n");
return -1;
}
/* iterate through the SN array, saving each binary section to a CSV file */
for(int i = 0; serialNumbersToExport[i] > 0; i++){
uint32_t sampleIndex = 0;
if(verbose)
printf("\nAttempting binary-to-csv export of serial number %d...\n", serialNumbersToExport[i]);
/* create the source file paths */
char strSrcPath[DEFAULT_STR_LENGTH];
snprintf(strSrcPath, DEFAULT_STR_LENGTH, "/home/updsys/data/SN%d.ubin", serialNumbersToExport[i]);
if(verbose)
printf("\tAttempting to access '%s'...\n", strSrcPath);
/* open the source file */
FILE *sourceF;
sourceF = fopen(strSrcPath, "rb");
if(sourceF != NULL){
if(verbose)
printf("\tSource binary found, proceeding...\n");
/* find the starting point in the file, begin writing to the file
* until you reach the end of the file or the end time specified */
int32_t networkTime = 0;
uint32_t fileByteOffset = 0;
uint8_t blockHeaderArray[COMPRESSION_BLOCK_HEADER_LENGTH];
uint8_t blockDataArray[MAX_BLOCK_SIZE_IN_BYTES];
/* while time is less than end time OR we have reached the end of the file */
while(networkTime < endTime){
if(verbose)
printf("\tbinary file offset: %d\n", fileByteOffset);
fseek(sourceF, fileByteOffset, SEEK_SET); // set read pointer to beginning of file
/* when fread returns 0, break the loop */
if(fread(blockHeaderArray, 1, COMPRESSION_BLOCK_HEADER_LENGTH, sourceF) == 0)
break;
fileByteOffset += COMPRESSION_BLOCK_HEADER_LENGTH;
fseek(sourceF, fileByteOffset, SEEK_SET);
networkTime = (uint32_t)blockHeaderArray[0]
+ (((uint32_t)blockHeaderArray[1]) << 8)
+ (((uint32_t)blockHeaderArray[2]) << 16)
+ (((uint32_t)blockHeaderArray[3]) << 24);
uint16_t numOfSamples = blockHeaderArray[4];
uint16_t compressedWidth = blockHeaderArray[6];
uint16_t numBytesToRead = getBlockNumOfBytes16(compressedWidth, numOfSamples);
fread(blockDataArray, 1, numBytesToRead, sourceF);
fileByteOffset += numBytesToRead;
/* if the start time is less/equal to than the time at
* the end of the current block, then decompress and
* save the data */
int32_t timeAtEndOfBlock = networkTime + (int32_t)(numOfSamples * SAMPLE_INTERVAL_MS);
if(startTime <= timeAtEndOfBlock){
if(verbose)
printf("\tstart time (%d) within block end time (%d), decompressing...\n", startTime, timeAtEndOfBlock);
/* use to save single-block data to */
uint16_t uncompressedBlockDataArray[(MAX_BLOCK_SIZE_IN_BYTES/2)] = {0};
/* prepare to decompress */
CompressionDataStruct16 compressionDataStruct;
compressionDataStruct.sampleCount = numOfSamples;
compressionDataStruct.compressedWidth = compressedWidth;
compressionDataStruct.compressedData = blockDataArray;
compressionDataStruct.uncompressedData = uncompressedBlockDataArray;
decompressTo16(&compressionDataStruct);
/* save the samples into the arrays
* that will become the CSV files */
int32_t time = 0;
for(int j = 0; (j < numOfSamples) && (time < endTime); j++){
time = networkTime + (j * SAMPLE_INTERVAL_MS);
timeArray[sampleIndex] = time;
sampleArray[sampleIndex] = uncompressedBlockDataArray[j];
sampleIndex++;
}
}
}
if(verbose){
printf("\t%d samples found, closing source binary file...\n", sampleIndex);
}
fclose(sourceF);
/* if data was found, then write to CSV; otherwise move on */
if(sampleIndex > 0){
/* save the variables to '~/data/nodeNum.csv' */
char strDestPath[DEFAULT_STR_LENGTH];
snprintf(strDestPath, DEFAULT_STR_LENGTH, "/home/updsys/data/SN%d.csv", serialNumbersToExport[i]);
FILE *f;
f = fopen(strDestPath, "w"); // overwrite
for(uint16_t j = 0; j < sampleIndex; j++){
fprintf(f, "%d,%d\n", timeArray[j], sampleArray[j]);
}
fclose(f);
if(verbose){
printf("%d samples found, saving to %s\n", sampleIndex,strDestPath);
}
}else{
}
}else{
if(verbose)
printf("Source binary not found, moving on to next file...\n");
}
}
/* free the memory */
free(timeArray);
free(sampleArray);
if(verbose)
printf("\nfreeing memory...\n");
if(verbose)
printf("program execution complete\n");
sampleIndex
可以大于 numOfSamples
因为它在内部循环中没有重新初始化为 0 while(networkTime < endTime)
解决方案
确保 sampleIndex
永远不会大于内部 for 循环中的 numOfSamples
。