我在维基百科上读到Cyclone编程语言是C编程语言的安全方言,因此请考虑以下C代码.
int strlen(const char *s)
{
int iter = 0;
if (s == NULL) return 0;
while (s[iter] != '\0') {
iter++;
}
return iter;
}
Run Code Online (Sandbox Code Playgroud)
此函数假定传入的字符串由NUL('\ 0')终止.但是如果我们传递这样的字符串,
char buf[] = {'h','e','l','l','o','!'}
Run Code Online (Sandbox Code Playgroud)
它会导致strlen
迭代通过不一定与字符串s相关联的内存.所以在Cyclone中还有另一个版本的代码
int strlen(const char ? s)
{
int iter, n = s.size;
if (s == NULL) return 0;
for (iter = 0; iter < n; iter++, s++) {
if (*s == '\0') return iter;
}
return n;
}
Run Code Online (Sandbox Code Playgroud)
我可以在Visual Studio中使用Cyclone,还是必须下载新的编译器?
这不是编程怀疑!
我打算为某些应用程序编写DLL.我有两个选择可供选择:C++或C#我应该用哪种语言编写DLL?
这会影响功能吗?
我是一个完全新手并且不知道C++和C#(但是C#中的一些小程序).
在C++或C#中编写DLL的优点和缺点是什么?
非常感谢您的宝贵时间!
此致,Swanand!
只需在VC2008中构建并运行它:
struct A
{
int a;
int b;
int c;
};
A a = { 10, 20, 30 };
printf("%d %d %d\n", a);
Run Code Online (Sandbox Code Playgroud)
这是正常的吗?
10 20 30
Run Code Online (Sandbox Code Playgroud)
我想投!但它不起作用:
struct A
{
int a;
int b;
int c;
operator int()
{
return a + b + c;
}
};
A a = { 10, 20, 30 };
printf("%d\n", a);
Run Code Online (Sandbox Code Playgroud)
仅输出:
10
Run Code Online (Sandbox Code Playgroud)
我需要自动转换模板实用程序.这是:https://code.google.com/p/boolib/source/browse/boolib/crypt/ShakedValue.h 它应该隐藏在内存中,任何hack-programms(ArtMoney)都找不到值.
还有一个技巧:打印struct/class的私有成员
fopen()
std::string
.线应仅由\n
其他变体分隔,而不是其他变体.string ReadWhole()
{
Seek(0);
char *data = new char[GetSize()];
if (1 != fread(data, GetSize(), 1, mFile))
FATAL(Text::Format("Read error: {0}", strerror(errno)));
string ret(data, GetSize());
delete[] data;
return ret;
}
Run Code Online (Sandbox Code Playgroud)
作为参考,这是GetSize
,但它只返回文件的大小(缓存):
int GetSize()
{
if (mFileSize)
return mFileSize;
const int current_position = ftell(mFile);
fseek(mFile, 0, SEEK_END);
mFileSize = ftell(mFile);
fseek(mFile, current_position, SEEK_SET);
return mFileSize;
}
Run Code Online (Sandbox Code Playgroud)
fread()
失败,因为文件有\r\n
行结尾,它们只计为1个字符而不是2个字符,所以它尝试读取的文件超过了文件中的字符.
我可以解决它,fgets
但我想知道是否有更好的方法.谢谢.
我已经包含#include </usr/include/c++/4.4.3/tr1/shared_ptr.h>
在我的类文件中,当我尝试编译我的类时,我得到以下错误:
> In file included from account.h:16:0:
/usr/include/c++/4.4.3/tr1/shared_ptr.h:61:46: error: '_Lock_policy' has not been declared
/usr/include/c++/4.4.3/tr1/shared_ptr.h:63:30: error: expected template-name before '<' token
/usr/include/c++/4.4.3/tr1/shared_ptr.h:63:30: error: expected '{' before '<' token
/usr/include/c++/4.4.3/tr1/shared_ptr.h:63:30: error: expected unqualified-id before '<' token
/usr/include/c++/4.4.3/tr1/shared_ptr.h:89:12: error: '_Lock_policy' has not been declared
/usr/include/c++/4.4.3/tr1/shared_ptr.h:89:31: error: '__default_lock_policy' was not declared in this scope
/usr/include/c++/4.4.3/tr1/shared_ptr.h:100:12: error: '_Lock_policy' has not been declared
/usr/include/c++/4.4.3/tr1/shared_ptr.h:100:31: error: '__default_lock_policy' was not declared in this scope
/usr/include/c++/4.4.3/tr1/shared_ptr.h:209:7: error: '_Sp_counted_base' does not name a type
/usr/include/c++/4.4.3/tr1/shared_ptr.h: In …
Run Code Online (Sandbox Code Playgroud) 我很困惑这两个初始化之间有什么区别:
int (*p)[10];
Run Code Online (Sandbox Code Playgroud)
和
int *p[10]
Run Code Online (Sandbox Code Playgroud)
我知道他们都可以指向2D数组,其行数中的元素数为10 ....
当一个单词被重新定义时,是否可以访问旧单词?
想象一下有一个词foo
被定义和重新定义
: foo ( n -- 2*n ) 2* ; ok
: foo ( n -- 2*n+1 ) foo 1+ ; redefined foo ok
10 foo . 21 ok
Run Code Online (Sandbox Code Playgroud)
foo
这里执行了两个定义。
是否可以执行第一个定义(“second-foo”)?
21 second-foo . 42 ok
Run Code Online (Sandbox Code Playgroud)
到see
它吗?
see second-foo
: foo
2* ; ok
Run Code Online (Sandbox Code Playgroud) PHP中有本地,私有,静态和公共变量这样的东西吗?如果是这样,你能给出每个样本以及如何在课堂内外演示它们的范围吗?
我有一个包含字符串的结构.像这样的东西:
struct Chunk {
int a;
string b;
int c;
};
所以,我想,我不能使用fread和fwrite函数从文件中读写这个结构.因为字符串可能会保留不同的内存容 但这样的代码工作正常.
Chunk var;
fwrite(&var, sizeof(Chunk), 1, file);
fread(&var, sizeof(Chunk), 1, file);
它真的有些问题吗?
是否可以在 C++ 中覆盖(C 风格)强制转换?
假设我有代码
double x = 42;
int k = (int)x;
Run Code Online (Sandbox Code Playgroud)
我可以让第二行中的演员执行我写的一些代码吗?就像是
// I don't know C++
// I have no idea if this has more syntax errors than words
operator (int)(double) {
std::cout << "casting from double to int" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我问的原因是因为“有没有办法让 gcc 或 clang 对显式转换发出警告?” 和我的建议。