Uccntu构建的gcc-6.2((Ubuntu 6.2.0-3ubuntu11~16.04)6.2.0 20160901)不再存在这个问题.
我使用了Ubuntu建立GCC-6.1的[1]((Ubuntu的6.1.1-3ubuntu11〜14.04.1)6.1.1 20160511),GNU binutils的2.24,和libstdc ++与GLIBCXX_3.4.22支持.即使在简单的"hello world"程序中,指定清洁剂也不会强制使用黄金链接器.
#include <iostream>
int main() {
std::cout << "Hello, world!\n";
}
Run Code Online (Sandbox Code Playgroud)
编译和链接
g++ -fsanitize=address -c -o main main.cpp
g++ -fsanitize=address -o main main.o
Run Code Online (Sandbox Code Playgroud)
给出了错误
/usr/bin/ld: unrecognized option '--push-state'
/usr/bin/ld: use the --help option for usage information
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
这表示该-fsanitize
选项未选择黄金链接器.当然,简单的解决方法是-fuse-ld=gold
在链接期间使用,但是在使用消毒剂时,gcc的先前版本不需要这样做.例如,这个代码在gcc-5.3和4.9下完全正常(两者都是Ubuntu版本).是否有其他人在使用非Ubuntu构建的gcc-6.1时遇到此问题?Ubuntu构建是否破损?
[1]使用以下标志构建(gcc-5.3和gcc-4.9的构建只有名称和后缀的差异)
--with-pkgversion='Ubuntu 6.1.1-3ubuntu11~14.04.1'
--with-bugurl=file:///usr/share/doc/gcc-6/README.Bugs
--enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++
--prefix=/usr
--program-suffix=-6
--enable-shared
--enable-linker-build-id
--libexecdir=/usr/lib
--without-included-gettext
--enable-threads=posix
--libdir=/usr/lib
--enable-nls
--with-sysroot=/
--enable-clocale=gnu
--enable-libstdcxx-debug
--enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=gcc4-compatible
--disable-libstdcxx-dual-abi
--enable-gnu-unique-object
--disable-vtable-verify
--enable-libmpx …
Run Code Online (Sandbox Code Playgroud) 我正在编写一个小的包装类open
,它将从文本文件中过滤出特定的行,然后在将它们传递给用户之前将它们拆分为名称/值对.当然,这个过程有助于使用生成器实现.
class special_file:
def __init__(self, fname):
self.fname = fname
def __iter__(self):
return self
def __next__(self):
return self.next()
def next(self):
with open(self.fname, 'r') as file:
for line in file:
line = line.strip()
if line == '':
continue
name,value = line.split()[0:2]
if '%' in name:
continue
yield name,value
raise StopIteration()
Run Code Online (Sandbox Code Playgroud)
for g in special_file('input.txt'):
for n,v in g:
print(n,v)
Run Code Online (Sandbox Code Playgroud)
遗憾的是,我的代码有两个巨大的问题:1)special_file
当它确实需要返回一个元组时返回一个生成器,以及2).我怀疑这两个问题是相关的,但我对生成器和可迭代序列的理解相当有限.我是否遗漏了一些关于实施发电机的痛苦明显的事情?StopIteration()
永远不会引发异常,因此无限期地重复读取文件
我通过将第一个生成器移动到循环外部然后循环遍历它来修复我的无限读取问题.
g = special_file('input.txt')
k = next(g)
for n,v in k: …
Run Code Online (Sandbox Code Playgroud) 我有一些2D数据,我使用pcolormesh显示,我想在上面显示一些轮廓.我使用创建网格化数据
import numpy as np
import matplotlib.pyplot as plt
def bin(x, y, nbins, weights=None):
hist, X, Y = np.histogram2d(x, y, bins=nbins, weights=weights)
x_grid, y_grid = np.meshgrid(X,Y)
return hist, x_grid, y_grid
data = ... # read from binary file
h,x_grid,y_grid = bin(data.x,data.y,512)
# do some calculations with h
h = masked_log(h) # "safe" log that replaces <0 elements by 0 in output
pcm = plt.pcolormesh(x_grid,y_grid,h,cmap='jet')
# Just pretend that the data are lying on the center of the grid …
Run Code Online (Sandbox Code Playgroud)