小编pmg*_*pmg的帖子

关于旋风的问题

在维基百科上读到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,还是必须下载新的编译器?

visual-studio

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

用于创建DLL的编程语言:C++或C#

这不是编程怀疑!

我打算为某些应用程序编写DLL.我有两个选择可供选择:C++或C#我应该用哪种语言编写DLL?

这会影响功能吗?

我是一个完全新手并且不知道C++和C#(但是C#中的一些小程序).

在C++或C#中编写DLL的优点和缺点是什么?

非常感谢您的宝贵时间!

此致,Swanand!

c# c++ dll

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

printf for struct?(C/C++,VC2008)

只需在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的私有成员

c++ printf struct field

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

将以文本模式打开的整个文件读入字符串变量的最佳方法

这些是我无法改变的:

  • 语言是C++
  • 但是,文件是用旧的打开的 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但我想知道是否有更好的方法.谢谢.

c++ file

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

包括tr1 :: shared_ptr

我已经包含#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)

c++ gcc g++

5
推荐指数
2
解决办法
9653
查看次数

C指针阵列初始化

我很困惑这两个初始化之间有什么区别:

    int (*p)[10];
Run Code Online (Sandbox Code Playgroud)

    int *p[10]
Run Code Online (Sandbox Code Playgroud)

我知道他们都可以指向2D数组,其行数中的元素数为10 ....

c pointers

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

如何访问 Forth 中的隐藏定义?

当一个单词被重新定义时,是否可以访问旧单词?

想象一下有一个词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)

forth gforth

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

PHP中的C风格变量初始化

PHP中有本地,私有,静态和公共变量这样的东西吗?如果是这样,你能给出每个样本以及如何在课堂内外演示它们的范围吗?

php c++ variables scope

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

使用fread/fwrite作为STL字符串.这是对的吗?

我有一个包含字符串的结构.像这样的东西:

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++ string fwrite fread

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

强制转换是可覆盖的操作吗?如果是这样,如何?

是否可以在 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 对显式转换发出警告?” 和我的建议。

c++ overriding casting

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

标签 统计

c++ ×7

c ×1

c# ×1

casting ×1

dll ×1

field ×1

file ×1

forth ×1

fread ×1

fwrite ×1

g++ ×1

gcc ×1

gforth ×1

overriding ×1

php ×1

pointers ×1

printf ×1

scope ×1

string ×1

struct ×1

variables ×1

visual-studio ×1