- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在我的项目中,我遇到了 C 程序。
htmp
是一个结构指针。我们首先为它分配内存。但是我们为什么要为其元素分配内存 word
再次? id
和 next
? #define HASHREC bitewisehash
typedef struct hashrec {
char *word;
long long id;
struct hashrec *next;
} HASHREC;
/* Move-to-front hashing and hash function from Hugh Williams, http://www.seg.rmit.edu.au/code/zwh-ipl/ */
/* Simple bitwise hash function */
unsigned int bitwisehash(char *word, int tsize, unsigned int seed) {
char c;
unsigned int h;
h = seed;
for (; (c =* word) != '\0'; word++) h ^= ((h << 5) + c + (h >> 2));
return((unsigned int)((h&0x7fffffff) % tsize));
}
/* Insert string in hash table, check for duplicates which should be absent */
void hashinsert(HASHREC **ht, char *w, long long id) {
HASHREC *htmp, *hprv;
unsigned int hval = HASHFN(w, TSIZE, SEED);
for (hprv = NULL, htmp = ht[hval]; htmp != NULL && scmp(htmp->word, w) != 0; hprv = htmp, htmp = htmp->next);
if (htmp == NULL) {
htmp = (HASHREC *) malloc(sizeof(HASHREC)); # allocate memory for htmp
htmp->word = (char *) malloc(strlen(w) + 1); # why allocate memory again ?
strcpy(htmp->word, w); #
htmp->id = id; # why not allocate memory for htmp->id ?
htmp->next = NULL; # why nor allocate memory for htmp->next?
if (hprv == NULL) ht[hval] = htmp;
else hprv->next = htmp;
}
else fprintf(stderr, "Error, duplicate entry located: %s.\n",htmp->word);
return;
}
最佳答案
您需要在脑海中区分两件事(1)我要存储的东西存储在什么内存中? (2) 什么变量(指针)将地址保存到它的存储位置,以便我可以再次找到它。
首先声明两个指向 struct hashrec
的指针:
HASHREC *htmp, *hprv;
for
中的两个指针。循环声明,例如
hprv = NULL, htmp = ht[hval]
及以后
hprv = htmp, htmp = htmp->next
所以大概两个指针现在都持有一个地址并指向某个地方。
if (htmp == NULL)
, 表示
htmp
不指向地址(如果您发现感兴趣的哈希索引为空,则可能是这种情况)。
HASHREC
提供存储空间(例如
struct hashrec
)您需要分配存储空间,以便您有一 block 内存来存储您想要存储的东西。所以你分配一个 block 来保存一个结构。 (见:
Do I cast the result of malloc?)
typedef struct hashrec {
char *word;
long long id;
struct hashrec *next;
} HASHREC;
char *word;
的结构分配存储空间。 (指向 char 的指针 - 8 字节(x86 上为 4 字节)); (2) 一个
long long id;
(两者均为 8 字节)和 (3) 一个指针,用于保存下一个
HASHREC
的地址在序列中。
id
可以持有
long long
值,但是
word
呢?和
next
?它们都是指针。指针有什么作用?可以找到他们指向的东西的地址。哪里可以
word
被发现?
w
当前指向你想要的东西, 但不能保证
w
将继续保留您想要的单词,因此您将制作一个副本并将其存储为
HASHREC
的一部分.所以你看:
htmp->word = malloc(strlen(w) + 1); /* useless cast removed */
malloc
是什么意思?返回?它将地址返回到新内存块的开头,
strlen(w) + 1
字节长。由于指针保存其他东西的值作为它的值,
htmp->word
现在将地址存储到新内存块的开头作为其值。所以
htmp->word
“指向”新的内存块,您可以使用
htmp->word
作为引用该内存块的引用。
strcpy(htmp->word, w); #
htmp->id = id; # why not allocate memory for htmp->id ?
htmp->next = NULL; # why nor allocate memory for htmp->next?
strcpy(htmp->word, w);
副本
w
进入那个新的内存块。
htmp->id = id;
赋值
id
至
htmp->id
和
无需分配因为当你分配:
htmp = malloc(sizeof(HASHREC)); /* useless cast removed */
char *
分配存储空间指针,(2) 一个
long long id;
(3) 一个
struct hashrec*
指针——你已经为
long long
分配了所以
htmp->id
可以存储
id
的值在
long long
的内存中.
htmp->next = NULL; # why nor allocate memory for htmp->next?
htmp->next
重新分配? (提示:目前没有)它将指向下一个
struct hashrec
.目前初始化为
NULL
以便下次您迭代到所有
struct hashrec next
的末尾指针,当你到达
NULL
时,你就知道你已经走到了尽头.
struct hashrec next
现在可以指向你刚刚分配的这个节点。同样不需要额外分配,前一个节点
->next
指针只是指向顺序的下一个节点,不需要分配任何特定的新内存。它仅用作引用(或指向)链中下一个节点的引用。
关于c - 关于 struct 内存分配机制的一些困惑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49290091/
我在具有 2CPU 和 3.75GB 内存 (https://aws.amazon.com/ec2/instance-types/) 的 c3.large Amazon EC2 ubuntu 机器上运
我想通过用户空间中的mmap-ing并将地址发送到内核空间从用户空间写入VGA内存(视频内存,而不是缓冲区),我将使用pfn remap将这些mmap-ed地址映射到vga内存(我将通过 lspci
在 Mathematica 中,如果你想让一个函数记住它的值,它在语法上是很轻松的。例如,这是标准示例 - 斐波那契: fib[1] = 1 fib[2] = 1 fib[n_]:= fib[n] =
我读到动态内存是在运行时在堆上分配的,而静态内存是在编译时在堆栈上分配的,因为编译器知道在编译时必须分配多少内存。 考虑以下代码: int n; cin>>n; int a[n]; 如果仅在运行期间读
我是 Python 的新手,但我之前还不知道这一点。我在 for 循环中有一个基本程序,它从站点请求数据并将其保存到文本文件但是当我检查我的任务管理器时,我发现内存使用量只增加了?长时间运行时,这对我
我正在设计一组数学函数并在 CPU 和 GPU(使用 CUDA)版本中实现它们。 其中一些函数基于查找表。大多数表占用 4KB,其中一些占用更多。基于查找表的函数接受一个输入,选择查找表的一两个条目,
读入一个文件,内存被动态分配给一个字符串,文件内容将被放置在这里。这是在函数内部完成的,字符串作为 char **str 传递。 使用 gdb 我发现在行 **(str+i) = fgetc(aFil
我需要证实一个理论。我正在学习 JSP/Java。 在查看了一个现有的应用程序(我没有写)之后,我注意到一些我认为导致我们的性能问题的东西。或者至少是其中的一部分。 它是这样工作的: 1)用户打开搜索
n我想使用memoization缓存某些昂贵操作的结果,这样就不会一遍又一遍地计算它们。 两个memoise和 R.cache适合我的需要。但是,我发现缓存在调用之间并不可靠。 这是一个演示我看到的问
我目前正在分析一些 javascript shell 代码。这是该脚本中的一行: function having() { memory = memory; setTimeout("F0
我有一种情况,我想一次查询数据库,然后再将整个数据缓存在内存中。 我得到了内存中 Elasticsearch 的建议,我用谷歌搜索了它是什么,以及如何在自己的 spring boot 应用程序中实现它
我正在研究 Project Euler (http://projecteuler.net/problem=14) 的第 14 题。我正在尝试使用内存功能,以便将给定数字的序列长度保存为部分结果。我正在
所以,我一直在做 Java 内存/注意力游戏作业。我还没有达到我想要的程度,它只完成了一半,但我确实让 GUI 大部分工作了......直到我尝试向我的框架添加单选按钮。我认为问题可能是因为我将 JF
我一直在尝试使用 Flask-Cache 的 memoize 功能来仅返回 statusTS() 的缓存结果,除非在另一个请求中满足特定条件,然后删除缓存。 但它并没有被删除,并且 Jinja 模板仍
我对如何使用 & 运算符来减少内存感到非常困惑。 我可以回答下面的问题吗? clase C{ function B(&$a){ $this->a = &$a; $thi
在编写代码时,我遇到了一个有趣的问题。 我有一个 PersonPOJO,其 name 作为其 String 成员之一及其 getter 和 setter class PersonPOJO { priv
在此代码中 public class Base { int length, breadth, height; Base(int l, int b, int h) { l
Definition Structure padding is the process of aligning data members of the structure in accordance
在 JavaScript Ninja 的 secret 中,作者提出了以下方案,用于在没有闭包的情况下内存函数结果。他们通过利用函数是对象这一事实并在函数上定义一个属性来存储过去调用函数的结果来实现这
我正在尝试找出 map 消耗的 RAM 量。所以,我做了以下事情;- Map cr = crPair.collectAsMap(); // 200+ entries System.out.printl
我是一名优秀的程序员,十分优秀!