我正在做嵌入式编程,其中节省内存很重要。
以下 C 代码在运行时会占用多少堆栈空间?
if (send_small_message) {
uint8_t buffer[16000];
// do something with the buffer
} else {
uint8_t buffer[32000];
// do something the with buffer
}
Run Code Online (Sandbox Code Playgroud)
某些编译器能否决定为两个缓冲区分配 16000 + 32000 = 48kB 堆栈空间?或者是否保证因为两个缓冲区永远不会同时使用,编译器将只分配 32kB - 较大缓冲区的大小?
跟进问题:
void SendSmallMessage() {
uint8_t buffer[16000];
// do something with the buffer
}
void SendLargeMessage() {
uint8_t buffer[32000];
// do something with the buffer
}
Run Code Online (Sandbox Code Playgroud)
某些编译器编译的代码是否可以在运行时使用 16000 + 32000 字节来执行以下代码段:
if (send_small_message) {
SendSmallMessage();
} else {
SendLargeMessage();
}
Run Code Online (Sandbox Code Playgroud) kubectl create -f deployment.yaml如果我使用以下文件运行命令deployment.yaml,一切都会成功。
apiVersion: v1
kind: Pod
metadata:
name: my_app
labels:
app: my_app
spec:
containers:
- name: my_app
image: docker:5000/path_to_my_custom_image
args: ["my_special_argument"]
Run Code Online (Sandbox Code Playgroud)
但是,现在我想要一个自定义的“my_special_argument”,如下所示
apiVersion: v1
kind: Pod
metadata:
name: my_app
labels:
app: my_app
spec:
containers:
- name: my_app
image: docker:5000/path_to_my_custom_image
args: ["$(ARG)"]
Run Code Online (Sandbox Code Playgroud)
我想在执行命令时以某种方式设置 $ARG$ 的值kubectl create -f deployment.yaml。怎么做?
我正在寻找类似的东西:
kubectl create -f deployment.yaml --ARG=new_arg
这样的命令可以执行吗?
我正在阅读有关C++模板的内容,并希望对比一个函数的两种不同实现,它计算从0到N的和.
不幸的是,我遇到了问题,并希望通过示例解决几个问题:
天真的代码:
#include <stdio.h>
template<int N>
struct Sum {
// Copied the implementation idea from Scott Meyers book
// "Effective C++". Is there a better way?
enum { value = N + Sum<N - 1>::value };
};
template<>
struct Sum<0> {
enum { value = 0 };
};
int main() {
// Works well in this case, but gives compilation error, if
// it's called with a larger value, such as 10000
// (error: template instantiation depth exceeds maximum …Run Code Online (Sandbox Code Playgroud) 使用Google Test,我想测试Server.AcceptRequest方法的行为:
class Server {
public:
// Clients can call this method, want to test that it works
Result AcceptRequest(const Request& request) {
queue_.Add(request);
... blocks waiting for result ...
return result;
}
private:
// Executed by the background_thread_;
void ProcessRequestsInQueue() {
while (true) {
Process(queue_.PopEarliest());
}
}
MySynchronizedQueue queue_;
std::thread background_thread_ = thread([this] {ProcessRequestsInQueue();});
};
Run Code Online (Sandbox Code Playgroud)
该方法接受客户端请求,将其排队,阻止等待结果,并在可用时返回结果。
当后台线程处理队列中的相应请求时,结果可用。
我有一个测试,如下所示:
TEST(ServerTest, TwoRequests) {
Server server;
Result r1 = server.AcceptClientRequest(request1);
Result r2 = server.AcceptClientRequest(request2);
ASSERT_EQ(r1, correctResultFor1);
ASSERT_EQ(r2, correctResultFor2); …Run Code Online (Sandbox Code Playgroud) c++ multithreading unit-testing googletest non-deterministic
考虑以下 C++ 程序:
string construct(string&& s) {
// Passing a r-value reference as an argument to the assignment operator
string constructed = s;
return constructed;
}
int main() {
string original = "Hello";
string temp1 = construct(std::move(original));
printf("%s\n", original.c_str()); // Prints "Hello", so original has not changed
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在我执行的一个小变化是在 r 值引用参数上调用 std::move :
string constructWithMove(string&& s) {
// Passing a cast to an r-value reference using r-value reference as an argument.
string constructed = std::move(s);
return constructed;
} …Run Code Online (Sandbox Code Playgroud) 我正在编写Java软件,它有一个单独的线程,可以监听被按下的外部按钮.如果按下按钮,则线程通知其他线程,否则它只是休眠.
我的模型是使用中断驱动的设计.理想情况下,只要没有按下按钮,我就想让线程休眠.当按下按钮时,我希望线程做一些工作然后再回去睡觉.
任何人都可以确认/更正以下实施吗?
// This is a code that interrupt-driven thread will execute
public void run() {
while (true) {
try {
Thread.sleep(1000); // Sleeps only for 1s. How to sleep indefinitely?
} catch (InterruptedException exception) {
process(exception); // Doing some work
// then going back to sleep using the while loop
}
}
}
Run Code Online (Sandbox Code Playgroud)
此外,在每个按钮点击终端后,我收到一条消息
I/O exception raised from stop()
Run Code Online (Sandbox Code Playgroud)
这条消息意味着什么(例如,如果我发现异常,为什么会打印出来)?我可以避免终端打印吗?
我有一个Java程序,在高层次上看起来像这样:
public static void main(final String[] args) {
Thread t1 = new ComplicatedThread1();
Thread t2 = new ComplicatedThread2();
Thread t3 = new ComplicatedThread3();
t1.start();
t2.start();
t3.start();
}
Run Code Online (Sandbox Code Playgroud)
每个线程都包含很多代码,这些代码不是我编写的.一个线程中可能有几个子线程.如果只有一个线程崩溃(即抛出未捕获的运行时异常),则会出现最大的问题.在这种情况下,整个程序仍在运行,但它出现故障.
既然我无法使每个线程变得万无一失,我想在任何线程崩溃时完全关闭JVM .
问题:如何在任何线程引发的主程序中捕获运行时异常并要求系统关闭?
我想从M包含至少一个负数的矩阵中删除这些列.例如,如果
M = (1 0 0 1)
(1 -1 0 2)
(2 3 4 -3)
Run Code Online (Sandbox Code Playgroud)
我希望M成为
M = (1 0)
(1 0)
(2 4)
Run Code Online (Sandbox Code Playgroud)
如何输入 M <- removeNegativeColumns(M) 代码?
我是汇编编程(x86 32 位架构)的新手,对以下代码有疑问:
SECTION .data
Msg: db "Hello", 10
Len: equ $-Msg
SECTION .text
global _start
_start:
; Printing Msg to stdout
mov eax, 4
mov ebx, 1
mov ecx, Msg ; Passing the ADDRESS to the beginning of what's stored in Msg
mov edx, Len ; Are we passing the address of Len, or the value of Len?
int 80H
; Terminating
mov eax, 1
mov ebx, 0
int 80H
Run Code Online (Sandbox Code Playgroud)
有人告诉我,mov ecx, Msg指令将Msg存储位置的地址移动到ecx寄存器中。 …
我们公司买了一个专有的 C 函数:我们有一个编译库ProcessData.a和一个接口文件来调用它:
# ProcessData.h
void ProcessData(char* pointer_to_data, int data_len);
Run Code Online (Sandbox Code Playgroud)
我们想在ARM嵌入式 CPU上使用这个函数,我们想知道它可能使用多少堆栈空间。
问题:如何测量任意函数的堆栈使用情况?
到目前为止,我尝试的是实现以下辅助功能:
static int* stackPointerBeforeCall;
void StartStackMeasurement(void) {
asm ("mov %0, sp" : "=r"(stackPointerBeforeCall));
// For some reason I can't overwrite values immediately below the
// stack pointer. I suspect a return address is placed there.
static int* pointer;
pointer = stackPointerBeforeCall - 4;
// Filling all unused stack space with a fixed constant
while (pointer != &_sstack) {
*pointer = 0xEEEEEEEE;
pointer--; …Run Code Online (Sandbox Code Playgroud) c++ ×3
assembly ×2
c ×2
java ×2
arm ×1
c++11 ×1
callstack ×1
command-line ×1
dimensions ×1
embedded ×1
googletest ×1
jvm ×1
kubectl ×1
kubernetes ×1
matrix ×1
move ×1
nasm ×1
r ×1
stack ×1
stack-size ×1
unit-testing ×1
x86 ×1