最近,我编写了许多程序,在函数中传递structs,以避免全局变量。但是,我想知道传递struct本身或它的指针是否更有效。听起来应该是这样,因为指针(在我的64位GNU/Linux系统上)是8个字节,而一个充满指针的struct显然要多得多。
但是,如果我有这个struct
struct Point {
int x;
int y;
}
它是8个字节,与指针大小相同,是将整个struct传递给一个函数还是传递一个指针?我相当精通C内存分配,所以在初始化指针时使用malloc和朋友并不是个问题。
我的另一个想法是,如果结构很大,直接通过的结构就会占用大量的堆栈空间。然而,简单地使
在C中,结构可以通过值传递/返回,也可以通过引用(通过指针)传递/返回。
一般的共识似乎是,在大多数情况下,前者可以适用于小型结构而不受惩罚。见和
从速度和清晰的角度来看,避免放弃是有益的。但是什么算小呢?我想大家都同意,这是一个小结构:
struct Point { int x, y; };
我们可以在相对不受惩罚的情况下按价值传递:
struct Point sum(struct Point a, struct Point b) {
return struct Point { .x = a.x + b.x, .y = a.y + b.y };
}
而Linux的task_struct是
谁能给我解释一下从linux内核获取的代码片段吗?
/*
* how to get the thread information struct from C
*/
static inline struct thread_info *current_thread_info(void) __attribute_const__;
static inline struct thread_info *current_thread_info(void)
{
register unsigned long sp asm ("sp");
return
目前,我能够找到程序的注册值,而不是为其他进程。
到目前为止,我写的是:
#include <linux/sched.h>
struct task_struct *task_list;
for_each_process(task_list){
register int* pc asm("%pc");
register int mar asm("%mar");
register int mdr asm("%mdr");
register int cir asm(
我正在步入汇编语言编程的世界。我在试着理解所有在上找到的东西
我看到了下面的代码:
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
;This part works fine.
;mov edx, len ;message length
;mov ecx, msg ;message to write
;This does not work because I interchanged edx and ecx.
最近,我想了解关于Go program.However启动的详细过程,当我读到一些关于线程本地存储的代码时,我感到很困惑。以下代码片段来自名为runtime/asm_amd64.s的源文件第183至198行:
LEAQ runtime·m0+m_tls(SB), DI
CALL runtime·settls(SB)
// store through it, to make sure it works
get_tls(BX)
MOVQ $0x123, g(BX)
MOVQ runtime·m0+m_tls(SB), AX