`
yaojingguo
  • 浏览: 207937 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Struct array member

 
阅读更多

C code:

#include <stdio.h>

// This struct does not store jp_data:            
//  +-+-+-+-+ 
// 0+-+-+-+-+ jp_len     
//    3 2 1 0
struct zero_size_array_struct {
	int jp_len;
	char jp_data[0];
};

// This struct stores a char pointer:
//  +-+-+-+-+ 
// 1+-+-+-+-+ jp_data
// 0+-+-+-+-+ jp_len     
//    3 2 1 0
struct pointer_struct {
	int jp_len;
	char* jp_data;
};

// This struct stores a 8-char array:
//  +-+-+-+-+ 
// 2+-+-+-+-+ 
// 1+-+-+-+-+ jp_data
// 0+-+-+-+-+ jp_len     
//    3 2 1 0
struct array_struct {
	int jp_len;
	char jp_data[8];
};

char data[] = {0, 1, 2, 3, 4, 5, 6, 7};

struct zero_size_array_struct* one = (struct zero_size_array_struct*) data;
struct pointer_struct* two = (struct pointer_struct*) data;
struct array_struct* three = (struct array_struct*) data;
struct array_struct instance = { 0x100, {0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80} };
struct array_struct* four = &instance;

void sizes() {
	printf("sizeof(struct zero_size_array_struct): %d\n", sizeof(struct zero_size_array_struct));
	printf("sizeof(struct pointer_struct): %d\n", sizeof(struct pointer_struct));
	printf("sizeof(struct array_struct): %d\n", sizeof(struct array_struct));
}

int main(int argc, const char *argv[]) {
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("struct pointer: %08x\n", one);
	printf("jp_len: %08x\n", one->jp_len);
	// movl	one, %eax
	// leal	4(%eax), %edx
	printf("jp_data: %08x\n", one->jp_data);  
	printf("jp_data[0]: %08x\n", one->jp_data[0]);
	printf("jp_data[1]: %08x\n", one->jp_data[1]);
	printf("jp_data[1]: %08x\n", *((char *) one + 5));

	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("struct pointer: %08x\n", two);
	printf("jp_len: %08x\n", two->jp_len);
	// movl	two, %eax
	// movl	4(%eax), %edx
	printf("jp_data: %08x\n", two->jp_data);

	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("struct pointer: %08x\n", three);
	printf("jp_len: %08x\n", three->jp_len);
	// movl	three, %eax
	// leal	4(%eax), %edx
	printf("jp_data: %08x\n", three->jp_data);
	printf("jp_data[0]: %08x\n", three->jp_data[0]);
	printf("jp_data[1]: %08x\n", three->jp_data[1]);
	printf("jp_data[1]: %08x\n", *((char *) three + 5));

	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("struct pointer: %08x\n", four);
	printf("jp_len: %08x\n", four->jp_len);
	// movl	four, %eax
	// leal	4(%eax), %edx
	printf("jp_data: %08x\n", four->jp_data);
	printf("jp_data[0]: %08x\n", four->jp_data[0]);
	printf("jp_data[1]: %08x\n", four->jp_data[1]);
	printf("jp_data[1]: %08x\n", *((char *) four + 5));
	return 0;
}
 

 

Assembly code:

	.file	"fix.c"
.globl data
	.data
	.type	data, @object
	.size	data, 8
data:
	.byte	0
	.byte	1
	.byte	2
	.byte	3
	.byte	4
	.byte	5
	.byte	6
	.byte	7
.globl one
	.align 4
	.type	one, @object
	.size	one, 4
one:
	.long	data
.globl two
	.align 4
	.type	two, @object
	.size	two, 4
two:
	.long	data
.globl three
	.align 4
	.type	three, @object
	.size	three, 4
three:
	.long	data
.globl instance
	.align 4
	.type	instance, @object
	.size	instance, 12
instance:
	.long	256
	.byte	16
	.byte	32
	.byte	48
	.byte	64
	.byte	80
	.byte	96
	.byte	112
	.byte	-128
.globl four
	.align 4
	.type	four, @object
	.size	four, 4
four:
	.long	instance
	.section	.rodata
	.align 4
.LC0:
	.string	"sizeof(struct zero_size_array_struct): %d\n"
	.align 4
.LC1:
	.string	"sizeof(struct pointer_struct): %d\n"
	.align 4
.LC2:
	.string	"sizeof(struct array_struct): %d\n"
	.text
.globl sizes
	.type	sizes, @function
sizes:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$24, %esp
	movl	$.LC0, %eax
	movl	$4, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	$.LC1, %eax
	movl	$8, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	$.LC2, %eax
	movl	$12, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	leave
	ret
	.size	sizes, .-sizes
	.section	.rodata
.LC3:
	.string	"~~~~~~~~~~~~~~~~~~~~~~~~~~"
.LC4:
	.string	"struct pointer: %08x\n"
.LC5:
	.string	"jp_len: %08x\n"
.LC6:
	.string	"jp_data: %08x\n"
.LC7:
	.string	"jp_data[0]: %08x\n"
.LC8:
	.string	"jp_data[1]: %08x\n"
	.text
.globl main
	.type	main, @function
main:
	pushl	%ebp
	movl	%esp, %ebp
	andl	$-16, %esp
	subl	$16, %esp
	movl	$.LC3, (%esp)
	call	puts
	movl	one, %edx
	movl	$.LC4, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	one, %eax
	movl	(%eax), %edx
	movl	$.LC5, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	one, %eax
	leal	4(%eax), %edx
	movl	$.LC6, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	one, %eax
	movzbl	4(%eax), %eax
	movsbl	%al,%edx
	movl	$.LC7, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	one, %eax
	movzbl	5(%eax), %eax
	movsbl	%al,%edx
	movl	$.LC8, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	one, %eax
	addl	$5, %eax
	movzbl	(%eax), %eax
	movsbl	%al,%edx
	movl	$.LC8, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	$.LC3, (%esp)
	call	puts
	movl	two, %edx
	movl	$.LC4, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	two, %eax
	movl	(%eax), %edx
	movl	$.LC5, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	two, %eax
	movl	4(%eax), %edx
	movl	$.LC6, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	$.LC3, (%esp)
	call	puts
	movl	three, %edx
	movl	$.LC4, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	three, %eax
	movl	(%eax), %edx
	movl	$.LC5, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	three, %eax
	leal	4(%eax), %edx
	movl	$.LC6, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	three, %eax
	movzbl	4(%eax), %eax
	movsbl	%al,%edx
	movl	$.LC7, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	three, %eax
	movzbl	5(%eax), %eax
	movsbl	%al,%edx
	movl	$.LC8, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	three, %eax
	addl	$5, %eax
	movzbl	(%eax), %eax
	movsbl	%al,%edx
	movl	$.LC8, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	$.LC3, (%esp)
	call	puts
	movl	four, %edx
	movl	$.LC4, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	four, %eax
	movl	(%eax), %edx
	movl	$.LC5, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	four, %eax
	leal	4(%eax), %edx
	movl	$.LC6, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	four, %eax
	movzbl	4(%eax), %eax
	movsbl	%al,%edx
	movl	$.LC7, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	four, %eax
	movzbl	5(%eax), %eax
	movsbl	%al,%edx
	movl	$.LC8, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	four, %eax
	addl	$5, %eax
	movzbl	(%eax), %eax
	movsbl	%al,%edx
	movl	$.LC8, %eax
	movl	%edx, 4(%esp)
	movl	%eax, (%esp)
	call	printf
	movl	$0, %eax
	leave
	ret
	.size	main, .-main
	.ident	"GCC: (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3"
	.section	.note.GNU-stack,"",@progbits
 

 

 

分享到:
评论

相关推荐

    详解C语言Struct(结构体)系列之一

    在结构体中使用0元素数组是一种创建柔性数组成员(flexible array member)的技巧,这在C99标准中被支持。柔性数组成员允许结构体的最后一个元素是一个长度为0的数组,这样做可以灵活地根据需要分配结构体后面的内存...

    C语言的Struct Hack笔记

    这是因为C语言中,直接声明`int a[]`是非法的,但在结构体中声明`int a[]`作为最后一个成员却是合法的,这就是所谓的flexible array member。这个特性允许我们在分配内存时同时存储数组的长度信息,从而解决在C语言...

    xmlrpc++0.7源代码,解析xml

    &lt;value&gt; &lt;struct&gt; &lt;member&gt; &lt;name&gt;staffid&lt;/name&gt; &lt;value&gt;1&lt;/value&gt; &lt;/member&gt; &lt;/struct&gt; &lt;/array&gt; &lt;/member&gt; &lt;/struct&gt; &lt;!-- 第2个组的信息 --&gt; ...... &lt;/array&gt; &lt;/member&gt; &lt;/struct&gt; ...

    extundelete

    "extundelete"是一款开源的数据恢复工具,专用于Linux系统,它可以帮助用户从EXT文件系统(如EXT2、EXT3、EXT4)中恢复意外删除的文件。在Linux环境中,由于文件系统的特性,一旦文件被删除,数据通常是可以被恢复的...

    StructSearch(input_​struct,search_term):搜索并返回带有匹配参数的详细信息和子结构。-matlab开发

    结果:[nx4 字符串] -&gt; [matching_member full_path_of_that_member Class_of_member Description_of_member] 2.Child_struct= 由匹配成员形成的结构,同时保留层次结构。 应用:对于具有大量成员的嵌套结构很有用。...

    结构体基本知识

    struct tagname varname = { .member1 = value1, .member2 = value2, ... }; ``` 11.6 结构数组的定义 结构数组的定义方式类似于普通数组,如: ```c struct tagname array[arraysize]; ``` 11.7 结构指针变量的...

    C/C++ 结构体与共用体教程

    struct tagname array[ARRAY_SIZE] = {{val1}, {val2}, ...}; // 初始化数组 ``` 11.7 结构指针变量的说明和使用 11.7.1 指向结构变量的指针 ```c struct tagname *ptr; ptr = &var; // 取 var 的地址 *ptr = ...

    C语言 结构体(Struct)详解及示例代码

    前面的教程中我们讲解了数组(Array),它是一组具有相同类型的数据的集合。但在实际的编程过程中,我们往往还需要一组类型不同的数据,例如对于学生信息登记表,姓名为字符串,学号为整数,年龄为整数,所在的学习...

    C语言全书word版CHAR11

    struct tagname (*ptr)[array_size] = &array_name; ``` 11.7.3 结构指针变量作函数参数: 结构体指针可以作为函数参数传递,实现对结构体的间接操作: ```c void func(struct tagname *param); func(&myStruct); ``...

    深度探索C++对象模型 超清版

    策略性正确的struct(The Politically Correct Struct) 1.3 对象的差异(An Object Distinction) 指针的类型(The Type of a Pointer) 加上多态之后(Adding Polymorphism) 第2章 构造函数语意学(The Semantics...

    《深度探索C++对象模型》(Stanley B·Lippman[美] 著,侯捷 译)

    策略性正确的struct(The Politically Correct Struct) 1.3 对象的差异(An Object Distinction) 指针的类型(The Type of a Pointer) 加上多态之后(Adding Polymorphism) 第2章 构造函数语意学(The Semantics...

    C语言英文精选Chapter-10ppt课件.ppt

    3. **结构体数组(Structure Array)** 结构体可以作为数组元素,这样就可以存储多个结构体实例。例如,创建一个存储10个学生信息的数组: ```c struct student class[10]; ``` 4. **结构体指针(Structure ...

    maopao.zip_goose9dd_冒泡排序_结构体信息

    qsort(array, length, sizeof(struct Info), compare); ``` 在"prog1.c"中,开发者可能实现了上述逻辑,并且"prog1.sln"可能是该项目的Visual Studio解决方案文件,用于编译和运行程序。"ipch"目录通常包含预编译...

    你必须知道的495个C语言问题

    int compare_structures(const struct MyStruct *a, const struct MyStruct *b) { if (a-&gt;member1 != b-&gt;member1) return 0; if (a-&gt;member2 != b-&gt;member2) return 0; // ... return 1; } ``` **2.10 我的...

    C语言常见问题及规避和解决办法

    这种方法被称为“灵活数组成员”(Flexible Array Member),在C99标准中被引入,允许在结构体的末尾定义一个变长数组。这种做法是合法的,但需要注意内存管理,确保正确地分配和释放内存。 **2.7 是否有自动比较...

    C语言程序设计基础入门教学视频全集(23讲)

    - **定义**: `struct structName { type member1; type member2; };` - **声明**: `structName variableName;` - **访问成员**: `variableName.memberName;` - **动态内存分配**: 使用`malloc`为结构体分配内存。 #...

Global site tag (gtag.js) - Google Analytics