问题列表 - 第318068页

我在这里正确解释了 C 的操作顺序吗?

我对CPPReference说 postincrement\xe2\x80\x99s 值评估在其副作用之前排序这一事实感到困惑,但 preincrement 没有这样的保证。

\n

我现在想出了一个例子,这很重要,但我不确定我的分析是否正确。

\n

据我了解,这两个程序的不同之处在于第一个包含 UB,而第二个则不包含:

\n
#include <stddef.h>\n#include <stdio.h>\n\nint main(void) {\n    int arr[] = {0, 1, 2};\n    int i = 1;\n    int x = ++arr[arr[i]];\n}\n
Run Code Online (Sandbox Code Playgroud)\n
#include <stddef.h>\n#include <stdio.h>\n\nint main(void) {\n    int arr[] = {0, 1, 2};\n    int i = 1;\n    int x = arr[arr[i]]++;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我对这个表达式的分析++arr[arr[i]]如下:

\n
    \n
  1. 有以下先序关系:\n
      \n
    • 的值计算在 的i值计算之前排序arr[i]
    • \n
    • 的值计算在 的arr[i]值计算之前排序arr[arr[i]]
    • \n
    • 的值计算在 的arr[arr[i]]值计算之前排序++arr[arr[i]]
    • \n
    \n …

c sequence-points language-lawyer order-of-execution

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

如何在 Bash 中迭代关联数组(其中值是数组)?

我编写此代码是为了循环访问 LAN 上的用户名和域。遗憾的是,脚本没有打印任何内容。

#!/bin/bash

construct_array_of_trgts() {
  declare -A usrs_n_dmns
  
  local -a guest_dmns
  local -a usrs=("j" "jim" "o" "root")
  local -a guest_dmns=("raspberrypi" "lenovo")
  for d in "${guest_dmns[@]}"; do 
    PS3="Select the users to include for sshing into $d. Q when done selecting."$'\n'
    local -a targt_usrs
    select u in "${usrs[@]}"; do
      if [[ "$u" ]]; then
        targt_usrs+=("$u")
     elif [[ "$REPLY" == 'q' ]]; then 
      break;
     fi
    done
    usrs_n_dmns["${d}"]="$targt_usrs"
  done
  
}
construct_array_of_trgts

for d in "${!usrs_n_dmns[@]}"; do
  targt_usrs=("${usrs_n_dmns["${d}"]}")
  echo "$usrs_n_dmns"
  for u in "${targt_usrs[@]}"; …
Run Code Online (Sandbox Code Playgroud)

arrays bash for-loop associative-array

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

C 中函数指针的正确语法使用

我已经阅读了有关函数指针语法的内容,但无法在我的代码中工作。

该视频解释了语法是:

type func0(type0 arg0, ..., typen argn){
    // do something
    return a_thing;
}
Run Code Online (Sandbox Code Playgroud)

然后在另一个函数中传递函数指针是:

type func1((*func0)(type0 arg0, ..., typen argn), another args){  
    //do something
    func0(arg0, ..., argn);    
    return a_thing;
}
Run Code Online (Sandbox Code Playgroud)

但是后来,我读了第一个关于C 中的函数指针如何工作?,语法为:

type func1((*func0)(type0 arg0, ..., typen argn), another args) {    
    //do something
    (*func0)(arg0, ..., argn);
    return a_thing;
}
Run Code Online (Sandbox Code Playgroud)

这是有道理的,因为您正在取消引用您正在传递的指针。最后, C 函数指针调用语法中的第一个答案解释了该语法可以是:

&func
func
*func
**func
Run Code Online (Sandbox Code Playgroud)

我不明白哪些信息是正确的。我有代码:

double *func1((*func0)(double), double *var) {
    double *temp = malloc(some_size);
    for(int i = 0; i < some_cycles; ++i) …
Run Code Online (Sandbox Code Playgroud)

c function-pointers

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

如何使用 Python 获得 9 个 0 和 9 个 1 的所有可能顺序?

我希望最终得到一个包含 48 620 个嵌套列表的列表,其中包含不同顺序的 9 个零和 9 个一。

from itertools import permutations

print(list(permutations('000000000111111111', r=18)))
Run Code Online (Sandbox Code Playgroud)

我假设上面的代码有效,但每个0and1都被视为一个单独的符号,因此对于每个排序,我都会得到大量重复:

('0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1')
('0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1')
('0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1')
('0', '0', '0', '0', '0', '0', '0', …
Run Code Online (Sandbox Code Playgroud)

python combinations combinatorics python-itertools python-3.x

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

Python多重继承中调用双亲的构造函数(一般情况)

我试图弄清楚Python中的多重继承,但我找到的所有文章都仅限于简单的情况。让我们考虑以下示例:

class Vehicle:
    def __init__(self, name: str) -> None:
        self.name = name
        print(f'Creating a Vehicle: {name}')

    def __del__(self):
        print(f'Deleting a Vehicle: {self.name}')


class Car(Vehicle):
    def __init__(self, name: str, n_wheels: int) -> None:
        super().__init__(name)
        self.wheels = n_wheels
        print(f'Creating a Car: {name}')

    def __del__(self):
        print(f'Deleting a Car: {self.name}')


class Boat(Vehicle):
    def __init__(self, name: str, n_props: int) -> None:
        super().__init__(name)
        self.propellers = n_props
        print(f'Creating a Boat: {name}')

    def __del__(self):
        print(f'Deleting a Boat: {self.name}')


class Amfibii(Car, Boat):
    def __init__(self, name: str, n_wheels: int, n_props: …
Run Code Online (Sandbox Code Playgroud)

python inheritance super diamond-problem

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

如何在 std::wstringstream 中插入 NULL 字符?

我需要REG_MULTI_SZ在 C++ 中从 .a 表示的值列表构造一个多字符串 () std::vector<std::wstring>。

这是首先想到的,但当然是错误的:

std::wstring GetMultiString(std::vector<std::wstring> Values)
{
    std::wstringstream ss;

    for (const std::wstring& Value : Values) {
        ss << Value;
        ss << L"\0";
    }

    ss << L"\0";

    return ss.str();
}
Run Code Online (Sandbox Code Playgroud)

经检查,返回的字符串没有任何嵌入的 NULL 字符。

c++ std stringstream

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

“memcpy 必须给出有效的指针”:我在标准中哪里可以找到这个?

cppref上指出

如果 或dest是src无效指针或空指针,则行为未定义,即使count为零

memset和也有同样的规定memmove。

我的问题是:标准中哪里有规定?我需要向我的客户证明这是一个 UB,但是指向 cppref 文章的链接不行,我需要标准中的参考。

标准的公开草案

c++ memcpy language-lawyer

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

为什么cudaFree不需要数据结构的地址?

我对工作方式有疑问cudaFree。在下面的代码中,为了在设备上分配数组,cudaMalloc需要数组的地址。这是通过使用&array_d.

int *array_d;
cudaMalloc((void**)&array_d, sizeof(int) * 100);
cudaFree(array_d);
Run Code Online (Sandbox Code Playgroud)

从逻辑上讲,当我们想要释放内存时,我们cudaFree还必须传递数组地址。否则,它如何知道哪部分内存必须被释放?

c memory-management cuda

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

ToString() - 本地化浮点数而不添加不必要的尾随零?

我在使用 ToString() 方法本地化数字时遇到了一些问题:

ToString("Nx")正确本地化所有千位分隔符,但在整数中添加了不必要的尾随 0 ,这在我的 UI 中看起来非常难看。另一方面,类似的东西ToString("0.###")按照我想要的方式处理小数,但它没有正确包含千位分隔符。

我希望字符串保留尽可能多的小数位,但不添加尾随 0 来填充空格。

float a = 1900.42f;

//Expected result: 1,900.42
a.ToString("N2"); //1,900.42 (Perfect!)
a.ToString("0.##"); //1900.42 (Missing separators)

float b = 1900;

//Expected result: 1,900
b.ToString("N2"); //1,900.00 (Localized, unnecessary 0s)
b.ToString("0.##"); //1900 (Missing separators, no 0s)
Run Code Online (Sandbox Code Playgroud)

有没有办法两者兼得呢?目前我正在添加:

float a = 1900.42f;

//Expected result: 1,900.42
a.ToString("N2"); //1,900.42 (Perfect!)
a.ToString("0.##"); //1900.42 (Missing separators)

float b = 1900;

//Expected result: 1,900
b.ToString("N2"); //1,900.00 (Localized, unnecessary 0s)
b.ToString("0.##"); //1900 (Missing separators, …
Run Code Online (Sandbox Code Playgroud)

.net c#

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

如何在 powershell 中执行 ls -d

我需要列出现有目录而不将文件列出到其中:

PS C:\Users\myHomeDIR> cat .\toto.txt
.\.config
C:\DA FI
PS C:\Users\myHomeDIR> cat .\toto.txt | dir | % FullName
C:\Users\myHomeDIR\.config\scoop
dir : Cannot find path 'C:\DA FI' because it does not exist.
At line:1 char:18
+ cat .\toto.txt | dir | % FullName
+                  ~~~
    + CategoryInfo          : ObjectNotFound: (C:\DA FI:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

PS C:\Users\myHomeDIR> 
Run Code Online (Sandbox Code Playgroud)

我期望这个输出(就像 LINUXls -d那样):

C:\Users\myHomeDIR\.config
dir : Cannot find path 'C:\DA FI' because it does not exist.
At line:1 char:18
+ cat …
Run Code Online (Sandbox Code Playgroud)

powershell ls equivalent

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