我一直在研究一个我们称之为"A"的分支.我刚刚意识到自从我上次提交以来我添加的代码应该是在特定的(实验性)分支中,而不是在"A"中.如何将更改提交到新分支并将分支"A"保留为上次提交时的状态?
我试图用这个代码(既了解在C浮点表示float和int是我的机器上4个字节):
int x = 3;
float y = *(float*) &x;
printf("%d %e \n", x, y);
Run Code Online (Sandbox Code Playgroud)
我们知道x的二进制表示如下
00000000000000000000000000000011
因此,我希望y表示如下
符号位(左起第一位)= 0
指数(左起第2-9位)= 0
尾数(第10-32位): 1 + 2^(-22)+2^(-23)
导致 y = (-1)^0 * 2^(0-127) * (1+2^(-22) + 2^(-23)) = 5.87747E-39
然而,我的程序打印出来
3 4.203895e-45
也就是说,y具有值4.203895e-45而不是5.87747E-39我预期的值.为什么会这样呢?我究竟做错了什么?
PS我也直接从gdb打印了值,所以printf命令不是问题.
我正在尝试研究C,我正在使用char*和char数组来解决问题.我正在使用库中的通用哈希集容器(我不想详细描述).该库包含该功能
void *HashSetLookup(hashset *h, const void *elemAddr);
Run Code Online (Sandbox Code Playgroud)
我必须使用它来搜索哈希集以查看该元素是否已经存在(哈希和比较函数是哈希集结构的一部分).在这种情况下,我使用hashset来存储指向C字符串的指针,或者更具体地说(char**).我的问题是以下代码给出了分段错误:
char word[1024];
/* Some code that writes to the word buffer */
HashSetLookup(stopList, &word);
Run Code Online (Sandbox Code Playgroud)
虽然这段代码工作正常(并按预期):
char word[1024];
/* The same code as before that writes to the word buffer */
char* tmp = strdup(word);
HashSetLookup(stopList, &tmp);
free(tmp);
Run Code Online (Sandbox Code Playgroud)
我认为char word []和char*基本上是一样的.唯一的区别是char字[1024]在堆栈中具有固定长度1024,但堆中的tmp仅占用所需的空间(strlen(word)+1).
因此,我不明白为什么我必须在堆中复制字符串才能调用此函数.为什么会这样?char*tmp = strdup("something")和char word [1024] ="something"之间是否存在一些更基本的区别?
我有一个问题拼接两个数组.我们假设我有两个数组:
a = array([1,2,3])
b = array([4,5,6])
Run Code Online (Sandbox Code Playgroud)
我什么时候vstack((a,b))得到
[[1,2,3],[4,5,6]]
Run Code Online (Sandbox Code Playgroud)
如果我这样做,hstack((a,b))我得到:
[1,2,3,4,5,6]
Run Code Online (Sandbox Code Playgroud)
但我真正想要的是:
[[1,4],[2,5],[3,6]]
Run Code Online (Sandbox Code Playgroud)
如何在不使用for循环的情况下完成此操作(需要快速)?