struct

Why does `sxhash` return a constant for all structs?

柔情痞子 提交于 2020-12-05 03:40:50
问题 When using the Common Lisp sxhash function on structs I'm getting the same value for all structs (in SBCL only structs of the same type). For instance, the following code prints two lists of integers all of which have the same value. (progn (defstruct foo data) (print (mapcar #'sxhash (loop for i below 10 collect (make-foo :data i)))) (defstruct bar data) (print (mapcar #'sxhash (loop for i below 10 collect (make-bar :data i))))) ;;; Allegro (319 319 319 319 319 319 319 319 319 319) (319 319

What are the differences between the multiple ways to create zero-sized structs?

寵の児 提交于 2020-12-02 05:54:27
问题 I found four different ways to create a struct with no data: struct A{} // empty struct / empty braced struct struct B(); // empty tuple struct struct C(()); // unit-valued tuple struct struct D; // unit struct (I'm leaving arbitrarily nested tuples that contain only () s and single-variant enum declarations out of the question, as I understand why those shouldn't be used). What are the differences between these four declarations? Would I use them for specific purposes, or are they

What are the differences between the multiple ways to create zero-sized structs?

杀马特。学长 韩版系。学妹 提交于 2020-12-02 05:54:27
问题 I found four different ways to create a struct with no data: struct A{} // empty struct / empty braced struct struct B(); // empty tuple struct struct C(()); // unit-valued tuple struct struct D; // unit struct (I'm leaving arbitrarily nested tuples that contain only () s and single-variant enum declarations out of the question, as I understand why those shouldn't be used). What are the differences between these four declarations? Would I use them for specific purposes, or are they

What are the differences between the multiple ways to create zero-sized structs?

自作多情 提交于 2020-12-02 05:54:04
问题 I found four different ways to create a struct with no data: struct A{} // empty struct / empty braced struct struct B(); // empty tuple struct struct C(()); // unit-valued tuple struct struct D; // unit struct (I'm leaving arbitrarily nested tuples that contain only () s and single-variant enum declarations out of the question, as I understand why those shouldn't be used). What are the differences between these four declarations? Would I use them for specific purposes, or are they

Declaring int array inside struct

情到浓时终转凉″ 提交于 2020-11-30 05:03:28
问题 In C, I have defined the struct seen below, and would like to initialize it inline. Neither the fields inside the struct, nor the array foos will change after initialization. The code in the first block works fine. struct Foo { int bar; int *some_array; }; typedef struct Foo Foo; int tmp[] = {11, 22, 33}; struct Foo foos[] = { {123, tmp} }; However, I don't really need the tmp field. In fact, it will just clutter my code (this example is somewhat simplified). So, instead I'd like to declare

Declaring int array inside struct

谁说我不能喝 提交于 2020-11-30 04:59:56
问题 In C, I have defined the struct seen below, and would like to initialize it inline. Neither the fields inside the struct, nor the array foos will change after initialization. The code in the first block works fine. struct Foo { int bar; int *some_array; }; typedef struct Foo Foo; int tmp[] = {11, 22, 33}; struct Foo foos[] = { {123, tmp} }; However, I don't really need the tmp field. In fact, it will just clutter my code (this example is somewhat simplified). So, instead I'd like to declare

What is the proper way to create a new generic struct?

纵饮孤独 提交于 2020-11-28 07:55:49
问题 I'm trying to make a generic struct that can be initialized to something of type T . It looks like this: pub struct MyStruct<T> { test_field: Option<T>, name: String, age: i32, } impl MyStruct<T> { fn new(new_age: i32, new_name: String) -> MyStruct<T> { MyStruct<T> { test_field: None, age: new_age, name: new_name, } } } This doesn't seem to work. Among other errors, I get: error: chained comparison operators require parentheses --> src/lib.rs:9:17 | 9 | MyStruct<T> { | ^^^^^ | 回答1: I highly

What is the proper way to create a new generic struct?

一世执手 提交于 2020-11-28 07:54:12
问题 I'm trying to make a generic struct that can be initialized to something of type T . It looks like this: pub struct MyStruct<T> { test_field: Option<T>, name: String, age: i32, } impl MyStruct<T> { fn new(new_age: i32, new_name: String) -> MyStruct<T> { MyStruct<T> { test_field: None, age: new_age, name: new_name, } } } This doesn't seem to work. Among other errors, I get: error: chained comparison operators require parentheses --> src/lib.rs:9:17 | 9 | MyStruct<T> { | ^^^^^ | 回答1: I highly

用C写有面向对象特点的程序

廉价感情. 提交于 2020-11-20 07:06:05
转载自:陈皓 http://blog.csdn.net/haoel/archive/2003/04/02/2864.aspx 比如在一个项目中,有大量的数据结构,他们都是双向链表,但又想共用一套对链表的操作算法,这怎么做到呢,C中又没有C++中的继承,不然我可以继承一父(类中只有两个指针,一个向前一个向后),而其算法可以写在你类中的虚函数中,供子类使用。如: class Links { public: Links* back; Links* forword; virtual Add(){ ... }; virtual Del(){ ... }; virtual Ins(){ ... }; virtual Print() =0; .... }; 于是对于特定的数据结构我们可以: class mylinks : public Links { public: char* myname; char sex; int age; ... virtual Print(){ .... } }; 对其操作时都可以使用你类的泛型算法。 在C中,该如何做呢?我们用C中的指针和强制类型转可以做到。 下面是我总结出来的一个小的程序,体现了用指针的弹性来实现这一继承的效果:(我在Liniux下的GCC调试通过) ======================================= #include

malloc()和calloc()

痞子三分冷 提交于 2020-11-17 07:15:46
malloc()和calloc() 进程对动态内存的请求被认为是不紧迫的。例如,当进程的可执行文件被装入时,进程并不一定立即对所有的代码进行访问。类似地,当进程调用malloc() 请求动态内存时,并不意味着进程很快就会访问所有获得的内存。因此一般来说,内核总是尽量推迟给用户态进程动态分配内存。 The kernel succeeds in deferring the allocation of dynamic memory to processes by using a new kind of resource. When a User Mode process asks for dynamic memory, it doesn't get additional page frames; instead, it gets the right to use a new range of linear addresses, which become part of its address space. This interval is called a "memory region." 内核使用一种资源成功实现了对进程动态内存的推迟分配。当用户态进程请求动态内存时,并没有获得请求的页框,而仅仅获得对一个新的线性地址区的使用权,而这一线性地址区间就成为进程地址空间的一部分