小编Shr*_*roy的帖子

C#条件运算符的返回类型

我有一个名为EmailMessage的对象,它有一个可以为空的System.DateTime字段,名为Timestamp.在我的C#代码中,我有以下几行:

var TS = EmailMessage.Timestamp == null ? System.DateTime.Now : EmailMessage.Timestamp;
Run Code Online (Sandbox Code Playgroud)

为什么.NET 4推断TS的数据类型是System.DateTime?而不是System.DateTime(换句话说,为什么.NET 4认为TS可以为空?)对我而言,TS显然是不可空的.

在此先感谢您的帮助.

c# entity-framework nullable

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

在C++ vector <T>中释放内存

以下是我现在可以构建的最佳"最小工作示例".我想了解以下代码是否泄漏了内存.

// Class CTest
class CTest {
  vector<int> Elements;
  CTest (vector<int>&);
  ~CTest ();
};
CTest::CTest (vector<int>& Elements_) {
  this->Elements = Elements_;
}
CTest::~CTest () {
}

// main
int main (int argc, char *argv[]) {
  vector<CTest> V;
  for (auto i = 0; i < 10; i++) {
    vector<int> U;
    for (auto j = i; j < i + 5; j++) U.push_back (j);
    V.push_back (*(new CTest (U)));
  }
  // Do whatever
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我是否正确地认为,由于delete每次调用都没有相应的调用new,这个程序确实泄漏了内存?

c++ memory-leaks

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

在64位计算机中分配32位整数数组

我有一台64位的i7机器.假设我为n个32位整数分配内存.在分配中实际使用了多少个物理寄存器:n还是n/2?

我试着编写以下简单程序来查找.

#include <iostream>
#include <cstdlib>

using namespace std;

int main (int argc, char *argv[]) {
    int a[4];
    cout << &a[0] << "\t" << &a[3] << endl;
    cin.ignore (1);
    return 0;
} // End main ()
Run Code Online (Sandbox Code Playgroud)

输出是:

0018FA04        0018FA10
Run Code Online (Sandbox Code Playgroud)

它们似乎比它们应该更加分开.为什么不是地址04和07?这是否意味着系统实际上分配了四个(或更多)整数,而不是将四个32位整数打包成两个64位寄存器?

在此先感谢您的帮助.

c++ memory-management

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

Visual Studio 2012试用版中的F#IntelliSense

我一直遇到以下问题:

(System.Console.ReadLine ()).Split [|'('; ')'|]
|> Array.filter (fun s -> not (System.String.IsNullOrEmpty (s)))
|> Array.map (fun s -> s.Split [|','|])
|> Array.map (fun s -> Array.map (fun t -> t.Trim ()) s) (* t.Trim () is underlined with a red squiggly line *)
|> [MORE CODE]
Run Code Online (Sandbox Code Playgroud)

与红色波浪线相关的错误是:

Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to constrain the type of the object. This …

f# type-inference

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

"数组类型有不完整元素类型"是什么意思?

我有以下简单的代码行:

#include <glib.h>
#include <stdio.h>

void my_func () {
    GHashTable htbls[3]; /* ASSUME LINE NUMBER IS N */
    /* Do something */
}

int main (int argc, char *argv[]) {
    my_func ();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

$gcc `pkg-config --cflags --libs glib-2.0` ./main.c
Run Code Online (Sandbox Code Playgroud)

给出以下错误:

./main.c:N: error: array type has incomplete element type
Run Code Online (Sandbox Code Playgroud)

我不明白为什么元素类型不完整. GHashTable是完全指定的glib.h.

在此先感谢您的帮助.

c hashtable glib gcc4

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

如何将F#(double - > double)转换为Func <double,double>?

我正在MathNet.Numerics.LinearAlgebra建立一个图书馆.我需要将一个用户指定的函数应用于矩阵的每个元素,我知道我可以使用它Map:

open System
open MathNet.Numerics.LinearAlgebra
open MathNet.Numerics.LinearAlgebra.Double

let m1 = matrix [[1.0; 2.0; 3.0]]
let f1 = fun s -> s * 3.14
let m2 = m1.Map f1 // THIS FAILS
let m3 = m1.Map (fun s -> s * 3.14) // THIS WORKS!
Run Code Online (Sandbox Code Playgroud)

m2我的行中得到以下错误:

This expression was expected to have type Func<float, 'a> but here has type double -> double
Run Code Online (Sandbox Code Playgroud)

但我需要能够传递映射函数,而不是将其定义为内联m3.该文档MathNet.Numerics似乎没有解决我的问题.

在此先感谢您的帮助.

f# mathnet-numerics math.net

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

实现文件中未正确理解签名文件中的列表声明

我有以下签名文件F.fsi:

module M =

    type T1 = A | B

    type T2 =
        | F
        | G
        static member x1 : list<T1>
        static member x2 : list<T1>
Run Code Online (Sandbox Code Playgroud)

在我的实现文件中,F.fs我有以下内容:

module M =

    type T1 = A | B

    type T2 =
        | F
        | G
        static member x1 = [T1.A; T1.B]
        static member x2 = [] // LINE MARKER 1; ERROR OCCURS HERE
Run Code Online (Sandbox Code Playgroud)

Visual Studio x2使用以下错误消息对行的定义进行红线处理:

模块'M'包含static member M.T2.x2 : obj list但其签名指定static member …

f# types

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

关于C#中线程的文档含糊不清

在阅读Joe Albahari的优秀着作"C#中的线程"时,我遇到了以下含糊不清的句子:

线程安全类型不一定使程序使用它是线程安全的,并且后者涉及的工作通常使前者冗余.

(您可以在此页面上找到该句子;只需搜索"不确定性"即可快速跳转到相应的部分.)

我希望使用ConcurrentDictionary来实现某些线程安全的数据结构.段落告诉我ConcurrentDictionary不保证对我的数据结构的线程安全写入吗?有人可以提供一个反例,显示线程安全类型实际上无法提供线程安全吗?

在此先感谢您的帮助.

c# multithreading thread-safety

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