小编Col*_*lin的帖子

为什么我们需要虚拟内存?

所以我的理解是每个进程都有自己的虚拟内存空间,范围从0x0到0xFF .... F.这些虚拟地址对应于物理内存(RAM)中的地址.为什么这种抽象级别有用?为什么不直接使用直接地址?

我理解为什么分页是有益的,但不是虚拟内存.

memory operating-system memory-management

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

如何正确使用fork,exec,等待

我正在写的shell需要执行用户给它的程序.这是我的程序的缩短版本

int main()
{
    pid_t pid = getpid(); // this is the parents pid

    char *user_input = NULL;
    size_t line_sz = 0;
    ssize_t  line_ct = 0; 

    line_ct = getline(&user_input, &line_sz, stdin); //so get user input, store in user_input

    for (;;)
    {
        pid_t child_pid = fork(); //fork a duplicate process

        pid_t child_ppid = getppid(); //get the child's parent pid

        if (child_ppid == pid) //if the current process is a child of the main process
        {
            exec(); //here I need to execute …
Run Code Online (Sandbox Code Playgroud)

c shell exec

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

重复一段代码固定次数

我正在尝试重复一段代码,不使用条件,但仍然只重复特定次数.

基本上,这样的事情:

repeat(50)
{
    //Do stuff here.
}
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?除了复制和粘贴50次?

我这样做是因为我想如果我知道有多少次我想重复一些事情,那么每次检查一个条件要快得多.那是准确的吗?或者我还会检查它重复了多少次?

基本上,它是否更快?

c++ performance repeat

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

使用 Catch2 编译多个测试源的正确方法是什么?

我有以下项目结构:

test_main.cc

#define CATCH_CONFIG_MAIN

#include "catch2.hpp"
Run Code Online (Sandbox Code Playgroud)

测试1.cc

#include "catch2.hpp"
#include "test_utils.hpp"

TEST_CASE("test1", "[test1]") {
  REQUIRE(1 == 1);
}
Run Code Online (Sandbox Code Playgroud)

测试2.cc

#include "catch2.hpp"
#include "test_utils.hpp"

TEST_CASE("test2", "[test2]") {
  REQUIRE(2 == 2);
}
Run Code Online (Sandbox Code Playgroud)

test_utils.hpp

#pragma once
#include <iostream>

void something_great() {
  std::cout << ":)\n";
}
Run Code Online (Sandbox Code Playgroud)

如果我使用类似的东西进行编译clang++ -std=c++17 test_main.cc test1.cc test2.cc,则该函数something_great在 test1.o 和 test2.o 中都有定义。这会导致类似的错误

duplicate symbol __Z15something_greatv in:
    test1.cc.o
    test2.cc.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to …
Run Code Online (Sandbox Code Playgroud)

c++ catch2

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

包含类型的类型的模板函数重载

我正在尝试执行以下操作:

#include <iostream>
#include <vector>
#include <tuple>
#include <list>

template <typename T>
void f(T t) {
    std::cout << "1" << std::endl;
}

template <typename T, typename V>
void f(T<std::tuple<V>> t) {
    std::cout << "2" << std::endl;
}

int main() {
    f(std::list<double>{}); // should use first template
    f(std::vector<std::tuple<int>>{}); // should use second template
}
Run Code Online (Sandbox Code Playgroud)

在C++ 14中执行此操作的最简单方法是什么?我认为我可以通过这种方式进行模式匹配,但编译器不会拥有它.

c++ templates template-templates c++14

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

PHP使用&lt; 和&gt; 代替<和>

我下面的在线PHP教程,它使用&lt;&gt;替代<,并>在PHP代码.例如:

&lt;?php

class User {
function __construct($data) {
    $this-&gt;id = (isset($data['id'])) ? $data['id'] : "";
    $this-&gt;username = (isset($data['username'])) ? $data['username'] : "";
    $this-&gt;hashedPassword = (isset($data['password'])) ? $data['password'] : "";
    $this-&gt;email = (isset($data['email'])) ? $data['email'] : "";
    $this-&gt;joinDate = (isset($data['join_date'])) ? $data['join_date'] : "";
}
Run Code Online (Sandbox Code Playgroud)

有没有理由这样做或是教程网站上的格式化导致它显示那样?

即我应该在我的.php文件中使用&lt;&gt;

php

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

无法显示 ImageIcon - 摆动

public class GUI
{
    JFrame frame;
    JPanel squares[][];


    /* Constructor credited to stackoverflow user ranzy
        http://stackoverflow.com/questions/2535417/chess-board-in-java */
    public GUI()
    {
        frame = new JFrame("Chess");
        squares = new JPanel[8][8];
        frame.setSize(500, 500);
        frame.setLayout(new GridLayout(8, 8));
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                squares[i][j] = new JPanel();

                if ((i + j) % 2 == 0) {
                    squares[i][j].setBackground(Color.white);
                } else {
                    squares[i][j].setBackground(Color.orange);
                }
                frame.add(squares[i][j]);
            }
        }

        ImageIcon pawnW = …
Run Code Online (Sandbox Code Playgroud)

java swing intellij-idea imageicon

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

在OCaml中键入错误

我正在尝试构建列表最小值出现的索引列表.

let rec max_index l =
let rec helper inList min builtList index = 
match inList with
| [] -> builtList
| x :: xs ->
    if (x < min) then
        helper xs x index :: builtList index + 1 //line 63
    else
        helper xs min builtList index + 1
in helper l 100000 [] 0;;
Run Code Online (Sandbox Code Playgroud)

第63行给出了以下错误.

Error: This expression has type 'a list -> 'a list
       but an expression was expected of type 'a
       The type variable 'a occurs …
Run Code Online (Sandbox Code Playgroud)

ocaml

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

库函数找到两个列表之间的差异 - OCaml

有库函数可以找到List1 minus elements that appear in List2吗?我一直在谷歌搜索,并没有找到太多.

自己写它似乎并不是微不足道.我编写了一个从列表中删除特定元素的函数,但这更简单:

let rec difference l arg = match l with
| [] -> []
| x :: xs -> 
    if (x = arg) then difference xs arg
    else x :: difference xs arg;;
Run Code Online (Sandbox Code Playgroud)

ocaml

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

基本的SQL查询-其中前缀=

可以这么说,我有一张带有属性make和model的汽车表。

现在,我要查询所有以字母“ Ex”开头的汽车

所以这有点像

SELECT model FROM cars WHERE model = 'Ex'; 
Run Code Online (Sandbox Code Playgroud)

但是我猜这只会返回整个模型名称为Ex的汽车,而不是其模型以Ex开头的汽车。我在这里需要什么查询?

我找不到列出此类基本查询的任何好的资源。谢谢!

sql

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