所有.
我想链接一个调用malloc()函数的库.但是,我的目标环境是不同的,并
malloc()作为内联函数提供.
如何将库的调用malloc()指向我的目标环境的malloc()例程?
是否可以更改导出的函数名称?如果是这样,我可以my_malloc()先编码并将其导出为malloc()并将库链接到该库:
#include <my_environment.h> // malloc() is inline function declared there
void my_malloc (void) {
malloc (void);
}
Run Code Online (Sandbox Code Playgroud)
更具体地说,该库是来自linux发行版的库,因此它依赖于libc.但我的环境嵌入了一个,没有libc库和malloc(),free()......是定制的实现.一些是内联函数,一些是库函数.
我正在研究一个C程序.有一个函数需要两个指针参数,调用它cmp().cmp()出于说明的原因,我在这里提供了一个简化的替身:
int cmp(struct foo *a, struct foo *b)
{
return a->bar == b->bar;
}
Run Code Online (Sandbox Code Playgroud)
我想制作一个NULL-check宏,如下所示:
#define SAFE_CMP(a,b) (((a) != NULL && (b) != NULL) ? cmp((a),(b)) : 0)
Run Code Online (Sandbox Code Playgroud)
我觉得这很好.但是,在使用-Wall以及将警告视为错误的compliation开关进行编译时,以下代码很麻烦:
int baz(struct foo *a)
{
struct foo b;
/* ... */
return SAFE_CMP(a, &b);
}
Run Code Online (Sandbox Code Playgroud)
因为gcc警告"b的地址永远不会为NULL".
有没有办法解决这种情况?具有多种辅助宏象SAFE_CMP_1(safe_arg,unsafe_arg)和SAFE_CMP_2(unsafe_arg,safe_arg)等是我想要的最后一件事.我想让一个助手宏适用于所有情况.
输入文件由两行组成,每行包含许多数字
1 2 3 4...
5 6 7 8...
Run Code Online (Sandbox Code Playgroud)
我想处理每一行的数据,如下所示:
doSomething :: [Int] -> [Int] -> Int
doSomething [] y = 0 -- stop execution. I'm concerted in memory behavior only.
doSomething (x:xs) y = doSomething xs y
main = do
inputdata <- getContents
let (x:xs) = lines inputdata
firstLine = map (read ::String->Int) $ words $ x
secondLine = map (read ::String->Int) $ words $ head xs
print $ doSomething firstLine secondLine
Run Code Online (Sandbox Code Playgroud)
当我运行此程序时,堆分析显示如下:

如果我不使用secondLine(xs),则此程序以恒定内存运行.firstLine处理列表的每个条目然后由GC丢弃.

为什么消耗的内存如此之大?我看到分配的内存量大约为100MB,但实际输入数据大小为5MB. …
当我尝试编写最短路径算法时,我遇到了一个奇怪的事情.后floydWarshall功能以阵列形式产生adjecency矩阵,main函数试图阵列多次查询(在replicateM_循环中).
我发现我的代码非常慢.所以我放在traceShow "doing"顶部floydWarshall并重新运行以发现每次res ! (start,end)调用都是floydWarshall重复的.
为什么每次重新生成数组?
完整源代码输入:https: //gist.github.com/cwyang/27ab81bee731e6d01bb3a7483fdb748e
floydWarshall :: AdjMatrix (Maybe Int) -> AdjMatrix (Maybe Int)
floydWarshall am = traceShow "doing" $ runST $ do
arr <- thaw am :: ST s (STArray s (Vertex,Vertex) (Maybe Int))
sequence_ [ go arr k i j | k <- r, i <- r, j <- r]
freeze arr
where ((minb,_), (maxb,_)) = bounds am
r …Run Code Online (Sandbox Code Playgroud) 我发现emacs24的etags不支持OCaml,exuberant-ctags的ctags不能很好的为ocaml生成标签(缺少标识符),opam自带的otags在Ocaml 4.06上不支持,只支持4.01版本。
生成 TAG 文件以浏览 Ocaml 代码库的最推荐方式是什么,尤其是对于 emacs?
我在理解Haskell懒惰评估方面遇到了困难.
我写了简单的测试程序.它读取4行数据,第二和第四输入行有很多数字.
consumeList :: [Int] -> [Int] -> [Int]
consumeList [] _ = error "hi" -- to generate heap debug
consumeList (x:xs) y = consumeList xs y
main = do
inputdata <- getContents
let (x:y:z:k:xs) = lines inputdata
s = map (read ::String->Int) $ words $ k
t = []
print $ consumeList s t
Run Code Online (Sandbox Code Playgroud)
words并且map在懒惰的字符流上执行,该程序使用常量内存.

但是当我添加论点时t,情况就会发生变化.我的期望是,因为t是map和words懒惰流,而t不是在使用consumeList,这种变化不应该改变的内存使用情况.但不是.
consumeList :: [Int] -> [Int] -> [Int]
consumeList …Run Code Online (Sandbox Code Playgroud) 我正在努力与Bash变量扩展.请参阅以下代码:
~/tmp 689$ a=~/Library/Application\ *; echo $a
/Users/foo/Library/Application *
~/tmp 690$ echo ~/Library/Application\ *
/Users/foo/Library/Application Scripts /Users/foo/Library/Application Support
Run Code Online (Sandbox Code Playgroud)
由于扩展的顺序是brace-> tilde-> parameter - > ....-> pathname,为什么路径名扩展不应用于$a第二个命令中的相同方式?
[添加]
空格逃逸是否隐藏了以下输出的行为?
~/tmp 705$ a=~/Library/Application*; echo $a
/Users/foo/Library/Application Scripts /Users/foo/Library/Application Support
Run Code Online (Sandbox Code Playgroud) 为什么第一个失败而后者成功编译?
我期望foo并且foo'是等价的,也就是说,foo'它只是一个无点函数foo:
foo :: [a] -> [a] -> [(a,a)]
foo = map id . zip
foo' :: [a] -> [a] -> [(a,a)]
foo' a b = map id $ zip a b
Run Code Online (Sandbox Code Playgroud)
但foo失败并出现以下错误:
Couldn't match type ‘[b0] -> [(a, b0)]’ with ‘[b]’
Expected type: [a] -> [b]
Actual type: [a] -> [b0] -> [(a, b0)]
Relevant bindings include
foo :: [a] -> [b] (bound at <interactive>:26:5)
Probable cause: ‘zip’ is …Run Code Online (Sandbox Code Playgroud)