小编Jon*_*fer的帖子

为什么crypt()函数没有内存泄漏?

来自crypt(3) - Linux手册页:

char *crypt(const char *key, const char *salt);
Run Code Online (Sandbox Code Playgroud)

返回值:返回指向加密密码的指针.出错,NULL返回.

由于除非给出key和salt,否则返回值是未知的,这应该是动态分配的内存,但valgrind不同意.

c crypt

5
推荐指数
1
解决办法
690
查看次数

Curl&Wget返回响应,浏览器超时

我正在将请求发送到云上的特定服务器:

wget --header="Host: example.com" http://x.x.x.x:80/
curl -i -H"Host: example.com" http://x.x.x.x:80/
Run Code Online (Sandbox Code Playgroud)

并且它完全按预期返回(一个简单的静态文件)。但是,当我尝试在浏览器中访问它时,请求超时。我无法想象这将是一个用户代理标头问题,但是话又说回来,我真的不知道还会是什么。

它不会去负载均衡器或任何东西,应该直接去站点。为什么会发生这种情况的任何想法?我将主机文件设置为转到该特定IP地址。

谢谢

browser linux redhat timeout wget

5
推荐指数
1
解决办法
827
查看次数

为什么这个按位运算返回30而不是384?

我正在使用Dev-C++编译器.这个程序应该打印30但是它的打印384.

#include <stdio.h>

int main() {
    int n = 3;
    int ans;

    ans = n<<3 + n<<1;
    printf("%d", ans);

    getch();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c dev-c++ bitwise-operators

4
推荐指数
1
解决办法
95
查看次数

Cython 直接访问全局变量

如何在不使用访问器函数的情况下访问用 Cython 声明的全局变量?

我尝试了以下示例:

pyfunktionen_a.pyx

import numpy as np

cdef extern from "funktionen_a.h":
    cdef void setValue(int value_to_set)
    cdef int readValue()
    cdef int value

def pysetValue (_value):
    setValue(_value)

def pyreadValue():
    print readValue()

def manipulateValue(value_to_set):
    value = value_to_set
Run Code Online (Sandbox Code Playgroud)

funktionen_a.c

#include "funktionen_a.h"


void setValue(int value_to_set){

    value = value_to_set;
}

int readValue(){
    return value;
}
Run Code Online (Sandbox Code Playgroud)

funktionen_a.h

#include <Python.h>
#include <stdio.h>


void setValue(int value_to_set);
int readValue();

int value;
Run Code Online (Sandbox Code Playgroud)

有了这个功能,我控制了整个事情:

控件.py

import pyfunktionen_a

pyfunktionen_a.pysetValue(8)
pyfunktionen_a.pyreadValue()

pyfunktionen_a.manipulateValue(5)
pyfunktionen_a.pyreadValue()
Run Code Online (Sandbox Code Playgroud)

期望什么结果:

>>    8
>>    5
Run Code Online (Sandbox Code Playgroud)

但是我得到了 …

python variables global cython

4
推荐指数
1
解决办法
6042
查看次数

在 Rust 中生成悬空引用(而不是解除引用)如何导致未定义的行为,并且可以观察到吗?

Rust 参考Rustonomicon都明确指出“生成”悬空引用是未定义的行为。

以这段代码片段为例:

fn main() {
    let p: std::ptr::NonNull<u8> = std::ptr::NonNull::dangling();
    #[allow(unused_variables)]
    let r: &u8 = unsafe { std::mem::transmute::<_, _>(p) };
}
Run Code Online (Sandbox Code Playgroud)

在操场上通过 Miri 运行它会产生:

error: Undefined Behavior: constructing invalid value: encountered a dangling reference (address 0x1 is unallocated)
 --> src/main.rs:4:27
  |
4 |     let r: &u8 = unsafe { std::mem::transmute::<_, _>(p) };
  |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a dangling reference (address 0x1 is unallocated)
  |
  = help: this indicates a bug in the program: …
Run Code Online (Sandbox Code Playgroud)

undefined-behavior rust

4
推荐指数
2
解决办法
278
查看次数

使用Python.h编译错误gcc

我试图在c ++中嵌入python,我一直在玩一些示例代码.我正在使用boost python解释器,它工作正常,但现在我似乎无法编译一些使用Python.h的c ++代码.我得到一个错误似乎是没有正确引用库(这段代码应该工作,因为它直接从http://www.codeproject.com/Articles/11805/Embedding-Python-in-CC-复制第一部分).我已经尝试了很多标志进行编译.任何帮助都感激不尽!谢谢 :)

以下是我收到的一个示例和错误:

g ++ -Wall -o call_function call_function.c

call_function.c: In function âint main(int, char**)â:
call_function.c:61:56: warning: format â%dâ expects argument of type âintâ, but argument 2 has type âlong intâ [-Wformat]
/tmp/ccAUMMHm.o: In function `main':
call_function.c:(.text+0x2a): undefined reference to `Py_Initialize'
call_function.c:(.text+0x3d): undefined reference to `PyString_FromString'
call_function.c:(.text+0x4d): undefined reference to `PyImport_Import'
call_function.c:(.text+0x5d): undefined reference to `PyModule_GetDict'
call_function.c:(.text+0x7b): undefined reference to `PyDict_GetItemString'
call_function.c:(.text+0x8b): undefined reference to `PyCallable_Check'
call_function.c:(.text+0xb2): undefined reference to `PyTuple_New'
call_function.c:(.text+0xe5): undefined …
Run Code Online (Sandbox Code Playgroud)

c++

3
推荐指数
1
解决办法
6967
查看次数

在c中使用memset()

我写了一个小程序来习惯memset()操作:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

int main()
{
    int arr[10], i;
    int t = INT_MAX;
    memset(arr, t, sizeof(arr));
    for (i = 0; i < 10; i++)
        printf("%d\t",arr[i]);

    printf("%d",t);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上述计划的结果是:

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

2147483647
Run Code Online (Sandbox Code Playgroud)

memset()上述程序的行为是什么?为什么要将数组元素设置为-1

c memset

3
推荐指数
1
解决办法
1023
查看次数

通过写入__FILE__动态更改运行代码?

我知道了一种使用__FILE__宏在C中打印正在运行的代码的源代码的方法.因此,我可以寻找位置并使用putchar()来改变文件的内容.

是否可以使用此方法动态更改正在运行的代码?

c

3
推荐指数
2
解决办法
192
查看次数

在 lxml 中定义默认命名空间(无前缀)

使用 lxml 渲染 XHTML 时,一切都很好,除非您碰巧使用 Firefox,它似乎无法处理以命名空间为前缀的 XHTML 元素和 javascript。虽然Opera是能够执行JavaScript(这适用于jQuery和MathJax)的罚款,无论XHTML命名空间是否具有前缀(h:在我的情况)或没有,Firefox中的脚本将用奇怪的错误(中止this.head是未定义的MathJax 的情况)。

我知道这个register_namespace函数,但它既不接受None也不""作为命名空间前缀。我_namespace_maplxml.etree模块中听说过,但是我的 Python 抱怨这个属性不存在(版本问题?)

有没有其他方法可以删除 XHTML 名称空间的名称空间前缀?请注意str.replace,正如在另一个相关问题的回答中所建议的那样,不是我可以接受的方法,因为它不知道 XML 语义并且可能很容易搞砸结果文档。

根据请求,您会发现两个可以使用的示例。一种带有命名空间前缀一种没有. 第一个将在 Firefox 中显示 0(错误),第二个将显示 1(正确)。Opera 将正确呈现两者。这显然是一个 Firefox 错误,但这只是作为想要使用 lxml 的无前缀 XHTML 的一个基本原理——还有其他很好的理由来减少移动客户端的流量等(h:如果你考虑数十或数百个 html 标签,甚至很多) .

python xslt xhtml lxml namespaces

2
推荐指数
1
解决办法
2540
查看次数

如何指定系统默认的serif和sans-serif font-family?

当我十多年前学习 CSS 时,使用“默认字体”(无论这意味着什么)的标准(也是唯一)方法是:

font-family: serif;
font-family: sans-serif;
Run Code Online (Sandbox Code Playgroud)

然后,去年,Apple为其新系统字体添加了自定义语法,如果我没记错的话,Blink 也做了类似的事情。

更了解 CSS 的人能否总结font-family一下当我只想要默认的 sans-serif 或 serif 字体时我的属性应该是什么样子?(我特别不想要 webfonts。)

css fonts

2
推荐指数
1
解决办法
1722
查看次数