memory_free--test

#include <stdio.h>
#include <stdlib.h>
int * returnint()
{
    int* point = (int*)malloc(sizeof(int));
    *point = 294969;
    printf("point is %d\n", *point);
    return point;
}


int main()
{
int *p;
p = returnint();
printf("the returnint is %d\n",*p);
free(p);
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用gettimeofday函数测量磁盘读写速度,并计算出所需的时间。以下是一个示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #define BUFFER_SIZE (1 << 20) // 1MB int main() { char *buffer = malloc(BUFFER_SIZE); if (buffer == NULL) { printf("Failed to allocate memory.\n"); return 1; } FILE *file = fopen("test.txt", "wb"); // 以二进制写入方式打开文件 if (file == NULL) { printf("Failed to open file.\n"); return 1; } struct timeval startTime, endTime; gettimeofday(&startTime, NULL); fwrite(buffer, BUFFER_SIZE, 1, file); // 写入1MB数据 fflush(file); // 强制刷新缓冲区 gettimeofday(&endTime, NULL); fclose(file); free(buffer); double timeElapse = (double) (endTime.tv_sec - startTime.tv_sec) * 1000.0 + (double) (endTime.tv_usec - startTime.tv_usec) / 1000.0; printf("Write time: %.2f ms.\n", timeElapse); buffer = malloc(BUFFER_SIZE); if (buffer == NULL) { printf("Failed to allocate memory.\n"); return 1; } file = fopen("test.txt", "rb"); // 以二进制读取方式打开文件 if (file == NULL) { printf("Failed to open file.\n"); return 1; } gettimeofday(&startTime, NULL); fread(buffer, BUFFER_SIZE, 1, file); // 读取1MB数据 gettimeofday(&endTime, NULL); fclose(file); free(buffer); timeElapse = (double) (endTime.tv_sec - startTime.tv_sec) * 1000.0 + (double) (endTime.tv_usec - startTime.tv_usec) / 1000.0; printf("Read time: %.2f ms.\n", timeElapse); return 0; } ``` 在上面的代码中,我们使用gettimeofday函数分别测量了磁盘写入和读取的时间,并计算出了所需的时间。首先,我们分配1MB内存作为缓冲区,然后以二进制写入方式打开文件,写入1MB数据,并强制刷新缓冲区。接着,我们测量写入所需时间,并输出结果。然后,我们以二进制读取方式打开文件,读取1MB数据,测量读取所需时间,并输出结果。最后,我们释放内存并关闭文件。 需要注意的是,gettimeofday函数返回的时间是以秒和微秒为单位,需要进行转换成毫秒。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值