我看过一些海报说这strdup是邪恶的.对此有共识吗?我使用它没有任何内疚感,并且没有理由比使用malloc/ 更糟糕memcpy.
我能想到的唯一可能strdup是声名远扬的是调用者可能会滥用它(例如,没有意识到他们必须释放返回的内存;尝试strcat到strdup'ed字符串的末尾).但是,malloc的字符串也没有被滥用的可能性.
感谢对那些认为这个问题无益的人的回复和道歉(投票结束).总结一下这些答复,似乎没有普遍的感觉strdup本身就是邪恶的,但是普遍的共识是它可以像C的许多其他部分一样被不恰当地或不安全地使用.
真的没有'正确'的答案,但为了接受一个,我接受了@nneoneo的答案 - 它同样可能是@R ..的答案.
有人能把我说对吗?我正在使用链接,-lIOKit但显然也需要另一个库。
Undefined symbols for architecture x86_64:
"___CFConstantStringClassReference", referenced from:
CFString in code-9daAw9.o
"_kCFBooleanTrue", referenced from:
_dimDisplayNow in code-9daAw9.o
Run Code Online (Sandbox Code Playgroud)
这是代码(可在http://www.cocoabuilder.com/archive/cocoa/191807-sleep-display.html找到)
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
static int dimDisplayNow(void)
{
io_registry_entry_t r =
IORegistryEntryFromPath(kIOMasterPortDefault,
"IOService:/IOResources/IODisplayWrangler");
if(!r) return 1;
int err = IORegistryEntrySetCFProperty(r, CFSTR("IORequestIdle"),
kCFBooleanTrue);
IOObjectRelease(r);
return err;
}
int main(int argc, char **argv)
{
dimDisplayNow();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我收到错误:
error: no matching function for call to 'A::A()'
note: candidates are: A::A(const A&)
note: A::A(const std::string&, size_t)
Run Code Online (Sandbox Code Playgroud)
由此:
#include <map>
#include <string>
using std::map;
using std::string;
class A {
public:
string path;
size_t size;
A (const string& p, size_t s) : path(p), size(s) { }
A (const A& f) : path(f.path), size(f.size) { }
A& operator=(const A& rhs) {
path = rhs.path;
size = rhs.size;
return *this;
}
};
int main(int argc, char **argv)
{
map<string, A> mymap;
A …Run Code Online (Sandbox Code Playgroud) 如果我编译一个空的 C 函数
void nothing(void)
{
}
Run Code Online (Sandbox Code Playgroud)
在 MacOS 上使用gcc -O2 -S(和clang),它会生成:
_nothing:
pushq %rbp
movq %rsp, %rbp
popq %rbp
ret
Run Code Online (Sandbox Code Playgroud)
为什么不gcc删除除 之外的所有内容ret?这似乎是一个简单的优化,除非它真的做了一些事情(对我来说似乎不是)。这种模式(开始时推/移动,最后弹出)在其他非空函数中也是可见的,其中rbp否则这些未使用。
在 Linux 上使用更新的gcc (4.4.5) 我只看到
nothing:
rep
ret
Run Code Online (Sandbox Code Playgroud)
为什么rep?该rep是在非空的功能缺失。
给出以下Python(来自http://norvig.com/sudoku.html)
def cross(A, B):
"Cross product of elements in A and elements in B."
return [a+b for a in A for b in B]
cols = '123456789'
rows = 'ABCDEFGHI'
squares = cross(rows, cols)
Run Code Online (Sandbox Code Playgroud)
这会产生:
['A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'B1', 'B2', 'B3', ...]
Run Code Online (Sandbox Code Playgroud)
作为练习,我想在C++中做同样的事情.目前我有:
#include <iostream>
#include <map>
#include <vector>
using std::string;
using std::vector;
static vector<string> cross_string(const string &A, const string &B)
{
vector<string> result;
for (string::const_iterator itA = A.begin(); itA != A.end(); …Run Code Online (Sandbox Code Playgroud) 这来自Herb Sutter的gotw3(http://www.gotw.ca/gotw/003.htm).
具有以下类别和FindAddr功能......
using std::string;
using std::list;
class Employee
{
public:
Employee(const string& n, const string& a) : name(n), addr(a) { }
string name;
string addr;
};
string FindAddr(const list<Employee>& l, const string& name)
{
string addr;
list<Employee>::const_iterator i = find(l.begin(), l.end(), name);
if (i != l.end()) {
addr = (*i).addr;
}
return addr;
}
Run Code Online (Sandbox Code Playgroud)
我收到编译错误,因为Employee类没有转换为字符串.我可以看到这样的转换不一定是明智的,但为了练习的目的,我添加了一个天真的转换:
string::string(const Employee& e)
{
return e.name;
}
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误:
gotw3.cc:17:9: error: C++ requires a type specifier for all declarations
string::string(const Employee& e)
~~~~~~ …Run Code Online (Sandbox Code Playgroud) 我有一个CSV文件,需要读入R,转置(交换列的行),然后处理.
这是文件的形式(不是列实际延伸到2014):
Year,1970,1971,1972
Variable one,1,2,3
Variable two,11,22,33
Variable three,111,222,333
Run Code Online (Sandbox Code Playgroud)
当我读它时,这些年份的前缀是'X'
> rc <- read.csv("file.csv")
> rc
Year X1970 X1971 X1972
1 Variable one 1 2 3
2 Variable two 11 22 33
3 Variable three 111 222 333
Run Code Online (Sandbox Code Playgroud)
当我转置数据时,所有内容都被视为一个字符串.
> t(rc)
[,1] [,2] [,3]
Year "Variable one" "Variable two" "Variable three"
X1970 " 1" " 11" "111"
X1971 " 2" " 22" "222"
X1972 " 3" " 33" "333"
Run Code Online (Sandbox Code Playgroud)
如果我删除csv文件中行的名称,则日期仍以X为前缀,但转置不会将数据更改为字符串.
那么我该如何正确地做到这一点,以便年份是数字,并且转置不会创建字符串.