所以我想知道命令行参数是否总是以空值终止?Google似乎是肯定的,并且在GCC上进行编译表明情况确实如此,但我可以保证这一切始终是真的吗?
int main(int argc, char** argv)
{
char *p;
for(int cnt=1; cnt < argc; ++cnt)
{
p = argv[cnt];
printf("%d = [%s]\n", cnt, p);
}
return 0;
}
$ MyProgram -arg1 -arg2 -arg3
1 = -arg1
2 = -arg2
3 = -arg3
Run Code Online (Sandbox Code Playgroud) 我用Google搜索,似乎无法找到这个简单问题的答案.
使用遗留代码库(最近移植到Linux,并慢慢更新到新的编译器),我看到了很多
int myfunction(...)
{
// no return...
}
Run Code Online (Sandbox Code Playgroud)
我知道函数的隐式返回TYPE是int,但是当没有指定返回时,隐式返回VALUE是什么.我已经测试过并且得到了0,但这只是用gcc.这个编译器是特定的还是标准定义为0?
编辑:12/2017调整接受的答案基于它引用更新版本的标准.
看一些旧代码,我们有很多类似的东西:
// This is dumb
string do_something(int in)
{
stringstream out;
try
{
out << std::fixed << in;
}
catch(std::exception &e)
{
out << e.what();
}
return out.str();
}
// Can't we just do this? Can this ever fail?
string do_something_better(int in)
{
stringstream out;
out << std::fixed << in;
return out.str();
}
Run Code Online (Sandbox Code Playgroud)
当stringstream读取一个原语时,它是否会抛出一个异常?读字符串怎么样?
我一直在阅读MVP模式,并且无法找到Java Swing代码示例.有没有人知道使用这种模式的开源项目或者我可以找到这样一个例子的地方?(我不打算使用框架来帮助实现模式,只是一个例子.)
我有一些遗留代码可以在所有地方执行此操作:
int fd; // open a file
if(fd == -1)
{
close(fd);
}
Run Code Online (Sandbox Code Playgroud)
这对我来说非常错误.
关闭无效的文件描述符是否有效?
我正在尝试替换旧的C宏:
// Copy the contents of the character array b into character array a. If the source
// array is larger than the destination array, limit the amount of characters copied
#define STRCPY(a,b) if(b) strncpy(a, b, sizeof(a) < sizeof(b) ? sizeof(a) : sizeof(b))
Run Code Online (Sandbox Code Playgroud)
我希望模板能有所帮助.也许这样的事情,但它不起作用.
template <typename T, size_t N, typename TT, size_t M>
void scopy(T (dest[N]), const TT (src[M]))
{
strncpy(dest, src, N < M ? N : M);
}
int main()
{
char foo[10] = "abc";
char bar[5] = …Run Code Online (Sandbox Code Playgroud) 继承了一个C++项目.我正在使用gcc 4.1.2通过makefile在RHEL 5.5中构建.该项目是巨大的(数百个文件),一般来说代码非常好.但是,在编译期间,我经常会收到GCC警告:
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/allocator.h: In constructor ‘std::allocator<_Alloc>::allocator() [with _Tp = char]’:
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/allocator.h:97: warning: will never be executed
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/allocator.h:97: warning: will never be executed
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/ext/new_allocator.h: In constructor ‘__gnu_cxx::new_allocator<_Tp>::new_allocator() [with _Tp = char]’:
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/ext/new_allocator.h:65: warning: will never be executed
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/allocator.h: In destructor ‘std::allocator<_Alloc>::~allocator() [with _Tp = char]’:
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/allocator.h:105: warning: will never be executed
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/ext/new_allocator.h: In destructor ‘__gnu_cxx::new_allocator<_Tp>::~new_allocator() [with _Tp = char]’:
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/ext/new_allocator.h:72: warning: will never be executed
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/allocator.h: In copy constructor ‘std::allocator<_Alloc>::allocator(const std::allocator<_Alloc>&) [with _Tp = char]’:
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/allocator.h:100: warning: will never be …Run Code Online (Sandbox Code Playgroud) 我正在修复我继承的遗留项目中的编译器警告。新编译器是 gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)。
他们有很多代码如下:
#include <cstdio>
#include <cstring>
struct foobar
{
char field1[10];
char field2[5];
};
int main()
{
struct foobar foo;
memset(&foo, ' ', sizeof(foo));
strncpy(foo.field1, "1234567890", sizeof(foo.field1));
// Produces warning
printf("[%.*s]", sizeof(foo.field1), foo.field1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这会产生警告消息:
1_test.c: In function ‘int main()’:
1_test.c:16:49: warning: field precision specifier ‘.*’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("[%.*s]", sizeof(foo.field1), foo.field1);
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎是错误的,因为 '.*' 应该期待 size_t 但显然它没有......
除了必须执行以下操作之外,还有没有办法在全球范围内解决此问题:
// …Run Code Online (Sandbox Code Playgroud) 我继承了一个大型的c ++代码库,并最终使它可以在64位Red Hat 7(gcc版本4.8.5 20150623)上编译和运行。
默认情况下,项目中的makefile是针对c ++ 98编译的。我想打开c ++ 11,因为它显然提供了许多语言上的好处。g ++ 4.8 完全支持 c ++ 11。
我的问题是:这会带来什么样的风险?我需要注意什么?什么类型的东西可能会破坏(除了不编译之外)?
我的google-foo在这方面的水平很低,因为我已经搜索过,但是还没有找到任何对此类型的分析。(也许因为我真的没有任何风险,我只是不知道。)
我想设置apt-get在我的ubuntu盒子上使用代理.我已成功配置synaptic以使用代理,因此我可以安装软件包,但我希望能够使用命令行.
我的工作代理需要用户名和密码,两者都有特殊字符.
在我的.bashrc我
export http_proxy="http://user@company:P@$$1234@10.20.30.40:80/"
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.
我也试过逃避特殊字符,但这似乎也不起作用:
export http_proxy="http://user\@company:P\@\$\$1234@10.20.30.40:80/"
Run Code Online (Sandbox Code Playgroud) c++ ×5
c ×3
apt-get ×1
argv ×1
file-io ×1
function ×1
g++ ×1
g++4.8 ×1
gcc-warning ×1
gcc4 ×1
java ×1
legacy-code ×1
linux ×1
printf ×1
stringstream ×1
swing ×1
ubuntu-10.04 ×1