核心中止(由于下一个大小无效)

时间:2014-09-17 05:44:59

标签: c

我正在编写一个程序来使用c中的读写功能对内存进行基准测试。

我在下面编写了一个程序,它接受使用顺序或随机访问复制到另一个内存位置的内存块大小。

每次我给出一个大于12个字节的值时,程序会给出以下信息:

以随机方式读取和写入秒的经过时间:0.000006

  <。> ./a.out'出错:free():下一个大小无效(快):0x00000000022cc030   中止(核心倾销)

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
int rand_lim(int blocksize) {
    /* return a random number between 0 and limit inclusive.
    */

    int divisor = RAND_MAX/(blocksize+1);
    int random_byte;

    do { 
        random_byte = rand() / divisor;
    } while (random_byte > blocksize);

    return random_byte;
}
int main(int argc , char *argv[])
{
    char temp;
    int num_threads=0;
    char *buffer, *write_buffer, access;
    int i=0, block_size;


    printf("Enter the block size of data in number of bytes:");
    scanf("%d",&block_size);
    buffer = (char*)malloc(block_size);
    write_buffer = (char*)malloc(block_size);
    for (i=0; i<block_size; i++)
    {
        memset(&buffer[i],'a',block_size);
        //printf("%s",&buffer[i]);   
    }
    printf("\nEnter the acess method --> random(R) OR seq(S) : ");
    scanf("%s", &access);

    printf("\nEnter the number of threads 1 , 2 , 4 , 8 \n");
    scanf("%d", &num_threads);

    if (access == 'S')
    {
        clock_t tic1 = clock();
        for (i=0; i<block_size; i++)
        {
            memcpy(&write_buffer[i],&buffer[i],block_size);
            //printf("%s",&write_buffer[i]); 
        }
        clock_t toc1 = clock();
        printf("Elapsed time for reading and writing in sequential manner : %f seconds  \n", (double)(toc1 - tic1)/CLOCKS_PER_SEC);
        free(buffer);
        free(write_buffer);
    }
    else if(access == 'R')
    {
        clock_t tic1 = clock();
        for (i=0; i<block_size; i++)
        {
            int j = rand_lim(block_size); 
            memcpy(&write_buffer[j],&buffer[j],block_size);
            int dummy = i;
        }

        clock_t toc1 = clock();
        printf("Elapsed time for reading and writing seconds in random manner : %f \n", (double)(toc1 - tic1)/CLOCKS_PER_SEC);
        free(buffer);
        free(write_buffer);
    }
    else
    { 
        printf ("\nPlease enter the correct method for bench marking. Exiting...");
        exit(0);
    }
    return 0;
}

我正在尝试使用1 GB这样的大数据进行基准测试,但程序不允许这样做。 任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:3)

这是未定义的行为:

buffer = (char*)malloc(block_size);
for (i=0; i<block_size; i++)
{
   memset(&buffer[i],'a',block_size); // !!
}

您应该用以下代码替换循环:

memset(buffer, 'a', block_size);

答案 1 :(得分:1)

for (i=0; i<block_size; i++)
{
   memset(&buffer[i],'a',block_size); // !!
}

你不需要运行循环,每次memset具有特定值的缓冲区。你可以用单行实现它

memset(buffer, 'a', block_size);

memcpy也是如此。其中一个是

for (i=0; i<block_size; i++)
{
    memcpy(&write_buffer[i],&buffer[i],block_size);
    //printf("%s",&write_buffer[i]); 
}

这里也是

memcpy(write_buffer,buffer,block_size);

就够了

最后

Do not cast the result of malloc

答案 2 :(得分:0)

除了John Zwinck's answer

可能你想要的是

int block_count = ...;
write_buffer = malloc(block_count * block_size);

...
for (i=0; i<block_count; i++)
    memcpy(write_buffer + i * block_size, buffer, block_size);

...

for (i=0; i<block_count; i++)
{
    int j = rand_lim(block_count - 1); // because rand_lim includes upper limit
    memcpy(write_buffer + j * block_size, buffer, block_size);
}

也就是说,write_bufferN块组成,每个块大小为block_size,您可以按顺序或随机编写它们。

BTW rand_lim()写得更简单:

int rand_lim(int limit) {
    return rand() % (limit + 1); // if you want to include the upper limit for some reason
}