小编mer*_*114的帖子

C 标准是否对使用的堆栈空间量有任何保证?

我正在做嵌入式编程,其中节省内存很重要。

以下 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)

c embedded stack

12
推荐指数
2
解决办法
220
查看次数

如何将命令行参数传递给 kubectl create 命令

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

这样的命令可以执行吗?

command-line command-line-arguments kubernetes kubectl

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

c ++支持模板元编程中的最后一次调用优化

我正在阅读有关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)

c++

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

使用 GTest 多次重复多线程测试的正确方法是什么?

使用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

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

在右值引用上调用 std::move 时会发生什么?

考虑以下 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)

c++ move rvalue-reference move-semantics c++11

3
推荐指数
1
解决办法
273
查看次数

Java:如何在中断后将线程重新置于休眠状态?

我正在编写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 multithreading

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

如果一个线程在多线程环境中崩溃,如何关闭JVM?

我有一个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 .

问题:如何在任何线程引发的主程序中捕获运行时异常并要求系统关闭?

java multithreading jvm

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

在R中如何删除包含负数的矩阵的所有列?

我想从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) 代码?

r matrix dimensions identity-column

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

用于打印到标准输出的 `mov edx, Len` 是否将“Len”的值或地址存储到寄存器中?

我是汇编编程(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寄存器中。 …

x86 assembly nasm cpu-registers

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

如何测量 C 中任意函数调用使用的堆栈量?

我们公司买了一个专有的 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 assembly callstack arm stack-size

0
推荐指数
1
解决办法
81
查看次数