- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个看起来像这样的 for 循环:
for (i2 = 0; i2 < n_lines; i2++) {
statfs_full *f_tmp;
f_tmp = malloc(sizeof(statfs_full));
struct statfs *tmp;
tmp = malloc(sizeof(statfs));
statfs(fse[i2].fs_file, tmp);
merge_statfs_structs(tmp, &f_tmp);
strcpy(f_tmp->f_fstypename, fse[i2].fs_vsftype);
strcpy(f_tmp->f_mntonname, fse[i2].fs_file);
strcpy(f_tmp->f_mntfromname, fse[i2].fs_spec);
free(f_tmp);
free(tmp);
它是一个更大的函数的一部分。 statfs_full
是一个经过typdef 处理的结构,它只是添加了三个字段的标准struct statfs
。对函数 merge_statfs_structs()
的调用使用 tmp
指向的标准结构中的值填充 f_tmp 指向的扩展结构。对 strcpy()
的三个调用填充了额外的三个字段。
代码编译正常,但我可以从一些 printf 调用中看到程序在调用 free()
时出现段错误。早些时候,我在循环外部进行了声明和 malloc 调用,并尝试将两个结构重置为 NULL,但这在第二次调用 merge_statfs_structs
时出现段错误。
所以我想我的问题是如何在每次循环时“重置” f_tmp 和 tmp 指向的结构?
编辑:添加 merge_statfs_structs
的源代码,以防它有密切关系...
int merge_statfs_structs(struct statfs *buf, statfs_full **buf_full) {
int i;
(*buf_full)->f_type = buf->f_type;
(*buf_full)->f_bsize = buf->f_bsize;
(*buf_full)->f_blocks = buf->f_blocks;
(*buf_full)->f_bfree = buf->f_bfree;
(*buf_full)->f_bavail = buf->f_bavail;
(*buf_full)->f_files = buf->f_files;
(*buf_full)->f_ffree = buf->f_ffree;
(*buf_full)->f_fsid = buf->f_fsid;
(*buf_full)->f_namelen = buf->f_namelen;
(*buf_full)->f_frsize = buf->f_frsize;
for (i = 0; i < 5; i++)
(*buf_full)->f_spare[i] = buf->f_spare[i];
return 0;
}
编辑2:添加mounted_fs_entry
和struct statfs_full
的定义:
#define FS_TYPE_LEN 90
#define MNT_FLAGS_LEN 256
typedef struct _mounted_fs_entry {
char fs_spec[PATH_MAX]; /* device or special file system path */
char fs_file[PATH_MAX]; /* mount point */
char fs_vsftype[FS_TYPE_LEN]; /* file system type */
char fs_mntops[MNT_FLAGS_LEN]; /* mount flags */
int fs_freq; /* dump */
int fs_passno; /* pass */
} mounted_fs_entry;
#if __WORDSIZE == 32
#define __WORD_TYPE int
#else /* __WORDSIZE == 64 */
#define __WORD_TYPE long int
#endif
typedef struct _statfs_full {
__WORD_TYPE f_type; /* type of filesystem */
__WORD_TYPE f_bsize; /* optimal transfer block size */
fsblkcnt_t f_blocks; /* total data blocks in filesystem */
fsblkcnt_t f_bfree; /* free blocks in fs */
fsblkcnt_t f_bavail; /* free blocks available to unprivileged user */
fsfilcnt_t f_files; /* total file nodes in filesystem */
fsfilcnt_t f_ffree; /* free file nodes in fs */
fsid_t f_fsid; /* filesystem id */
__WORD_TYPE f_namelen; /* maximum length of filenames */
__WORD_TYPE f_frsize; /* fragment size (since Linux 2.6) */
__WORD_TYPE f_spare[5];
/* these extra fields add path info as in the *BSD versions of statfs() */
char f_fstypename[FS_TYPE_LEN]; /* fs type name */
char f_mntonname[PATH_MAX]; /* directory on which mounted */
char f_mntfromname[PATH_MAX]; /* mounted file sytem */
} statfs_full;
我自己没有定义struct stafs
,它是在/usr/include/sys/statfs.h
中定义的,或者你也可以man statfs
。 mounted_fs_entry
是 for 循环中 fse[i2]
引用的结构体。我在 github 上有整个文件 here如果你想看一下。
我在 gdb 中对此进行了更多的研究,在调用 free 期间,段错误发生在 glibc 内部。不幸的是,我的 glibc 不是用调试符号构建的,所以我无法使用 valgrind 来跟踪它。
最佳答案
我自己想出来了。将 malloc/free 调用移到循环之外,并在定义结构时摆脱了 typedef 语句。现在效果很好。这是更新后的代码(未删除错误检查):
struct statfs_ext *sas = malloc(sizeof(struct statfs_ext) * n_lines);
struct statfs_ext *f_tmp;
f_tmp = malloc(sizeof(struct statfs_ext));
if (f_tmp == NULL) {
perror("unable to malloc");
exit(EXIT_FAILURE);
}
struct statfs *s_tmp;
s_tmp = malloc(sizeof(struct statfs));
if (s_tmp == NULL) {
perror("unable to malloc");
exit(EXIT_FAILURE);
}
for (i2 = 0; i2 < n_lines; i2++) {
if (statfs(fse[i2].fs_file, s_tmp) != 0) {
perror("statfs() failed");
continue; /* might not be fatal */
}
merge_statfs_structs(s_tmp, &f_tmp);
strncpy(f_tmp->f_fstypename, fse[i2].fs_vsftype, FS_TYPE_LEN);
strncpy(f_tmp->f_mntonname, fse[i2].fs_file, PATH_MAX);
strncpy(f_tmp->f_mntfromname, fse[i2].fs_spec, PATH_MAX);
sas[i2] = *f_tmp;
}
free(s_tmp);
free(f_tmp);
以及结构定义:
#define FS_TYPE_LEN 90
#define MNT_FLAGS_LEN 256
struct mounted_fs_entry {
char fs_spec[PATH_MAX]; /* device or special file system path */
char fs_file[PATH_MAX]; /* mount point */
char fs_vsftype[FS_TYPE_LEN]; /* file system type */
char fs_mntops[MNT_FLAGS_LEN]; /* mount flags */
int fs_freq; /* dump */
int fs_passno; /* pass */
};
#if __WORDSIZE == 32
#define __WORD_TYPE int
#else /* __WORDSIZE == 64 */
#define __WORD_TYPE long int
#endif
struct statfs_ext {
__WORD_TYPE f_type; /* type of filesystem (see below) */
__WORD_TYPE f_bsize; /* optimal transfer block size */
fsblkcnt_t f_blocks; /* total data blocks in filesystem */
fsblkcnt_t f_bfree; /* free blocks in fs */
fsblkcnt_t f_bavail; /* free blocks available to unprivileged user */
fsfilcnt_t f_files; /* total file nodes in filesystem */
fsfilcnt_t f_ffree; /* free file nodes in fs */
fsid_t f_fsid; /* filesystem id */
__WORD_TYPE f_namelen; /* maximum length of filenames */
__WORD_TYPE f_frsize; /* fragment size (since Linux 2.6) */
__WORD_TYPE f_spare[5];
/* these extra fields add path info as in the *BSD versions of statfs() */
char f_fstypename[FS_TYPE_LEN]; /* fs type name */
char f_mntonname[PATH_MAX]; /* directory on which mounted */
char f_mntfromname[PATH_MAX]; /* mounted file sytem */
};
关于c - 如何循环结构体指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26007769/
这个问题已经有答案了: In C, does a pointer to a structure always point to its first member? (3 个回答) 已关闭 7 年前。
当执行第二个fscanf时,控制台停止工作。我做错了什么? 输入文件包含: 3 minsu 50 80 40 sarah 30 60 40 jason 70 80 90 代码: #define _CR
Swift 结构体是构建代码所用的一种通用且灵活的构造体。 我们可以为结构体定义属性(常量、变量)和添加方法,从而扩展结构体的功能。 与 C 和 Objective C 不同的是: 结构
我想在 javascript 中创建一个结构。我有一对信息,我想使用,例如: array[1] = new Struct(); array[1].name = "parameter-name"; ar
我不允许使用带有 in 关键字的结构,对吗?例如: struct Rect { float x,y,width,height; }; layout(location = 7) in Rect
我的结构声明的片段: struct record{ char type[4]; uint32_t data_size; uint32_t flags; uint32_t
您能否帮助我理解为什么我的 dataStruct 结构的值不是其成员之一的值? (对于simpleDataStruct结构) 我用这一行打印值: printf("dataStruct:........
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
这个问题已经有答案了: Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized point
我不确定如何在 C 中创建一个具有不同位大小的变量的结构,例如: 我想创建一个结构体,其中一个变量作为 8 位整数,一个变量作为 16 位 bool 值,一个变量作为 8 位 bool 值,一个变量作
我正在为一个项目编写一个通信协议(protocol),其中包括两个用于请求和响应的 C 结构。根据设备的设置方式,数据传输可以是请求(主模块)或响应(从模块)。 这些结构彼此非常接近。最大的区别在于请
我有以下 C 结构体,代表外部芯片中的寄存器 typedef union { // Individual Fields struct { uint8_t ELEM_1
我了解 C++,并且正在学习 C。我想将 nullptr 定义为 NULL,但是当我使用括号初始化结构时,它会导致错误并显示预期的“}”。我正在使用 Visual Studio 编译器,我的代码示例如
#include struct s { char *a1; int a; }; int main(){ struct s p={"asdv",11}; struct s p1=p;
如果将记录作为参数发送给函数,如何添加记录? struct record { char name[20]; int nr; }; void AddRecord(struct record **p_al
使用python向C语言的链接库传递数组、结构体、指针类型的数据 由于最近的项目频繁使用python调用同事的C语言代码,在调用过程中踩了很多坑,一点一点写出来供大家参考,我们仍然是使用ctype
枚举、结构体、类 注:本文为作者自己总结,过于基础的就不再赘述 ,都是亲自测试的结果。如有错误或者遗漏的地方,欢迎指正,一起学习。 1、枚举 枚举是用来定义一组通用类型的一组相关值 ,关键字enum
Swift 结构体是构建代码所用的一种通用且灵活的构造体 可以为结构体定义属性(常量、变量)和添加方法,从而扩展结构体的功能 与 C 和 Objective C 不同的是: 结构体不需要包
关于结构的快速问题: struct xint { int number; char string[12]; }; int main(int argc, char *argv[])
编辑 我发现了这个: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays 基本上,如果我有这样的东西: struc
我是一名优秀的程序员,十分优秀!