- 浏览: 208996 次
- 性别:
- 来自: 北京
最新评论
-
fuliang:
more cleanner than before
Big Integer Arithmetic -
yaojingguo:
Hi, Liang LiangThanks for the i ...
Redirect and restore stdout in C -
fuliang:
使用gcc编译.cpp,可以使用-lstdc++选项,这样gc ...
Redirect and restore stdout in C
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
发表评论
-
Division and modulo in C/C++, Java and Python
2013-12-19 12:58 688引用 a is an integer, n is a posi ... -
Hacking Assembly Code Generated by G++
2013-06-09 15:31 1205For the following C++ code: ... -
An Application of Bubble Sort
2013-04-11 21:03 775This problem is inspired by t ... -
C++ inline
2012-07-10 13:41 733http://www.parashift.com/c++-fa ... -
Check the final definition of a typedef
2012-07-04 10:43 661Refer to http://stackoverflow.c ... -
C symbol table
2012-05-06 10:38 1204C source code: #include ... -
GCC options: -ffunction-sections and -fdata-sections
2012-05-05 11:21 2070C code: struct person { ... -
Source code change effect on object code
2012-05-05 11:15 653C code: #include <std ... -
gcov tutorial
2012-05-03 21:01 822tmp.c #include <stdio.h& ... -
x86 64 assembly tutorial (MEMO)
2012-04-24 15:50 685Refer to http://os-blog.com/x86 ... -
The New C: X Macros (memo)
2012-04-20 10:42 792The code: #include <stdi ... -
Assembly code for stack array initialization
2012-04-14 10:28 845I develop the code with x86 gcc ... -
C array initialization
2012-04-14 10:18 732For fewer initializers, refer t ... -
Example of snprintf
2012-04-09 18:18 789#include <stdio.h> ... -
Array and its first element
2012-04-09 17:46 862#include <stdio.h> ... -
Relationship between union and its members
2012-04-08 09:42 664Code: #include <stdio.h& ... -
#if and #ifdef in C
2012-04-06 22:19 739#include <stdio.h> ... -
Optional comma in C initialization
2012-04-06 14:09 721#include <stdio.h> ... -
Access elements in array of struct
2012-04-05 10:32 766#include <stdio.h> ... -
Compute the difference between a value and 2's power
2012-03-31 10:14 544#include <stdio.h> ...
相关推荐
在结构体中使用0元素数组是一种创建柔性数组成员(flexible array member)的技巧,这在C99标准中被支持。柔性数组成员允许结构体的最后一个元素是一个长度为0的数组,这样做可以灵活地根据需要分配结构体后面的内存...
这是因为C语言中,直接声明`int a[]`是非法的,但在结构体中声明`int a[]`作为最后一个成员却是合法的,这就是所谓的flexible array member。这个特性允许我们在分配内存时同时存储数组的长度信息,从而解决在C语言...
<value> <struct> <member> <name>staffid</name> <value>1</value> </member> </struct> </array> </member> </struct> <!-- 第2个组的信息 --> ...... </array> </member> </struct> ...
"extundelete"是一款开源的数据恢复工具,专用于Linux系统,它可以帮助用户从EXT文件系统(如EXT2、EXT3、EXT4)中恢复意外删除的文件。在Linux环境中,由于文件系统的特性,一旦文件被删除,数据通常是可以被恢复的...
结果:[nx4 字符串] -> [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 结构指针变量的...
struct tagname array[ARRAY_SIZE] = {{val1}, {val2}, ...}; // 初始化数组 ``` 11.7 结构指针变量的说明和使用 11.7.1 指向结构变量的指针 ```c struct tagname *ptr; ptr = &var; // 取 var 的地址 *ptr = ...
前面的教程中我们讲解了数组(Array),它是一组具有相同类型的数据的集合。但在实际的编程过程中,我们往往还需要一组类型不同的数据,例如对于学生信息登记表,姓名为字符串,学号为整数,年龄为整数,所在的学习...
struct tagname (*ptr)[array_size] = &array_name; ``` 11.7.3 结构指针变量作函数参数: 结构体指针可以作为函数参数传递,实现对结构体的间接操作: ```c void func(struct tagname *param); func(&myStruct); ``...
策略性正确的struct(The Politically Correct Struct) 1.3 对象的差异(An Object Distinction) 指针的类型(The Type of a Pointer) 加上多态之后(Adding Polymorphism) 第2章 构造函数语意学(The Semantics...
3. **结构体数组(Structure Array)** 结构体可以作为数组元素,这样就可以存储多个结构体实例。例如,创建一个存储10个学生信息的数组: ```c struct student class[10]; ``` 4. **结构体指针(Structure ...
qsort(array, length, sizeof(struct Info), compare); ``` 在"prog1.c"中,开发者可能实现了上述逻辑,并且"prog1.sln"可能是该项目的Visual Studio解决方案文件,用于编译和运行程序。"ipch"目录通常包含预编译...
int compare_structures(const struct MyStruct *a, const struct MyStruct *b) { if (a->member1 != b->member1) return 0; if (a->member2 != b->member2) return 0; // ... return 1; } ``` **2.10 我的...
这种方法被称为“灵活数组成员”(Flexible Array Member),在C99标准中被引入,允许在结构体的末尾定义一个变长数组。这种做法是合法的,但需要注意内存管理,确保正确地分配和释放内存。 **2.7 是否有自动比较...
- **定义**: `struct structName { type member1; type member2; };` - **声明**: `structName variableName;` - **访问成员**: `variableName.memberName;` - **动态内存分配**: 使用`malloc`为结构体分配内存。 #...