这可能是一个基本问题,可能已经被问过(比方说,这里); 但我还是不明白.所以,让我问一下.
考虑以下C++类:
class Obj{
char* str;
public:
Obj(char* s){
str = s;
cout << str;
}
~Obj(){
cout << "Done!\n";
delete str; // See the comment of "Loki Astari" below on why this line of code is bad practice
}
};
Run Code Online (Sandbox Code Playgroud)
以下代码片段之间的区别是什么:
Obj o1 ("Hi\n");
Run Code Online (Sandbox Code Playgroud)
和
Obj* o2 = new Obj("Hi\n");
Run Code Online (Sandbox Code Playgroud)
为什么前者调用析构函数,但后者不调用(没有显式调用delete
)?
哪一个更受欢迎?
OpenSSL文档声明它可以安全地用于多线程应用程序,前提是至少设置了两个回调函数,locking_function和threadid_func ....
我编写了使用OpenSSL API的程序.而且,我知道如何使用pthreads.但是,OpenSSL文档是以手册的形式编写的,我无法看到在多线程应用程序中使用OpenSSL时我必须要做的一步一步的指导.
是否有关于使用OpenSSL和pthreads的教程?(我在网上搜索,但没有出现满意的结果.)
PS:我在Debian Lenny和Ubuntu Lucid/Maverick工作.
PS2: OpenSSL包含一个示例,但它开始时太复杂了.
我想通过交换方法使用Spring RestTemplate发送 HTTP 请求。
第三个参数是 的一个实例HttpEntity
,它允许设置请求的标头/正文。我尝试了以下代码片段:
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
public class Connector {
public static void main(String[] args) {
HttpHeaders headers = new HttpHeaders();
headers.set("Host", "www.example.com");
headers.set("User-Agent", "whatever");
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.exchange(
"http://httpbin.org/headers", HttpMethod.GET,
new HttpEntity<String>(null, headers), String.class);
System.out.println(responseEntity.getBody());
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,http : //httpbin.org/headers是一个简单的 HTTP 请求和响应服务,它(在本例中)返回 HTTP 标头。
运行Java代码的结果如下:
{
"headers": {
"Accept": "text/plain, */*",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "whatever"
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,User-Agent
设置为我想要的,但 …
我正在使用Java开发Web应用程序,它需要与数据库建立连接.良好的资源管理非常重要.应用程序将在Tomcat 6 servlet容器中,我已经实现了BoneCP来管理连接(我不能使用Spring).
我已经读过DataSource的JNDI查找过于昂贵,而且我正在考虑创建一个DataSource对象的单例,只获取一次JNDI资源,并为将来的连接返回相同的DataSource.
问题:仅创建一次DataSource并从同一DataSource获取连接是一个好主意吗?我不想获得相同的连接,只有相同的DataSource.
谢谢 ;)
请考虑以下代码片段,它只是将内容写入someByteBuffer
标准输出:
// returns an instance of "java.nio.channels.Channels$WritableByteChannelImpl"
WritableByteChannel w = Channels.newChannel(System.out);
w.write(someByteBuffer);
Run Code Online (Sandbox Code Playgroud)
Java指定通道通常旨在对多线程访问安全,而缓冲区对于多个并发线程使用是不安全的.
所以,我想知道上面的代码片段是否需要同步,因为它write
在一些缓冲区(它不是线程安全的)上调用一个通道的方法(应该是线程安全的).
public int write(ByteBuffer src) throws IOException {
int len = src.remaining();
int totalWritten = 0;
synchronized (writeLock) {
while (totalWritten < len) {
int bytesToWrite = Math.min((len - totalWritten),
TRANSFER_SIZE);
if (buf.length < bytesToWrite)
buf = new byte[bytesToWrite];
src.get(buf, 0, bytesToWrite);
try {
begin();
out.write(buf, 0, bytesToWrite);
} finally {
end(bytesToWrite > …
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.SecureRandom;
public class AES_Mod_Speed {
// AES parameters
private static final int AES_KEY_SIZE = 128; // in bits
private static final int AES_COUNTER_SIZE = 16; // in bytes
private static final int GCM_NONCE_LENGTH = 12; // in bytes. 12 is the recommended value.
private static final int GCM_TAG_LENGTH = 16 * 8; // in bits
public static void main(String[] args) throws Exception {
SecureRandom sr = new SecureRandom(); …
Run Code Online (Sandbox Code Playgroud) 考虑这个片段:
@ParameterizedTest
@ValueSource(strings = {"a", "b", "c"})
void test(final String line) {
// code here
}
Run Code Online (Sandbox Code Playgroud)
这将是一个实际测试,但为了简单起见,假设其目的只是打印以下内容:
Line 1: processed "a" successfully.
Line 2: processed "b" successfully.
Line 3: failed to process "c".
Run Code Online (Sandbox Code Playgroud)
换句话说,我希望测试值的索引可以在测试中访问。根据我的发现,{index}
可以在测试之外使用它来正确命名。
好吧,我读了几篇关于可移植可执行文件(PE)的Matt Pietrek的文章,比如:
另外,我已经阅读了关于这个主题的一些其他来源.要么是我忽略了某些部分,要么就是那里没有回答问题.
那么,这里有一些问题:
众所周知,在加载EXE时,Windows Loader从Importa地址表(IAT)读取导入的DLL列表,并将它们加载到进程地址空间.
进程地址空间是一个虚拟空间.DLL可能已经加载到某个物理空间中.对于像KERNEL32.dll
或等的DLL,会发生这种情况USER32.dll
.物理地址和虚拟地址之间有什么关系?加载器是仅分配页面并复制DLL,还是引用?
如果没有加载DLL,Loader是加载整个DLL,还是仅加载所需的函数?例如,如果您使用了函数foo()
from bar.dll
,那么加载器是否会将整个加载bar.dll
到进程地址空间中?或者,它只是将foo
代码加载到进程地址空间?
假设您的EXE文件使用驻留的函数MessageBox()
from .您是否可以开发自定义,将其放在与EXE文件相同的目录中,并期望您的应用程序调用您的自定义而不是系统默认值?USER32.DLL
%WINDIR%\system32\user32.dll
USER32.DLL
MessageBox
MessageBox
我正在通过UDP编程网络协议,在Linux中使用C/C++.该协议必须提供可靠性,因此我将模拟像TCP上的TCP重传这样的东西.
这可以使用pthreads或fork来完成,但我认为这是一种过度杀伤并消耗大量系统资源.更好的方法是利用调度程序.
我可能无法使用Linux内部调度程序,因为我在用户空间编程.是否有标准的C/C++库来实现这一目标?第三方图书馆怎么样?
编辑:有人问为什么我这样做.为什么不使用TCP呢?
答案是,因为我正在实施隧道协议.如果有人通过TCP隧道TCP,效率将大幅下降.这里有更多信息为什么TCP over TCP是一个坏主意.
我正在Linux下编写一个C/C++客户端 - 服务器程序.假设将消息m从客户端发送到服务器.
在发送m之前,客户端是否可以读取将携带m的数据包的TCP序列号?
实际上,我想将此序列号附加到m,并发送生成的数据包.(嗯,事情变得更复杂,但让我们保持这么简单.事实上,我想将身份验证信息应用于此序列号,然后将其附加到m.)
此外,
是否有可能服务器读取携带m的数据包的TCP序列号?
java ×5
c ×2
c++ ×2
linux ×2
buffer ×1
channel ×1
concurrency ×1
constructor ×1
destructor ×1
dll ×1
encryption ×1
jndi ×1
junit5 ×1
new-operator ×1
openssl ×1
performance ×1
pthreads ×1
reference ×1
rest ×1
resttemplate ×1
scheduling ×1
singleton ×1
sockets ×1
spring ×1
tcp ×1
windows ×1