小编snr*_*snr的帖子

Java 10中"var"的优点/缺点

AFAIK,var不是Java中的关键字.它是保留类型名称.我想知道在什么情况下我们应该使用/避免它.它的用法是否有原则?

import java.util.HashMap;

public class Test {
    public static void main(String[] args) {
        var x = new HashMap<> (); // legal
        var y = new HashMap<String, Integer>(); // legal

        var z = "soner"; // legal
        System.out.println(z);

        var umm = null;  // xx wrong xx //
        var foo; // xx wrong usage without initializer xx //

        var var = 5; // legal

    }
}
Run Code Online (Sandbox Code Playgroud)

java java-10

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

为什么 IEnumerable 的 Where 将第一个参数作为“this”传递

我刚刚开始研究 LINQ。以下示例使用Wherewhich 是标准查询运算符之一

string[] names = { "Tom", "Dick", "Harry" };
IEnumerable<string> filteredNames = System.Linq.Enumerable.Where(names, n => n.Length >= 4);
Run Code Online (Sandbox Code Playgroud)

我对它的工作原理做了一些研究,并找到了这个来源

public static partial class Enumerable
{
    public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) {
        if (source == null) throw Error.ArgumentNull("source");
        if (predicate == null) throw Error.ArgumentNull("predicate");
        if (source is Iterator<TSource>) return ((Iterator<TSource>)source).Where(predicate);
        if (source is TSource[]) return new WhereArrayIterator<TSource>((TSource[])source, predicate);
        if (source is List<TSource>) return new WhereListIterator<TSource>((List<TSource>)source, predicate);
        return new WhereEnumerableIterator<TSource>(source, …
Run Code Online (Sandbox Code Playgroud)

.net c# linq arrays

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

fork()中的父ID和提前终止

我正在练习使用并行编程fork().我希望每个进程的父ID都是前进程的id,但在我的输出中显示相同的id.为什么?第二个问题是关于终止.有时输出显示所有进程,有时只有两个或三个,有时只有一个.为什么?我知道父母的过程应该等待它的孩子,但如果不是在我的问题中.我的困惑是,在fork()调用时,两个进程都是在不知道命令的情况下执行的,不是吗?也许父进程终止自己的执行.但它的孩子可以继续跑步到其余的节目或终止或其他什么?(因为它可以在输出上看到并不总是终止,也不总是完全正确)我不理解输出只显示一个但有时两个或三个或不是全部.我希望我能解释一下我的问题.看来,我做不到.

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

int main (int argc, char *argv[]) {
   pid_t childpid = 0;
   int i;

   for (i = 0; i < 3; i++)
      if (childpid = fork())
         break;

   fprintf(stderr, "i:%d  process ID:%ld  parent ID:%ld  child ID:%ld\n",
           i, (long)getpid(), (long)getppid(), (long)childpid);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出(S):

i:0  process ID:2783  parent ID:1954  child ID:2784
i:1  process ID:2784  parent ID:1  child ID:2785
i:2  process ID:2785  parent ID:1  child ID:2786 …
Run Code Online (Sandbox Code Playgroud)

c parallel-processing posix fork

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

给定循环的时间复杂度O(logn)或O(n)

//loop1
for (int i = 1; i <= n; i*=2) { }
//loop2
for (int i = 1; i <= logn; i++) { }
Run Code Online (Sandbox Code Playgroud)

我们和我的朋友争论我认为第一个是O(logn)后者的循环而后者是循环O(n).然而,对于后者,他说它O(logn)也不是O(n).

你能解释一下吗?

algorithm complexity-theory big-o time-complexity

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

C 中的线程安全与原子性

据我所知,可以调用线程安全函数而无需互斥或信号量。他们不能吗?

例如,strcpy()orstrdup()是一种线程安全函数。

但是当我阅读手册页时,我看到了以下内容,但不明白这句话以及粗体示例。

MT 安全并不意味着函数是原子的,也不意味着它使用 POSIX 向用户公开的任何内存同步机制。甚至有可能按顺序调用 MT-Safe 函数不会产生 MT-Safe 组合。 例如,让一个线程依次调用两个 MT 安全函数并不能保证与这两个函数的组合的原子执行等效的行为,因为其他线程中的并发调用可能会以破坏性方式进行干扰。

线程函数中以下用法是否错误?如果是的话,错误的地方是什么?如果不是,加粗的引述是什么意思?

char *s1 = calloc(14, 1);
char *s2 = calloc(6, 1);
char *s3 = strdup("soner");
char *s4 = strdup("stackoverflow");
strcpy(s2, s3);
strcpy(s1, s4);
s1[13] = s2[5] = 0;

mutex_lock(&mtx);
printf("%s %s", s1, s2);
fflush(stdout);
mutex_unlock(&mtx);

free(s1);
free(s2);
free(s3);
free(s4);
Run Code Online (Sandbox Code Playgroud)

c unix multithreading thread-safety

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

混淆确定数组的元素编号

我从讲师那里听说,sizeof(某些指针)总是在机器上相等.例如,如果它是64位,则所有指针都是8字节,在32位4字节中.所以,sizeof当我想指定数组的大小时,我喜欢使用

int arr[] = {2, 5, -1, 10, 9};
int size = sizeof(arr) / sizeof(int)
Run Code Online (Sandbox Code Playgroud)

我假定上面的代码的int需要4,所以20/4给出了元件的数目arr.

int arr[] = {2, 5, -1, 10, 9};
int size = sizeof(arr) / sizeof(int*)
Run Code Online (Sandbox Code Playgroud)

我也假设sizeof(some pointer)take 4,所以20/4再次给出了元素的数量arr.

当我将它应用于double数组时,我很困惑.我认为它应该不起作用.但是,它有效.

double arr[] = {2, 5, -1, 10, 9};
int size = sizeof(arr) / sizeof(double*);
Run Code Online (Sandbox Code Playgroud)

如果每个指针在一台机器中占用相同的大小,结果怎么样呢?问题可以解释一下吗?编辑

我把*arr和双指针混合在一起

c c++ memory pointers sizeof

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

将类成员传递给void*

class testClass
{
public:
    void set(int monthValue, int dayValue);
    int getMonth( );
    int getDay( );
private:
    int month;
    int day;
};
Run Code Online (Sandbox Code Playgroud)

我有一个简单的课程,如上所示.我尝试将其对象传递给检查它们是否相等的函数.

testClass obj[3];
obj[0].set(1,1);
obj[1].set(2,1);
obj[2].set(1,1);
Run Code Online (Sandbox Code Playgroud)

首先,我试过cout << (obj[0] == obj[1]);但是没有运算符重载,使用模板等是不可能的.所以,我可以使用它们来做但是如何将成员变量传递给void*函数?

bool cmp_testClass(void const *e1, void const *e2)
{
    testClass* foo = (testClass *) e1;
    testClass* foo2 = (testClass *) e2;
    return foo - foo2; // if zero return false, otherwise true
}
Run Code Online (Sandbox Code Playgroud)

我这么想,但我无法解决这个问题.我想比较一下

obj[0].getDay() == obj[1].getDay();
obj[0].getMonth() == obj[1].getMonth();
Run Code Online (Sandbox Code Playgroud)

通过传递.

c++ void-pointers

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

通过预处理器检查是否存在库

有两个库zconf.hunistd.h它们用于至少拿到pid的过程。我通常会在Mac OSX和Ubuntu 18.04上测试我的代码,如果我忘了添加代码,zconf.h最好使用它们(由编译器提供zconf.h)代替unistd.h,如果代码可以工作,就可以了。但是,在前一天,我需要在另一台装有Ubuntu 10或12的计算机上测试代码。它的编译器抱怨没有zconf.h。我想知道是否有一种方法可以检查机器是否zconf.h使用过unistd.h。可以使用像

#ifdef ITS_IF_CONDITION
    #include <zconf.h>
#else
    #include <unistd.h>
Run Code Online (Sandbox Code Playgroud)

c macros c-preprocessor

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

了解“ volatile”关键字和比较的工作方式

如果未使用关键字指定变量volatile,则编译器可能会进行缓存。必须始终从内存访问该变量,否则直到其事务单元结束。我想知道的重点在于装配零件。

int main() {
    /* volatile */ int lock = 999;
    while (lock);
}
Run Code Online (Sandbox Code Playgroud)

x86-64-clang-3.0.0编译器上,其汇编代码如下。

main:                                   # @main
        mov     DWORD PTR [RSP - 4], 0
        mov     DWORD PTR [RSP - 8], 999


.LBB0_1:                                # =>This Inner Loop Header: Depth=1
        cmp     DWORD PTR [RSP - 8], 0
        je      .LBB0_3
        jmp     .LBB0_1


.LBB0_3:
        mov     EAX, DWORD PTR [RSP - 4]
        ret
Run Code Online (Sandbox Code Playgroud)

volatile关键字被注释时,结果如下。

main:                                   # @main
        mov     DWORD PTR [RSP - 4], 0
        mov     DWORD …
Run Code Online (Sandbox Code Playgroud)

c assembly x86-64 volatile

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