可能重复:
有关malloc和sizeof的新手问题
我正在尝试将字符串读入程序.当我注意到字符串有时被破坏时,我尝试了以下代码:
void *mallocated = malloc(100);
printf("sizeof(mallocated) = %d\n", sizeof(mallocated));
Run Code Online (Sandbox Code Playgroud)
根据我的程序,大小mallocated是8,即使我为它分配了100个字节.因此,每当我尝试存储长度超过8个字节的字符串时,第8个字节后的所有内容有时会消失.为什么会发生这种情况,我该如何预防呢?
(我不确定"flag"是否是我正在寻找的词,但我会解释它.)
我正在尝试编译一个使用GMP大数字库的程序.但是为了能够使用GMP进行编译,我必须添加-lgmp到命令的末尾.例如,如果我想编译"program.c",我必须输入gcc program.c -lgmp.这很容易从命令行,但我不知道如何在Xcode中做到这一点.如何lgmp在使用Xcode时添加标志?
Eclipse编译器会自动将乘法乘以2的幂转换为位移,还是应该手动执行?谢谢您的帮助.
我有一个Java BigInteger类的问题:我无法将大值粘贴到BigInteger中.例如,假设我想为此数字指定一个BigInteger:
26525285981219105863630848482795
我无法直接分配它,因为编译器认为它是一个整数:
val bi = 26525285981219105863630848482795 //compile error
Run Code Online (Sandbox Code Playgroud)
但我希望它是一个BigInteger.有没有办法可以直接将其粘贴到源代码中?如果没有这种方式,那么在Scala中是否有一种方法,它有一个更容易使用的BigInt类?谢谢您的帮助.
我试图在C中实现一个单独链接的列表.你看到在互联网上浮动的常见实现是类似的
typedef struct {
int head;
Node *tail;
} Node;
Run Code Online (Sandbox Code Playgroud)
用像这样的方法
Node cons(int head, Node tail) {
Node y;
y.head = head;
y.tail = malloc(sizeof(Node));
*y.tail = tail;
}
Run Code Online (Sandbox Code Playgroud)
表现非常重要.有没有办法在C中实现比这更快的链表?例如,摆脱内存分配(y.tail = malloc(sizeof(Node)))应该会显着提高速度.
我使用Eclipse和Dvorak-QWERTY命令(Mac选项).我正在运行OS 10.6.但是当我按下命令键时,它不会恢复到QWERTY键盘,而是保持为Dvorak.这使得键盘快捷键更难以使用.我该如何解决这个问题?
多长时间需要用C来声明一个变量,例如int x或unsigned long long var?我想知道它是否会使我的代码更快地在这样的东西.
for (conditions) {
int var = 0;
// code
}
Run Code Online (Sandbox Code Playgroud)
这样做会更快,还是更容易?
int var;
for (conditions) {
var = 0;
// code
}
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助.
I am allocating memory in a C program using malloc. It's possible for my program to allocate more memory than the system has room for, at which point the program crashes. For my purposes it would be better if malloc would just return NULL (like it's apparently supposed to), so I can catch the error. Instead what it does is it throws an error saying "No memory available to program now: unsafe to call malloc." And crashes the …
标题大多是不言自明的:链表比二叉树有什么优势?我能想到的唯一一个链表更有效的情况是迭代每个元素,在这种情况下,它仍然非常接近.看起来二进制树在访问数据和插入新元素方面都更快.那么为什么要使用链表呢?
我正在使用GMP,我希望能够快速转换mpz为mpf.我查看了图书馆,找不到多少.我能想到的最好的事情就是:
mpz_t x;
/* Insert code here that assigns some value to x */
char buf[SIZE];
gmp_sprintf(buf, "%Zd", x);
mpf_t y;
mpf_set_str(y, buf);
Run Code Online (Sandbox Code Playgroud)
此解决方案需要重复转换为字符串.此外,它受到限制SIZE,我认为没有办法预先决定SIZE需要多大.有没有更好的方法来进行这种转换?
我正在尝试在Haskell中编写一个函数来计算满足谓词的列表中的元素,True如果数量超过某个阈值则返回.我有一个看起来像这样的实现:
hitsThreshold :: Int -> (a -> Bool) -> [a] -> Bool
hitsThreshold threshold test strs =
(length $ filter test strs) >= threshold
Run Code Online (Sandbox Code Playgroud)
问题是,我希望这个懒惰地评估,以便它在长度达到阈值时立即终止.例如,我应该能够传入一个无限列表,它应该在有限的时间内终止(假设最终达到阈值).有一个简单的方法吗?
c ×6
eclipse ×2
gmp ×2
java ×2
linked-list ×2
malloc ×2
optimization ×2
bigint ×1
biginteger ×1
binary-tree ×1
command ×1
crash ×1
declaration ×1
dvorak ×1
haskell ×1
macos ×1
performance ×1
scala ×1
sizeof ×1
variables ×1
xcode ×1