小编Sta*_*123的帖子

什么是“一元构造函数”?

此链接中,它说:

static_cast 关键字可用于类型之间的任何正常转换。依赖于静态(编译时)类型信息的转换。这包括数字类型之间的任何转换、指针转换和层次结构上的引用、使用一元构造函数的转换转换、使用转换运算符的转换。对于数字类型之间的转换,不会执行运行时检查数据是否适合新类型。即使声明为显式,也将使用一元构造函数进行转换。

也在这里

为了与其他符合 CLS 的语言进行良好的互操作,您可能希望使用相应的 convert-from 运算符包装给定类的每个用户定义的一元构造函数

他们所说的“一元构造函数”是什么意思?

c++ constructor

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

为什么 malloc 在我达到某个阈值之前不分配内存?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
        size_t sz = atol(argv[1]);
        char *arr = malloc(sz);

        sleep(10);
}
Run Code Online (Sandbox Code Playgroud)

我编译了这段代码并尝试运行它,用于pmap查看程序的内存映射。

当我使用一些大数字时1024000,我会得到这样的映射:

3901:   ./alloc_program 1024000
0000560192f43000      4K r---- alloc_program
0000560192f44000      4K r-x-- alloc_program
0000560192f45000      4K r---- alloc_program
0000560192f46000      4K r---- alloc_program
0000560192f47000      4K rw--- alloc_program
0000560192fac000    132K rw---   [ anon ]
00007f75b69e9000   1004K rw---   [ anon ]     <---- I believe this is the allocated memory
00007f75b6ae4000    148K r---- libc-2.31.so
00007f75b6b09000   1504K r-x-- libc-2.31.so
00007f75b6c81000 …
Run Code Online (Sandbox Code Playgroud)

c linux malloc dynamic-memory-allocation

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

运行时检查失败 #4 - 此函数保留的 _alloca 内存周围的堆栈区域已损坏?

#include <iostream>
#include <malloc.h>

void print_vals(int n)
{
    int *arr = (int *)alloca(n);

    for (int i = 0; i < n; i++)
        arr[i] = i;

    for (int i = 0; i < n; i++)
        std::cout << arr[i] << ' ';

    std::cout << '\n';
}

int main()
{
    print_vals(5);
    print_vals(10);
}
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,每次调用都会收到此错误:

Run-Time Check Failure #4 - Stack area around _alloca memory reserved by this function is corrupted
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明 我使用的是 Visual C++ 2019,stdc++14 和 stdc++17 都会产生相同的错误。

这段代码有什么问题?

c++ stack runtime-error alloca

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

为什么使用 System.in 读取没有 ASCII 表示的字符不会给出两个字节的字符?

import java.io.IOException;

public class Main {

    public static void main(String[] args) throws IOException {

        char ch = '?';

        System.out.println((int)ch);

        int c;
        while ((c = System.in.read()) != -1)
        {
            System.out.println(c);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

35830
Run Code Online (Sandbox Code Playgroud)

这里,?Unicode中表示字符的值是35830. 在二进制中,它将是10001011 11110110.

当我在终端中输入该字符时,我希望得到两个字节,10001011并且11110110. 当再次组合它们时,我可以获得原始字符。

但我实际得到的是:

232
175
182
10
Run Code Online (Sandbox Code Playgroud)

我可以看到10代表换行符。但是前3个数字是什么意思?

java system.in character-encoding

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

为什么在这个elf文件中目标OS设置为HP-UX而不是Linux?

这个维基在此处输入图片说明

它指出偏移量 7 处的数字标识目标操作系统。

我已经为 linux 机器编译了 ac 程序并检查elf header了结果文件的(前 64 个字节):

> xxd -l 64 helloworld
00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000  .ELF............
00000010: 0300 3e00 0100 0000 8012 0000 0000 0000  ..>.............
00000020: 4000 0000 0000 0000 183f 0000 0000 0000  @........?......
00000030: 0000 0000 4000 3800 0d00 4000 1f00 1e00  ....@.8...@.....
Run Code Online (Sandbox Code Playgroud)

为什么我得到01第 7 个字节?不应该03吗?

c linux elf

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