我刚刚发现C中的一个怪癖,我觉得很困惑.在C语言中,可以在声明结构之前使用指向结构的指针.这是一个非常有用的功能,因为当您处理指向它的指针时,声明是无关紧要的.我刚刚找到一个角落的情况,但是(令人惊讶的是)这不是真的,我无法解释原因.对我来说,这似乎是语言设计中的一个错误.
拿这个代码:
#include <stdio.h>
#include <stdlib.h>
typedef void (*a)(struct lol* etc);
void a2(struct lol* etc) {
}
int main(void) {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
得到:
foo.c:6:26: warning: ‘struct lol’ declared inside parameter list [enabled by default]
foo.c:6:26: warning: its scope is only this definition or declaration, which is probably not what you want [enabled by default]
foo.c:8:16: warning: ‘struct lol’ declared inside parameter list [enabled by default]
Run Code Online (Sandbox Code Playgroud)
要解决此问题,我们可以简单地执行此操作:
#include <stdio.h>
#include <stdlib.h>
struct lol* wut;
typedef void (*a)(struct lol* etc);
void …Run Code Online (Sandbox Code Playgroud) 假设您有一个大型PHP项目,并且在尝试运行它时,您最终会得到一个空白页面.脚本终止,您希望尽可能少地找到确切的位置.
是否有工具/程序/命令/ IDE可以在PHP脚本终止时告诉您脚本退出的位置?
注意:我无法将自己的帖子标记为"已接受的答案",因此请查看底部以查看我的解决方案.如果您想出一个更好的解决方案,我会将您的帖子标记为答案.
我的项目需要一个文件,我将存储应该能够被用户读取和修改的键/值对数据.我希望程序只是期望键存在,我想尽快从文件中解析它们.
我可以将它们存储在XML中,但XML是复杂的方式,它需要遍历节点和子节点等等,我想要的只是一些获取文件并生成键值对的类.我希望尽可能少的错误处理,并希望尽可能少的代码完成它.
我可以自己编写类似的类,但我宁愿在框架中学习它不是两次发明轮子.在.NET(3.5)中是否有一些内置的魔术类能够这样做?
MagicClass kv = new MagicClass("Settings.ini"); // It doesn't neccesarily have to be an INI file, it can be any simple key/value pair format.
string Value1 = kv.get("Key1");
...
Run Code Online (Sandbox Code Playgroud) 在for循环中声明匿名结构的代码在gcc中使用-std = c99/gnu99正常工作
for (struct {int foo; int bar;} i = {0}; i.foo < 10; i.foo++);
Run Code Online (Sandbox Code Playgroud)
但是,当我切换到clang而不是我得到错误:
error: declaration of non-local variable in 'for' loop
Run Code Online (Sandbox Code Playgroud)
为什么这是一个错误?为什么它会允许某些类型(例如"int")而不允许其他类型(例如struct {int foo;})?这似乎不一致.clang是否无法正确实现c99或者代码无效c99和gcc恰好支持它?
有没有人知道在clang支持的for循环中声明多种类型变量的方法?(这对宏很有用.)
编辑:
由于人们问为什么这有用,我会粘贴一些示例代码:
#define TREE_EACH(head, node, field, iterator) for ( \
/* initialize */ \
struct { \
node* cur; \
node* stack[((head)->th_root == 0? 0: (head)->th_root->field.avl_height) + 1]; \
uint32_t stack_size; \
} iterator = {.cur = (head)->th_root, .stack_size = 0}; \
/* while */ \
iterator.cur != 0; \
/* …Run Code Online (Sandbox Code Playgroud) 我正在学习Haskell.我选择这种语言的动机之一是编写具有高度鲁棒性的软件,即完全定义的函数,数学上确定的函数,永不崩溃或产生错误.我并不是指由系统的谓词("系统内存不足","计算机着火"等)引起的故障,这些都不是很有趣,并且可能只是使整个过程崩溃.我也不是指由无效声明(pi = 4)引起的错误行为.
相反,我指的是错误状态导致的错误,我希望通过严格的静态类型使这些状态不可表示和不可编译(在某些函数中)来删除.在我看来,我把这些功能称为"纯粹",并认为强类型系统可以让我实现这一目标.但是,Haskell并没有以这种方式定义"纯",并允许程序error在任何上下文中崩溃.
这是完全可以接受的,并不奇怪.然而,令人失望的是,Haskell似乎没有提供某些功能来禁止可能导致分支使用的函数定义error.
这是一个人为的例子,为什么我觉得这令人失望:
module Main where
import Data.Maybe
data Fruit = Apple | Banana | Orange Int | Peach
deriving(Show)
readFruit :: String -> Maybe Fruit
readFruit x =
case x of
"apple" -> Just Apple
"banana" -> Just Banana
"orange" -> Just (Orange 4)
"peach" -> Just Peach
_ -> Nothing
showFruit :: Fruit -> String
showFruit Apple = "An apple"
showFruit Banana = "A Banana"
showFruit (Orange x) …Run Code Online (Sandbox Code Playgroud) 我正在使用GDB调试我的C程序,它有点不稳定,可能是因为我没有使用glibc,所以它不会检测新线程,直到它们中断.我通过添加一个立即恢复的断点来修复此问题(如果为0则中断).
然而今天我遇到了一堵墙.
我需要execve()非常快,所以普通的fork()是不可能的(将使用大量的内存)我不能vfork()(我需要一个堆栈来设置管道等)所以我使用相同的此库中的方法(https://code.google.com/p/popen-noshell/)基本上只使用CLONE_VM进行克隆.这个(或者通常调用execve() - 我真的不知道)使GDB真的因某种原因而感到困惑 - 基本上它输出的是这样的:
[New LWP 516]
[New LWP 520]
[New LWP 519]
[New LWP 521]
LWP 521 is executing new program: /bin/bash
Error in re-setting breakpoint 1: No source file named xxx.c.
Error in re-setting breakpoint 2: No source file named yyy.c.
Error in re-setting breakpoint 4: Function "zzz_main" not defined.
[LWP 521 exited]
Program received signal SIGTRAP, Trace/breakpoint trap.
[Switching to LWP 519]
0x00000000004307f8 in shell_execve ()
(gdb) info threads
Id Target Id Frame
* …Run Code Online (Sandbox Code Playgroud) 我对这种架构感到沮丧,因为没有明显的解释为什么工作组应该是三维的,或者我还没有找到解释.由于可以从一维工作组模拟任意数量的维度,因此它似乎增加了额外的复杂性,并且使得理解将工作划分为工作组的最佳方式变得更加困难.
我有一个假设是,OpenCL希望工作项id和内存查找之间存在一个微不足道的关系,以允许可以进行I/O优化的可预测内存操作.
我编写了代码来可视化决策树模型。最初,我遇到了诸如未找到 graphviz 的可执行文件之类的错误,但我将其路径添加到环境变量中,甚至重新安装了 graphviz 模块。现在似乎工作得很好。但现在出现以下错误:
Traceback (most recent call last):
File "C:/Ankur/Python36/Python Files/Decision_Tree.py", line 57, in
<module>
Image(graph.create_png())
TypeError: 'module' object is not callable
Run Code Online (Sandbox Code Playgroud)
代码如下。
from sklearn import tree
from io import StringIO
from PIL import Image
from graphviz import Graph
import pydotplus as py
# Code for creating the model and fitting the data.
#...........................
dot_data=StringIO()
tree.export_graphviz(clf,out_file=dot_data)
graph=py.graph_from_dot_data(dot_data.getvalue())
Image(graph.create_png())
Run Code Online (Sandbox Code Playgroud)