我在D中理解模板时遇到一些麻烦.
我理解struct Foo(T) { }类或函数的作用或等价物是什么,但是什么是template Bar(T) { }?它与类,结构或函数模板有何不同,何时使用?
我正在尝试使用手动内存管理从昂贵的对象管理生命周期,在我的单元测试期间,我似乎destroy(bar)在本示例中使用下方的main方法中的访问冲突导致我的程序崩溃.这是我遇到访问冲突的问题的最小示例.
我不明白出了什么问题.
class Foo { int i;}
struct Bar
{
Foo _p;
this(Foo foo)
{
_p = foo;
}
~this() {
import core.stdc.stdlib : free;
if (_p !is null)
{
destroy(_p);
free(cast(void*)_p);
_p = null;
}
}
}
void main(string[] argv)
{
import std.conv;
import core.stdc.stdlib;
Foo foo = emplace(cast(Foo) malloc(Foo.sizeof));
Bar bar = Bar(foo);
destroy(bar);
}
Run Code Online (Sandbox Code Playgroud) 我使用Dim的Deimos openssl标头将D链接到OpenSsl,并使用ldc 1.8.0编译器,尝试将字符串加密为小测试.加密的字节数组与我的预期不一致.当我运行程序并加密字符串并在之后对其进行解密后,我将原始字符串恢复.但是中间加密字节数组在代码执行之间不一致.
所以我的问题是,这是预期的行为,并且OpenSsl中的AES是否会为内容添加某种盐,因此攻击更难,或者这是我的错误?
import std.stdio;
import std.conv;
import std.string;
import std.outbuffer;
import deimos.openssl.aes;
void main()
{
writeln("hello world");
const auto encryption_passphrase = "foo!";
writeln("The encryption key is \"" ~ encryption_passphrase ~ "\"");
const auto encryption_content = "bar";
writeln("The to be encrypted content is: \"" ~ encryption_content ~ "\"");
writeln("The content lenght is " ~ encryption_content.length.to!string);
writeln("----------");
writeln("encrypting");
AES_KEY encryption_key;
AES_set_encrypt_key(cast(ubyte*) encryption_passphrase.toStringz, 128, &encryption_key);
OutBuffer buf = new OutBuffer();
buf.write(encryption_content);
ubyte[] inbuffer = buf.toBytes();
ubyte[] encryptedbuffer = new ubyte[inbuffer.length];
AES_encrypt(&inbuffer[0], …Run Code Online (Sandbox Code Playgroud) 我正在努力学习d所以我从hello world开始,并尝试扩展它.
import std.stdio;
import core.thread;
void main(string[] args){
writeln("Hello World!");
Thread.sleep(dur!("seconds")(5));
writeln("Press enter key to exit...");
writeln(readln());
}
Run Code Online (Sandbox Code Playgroud)
所以我希望我的输出如下
Hello World!
Press enter key to exit...
//input "abcd"
abcd
Run Code Online (Sandbox Code Playgroud)
但我得到了这个
//input "abcd"
Hello World!
Press enter key to exit....
abcd
Run Code Online (Sandbox Code Playgroud)
睡眠功能甚至被跳过.怎么了?