小编Hri*_*sto的帖子

从不兼容的指针类型分配

我已经设置了以下结构:

typedef struct _thread_node_t {
    pthread_t thread;
    struct thread_node_t *next;
} thread_node_t;
Run Code Online (Sandbox Code Playgroud)

......然后我定义了:

// create thread to for incoming connection
thread_node_t *thread_node = (thread_node_t*) malloc(sizeof(thread_node_t));
pthread_create(&(thread_node->thread), NULL, client_thread, &csFD);

thread_node->next = thread_arr; // assignment from incompatible pointer type

thread_arr = thread_node;
Run Code Online (Sandbox Code Playgroud)

thread_arr的位置 thread_node_t *thread_arr = NULL;

我不明白为什么编译器在抱怨.也许我误会了什么.

c struct pointers typedef

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

创建指向文件的指针数组

我如何在C中创建一个文件指针数组?
我想创建一个指向main的参数的文件指针数组...如a1.txt,a2.txt等...所以我会让./prog arg1.txt arg2.txt arg3.txt程序使用这些文件.
然后主要的论点是char **argv

从argv,我想创建文件/文件指针数组.这就是我到目前为止所拥有的.

FILE *inputFiles[argc - 1];
int i;
for (i = 1; i < argc; i++)
    inputFiles[i] = fopen(argv[i], "r");
Run Code Online (Sandbox Code Playgroud)

c io file argv

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

C中的结构数组

我正在尝试创建一个结构数组,也是一个指向该数组的指针.我不知道阵列有多大,所以它应该是动态的.我的结构看起来像这样:

typedef struct _stats_t
{
 int hours[24]; int numPostsInHour;
 int days[7]; int numPostsInDay;
 int weeks[20]; int numPostsInWeek;
 int totNumLinesInPosts;
 int numPostsAnalyzed;

} stats_t;
Run Code Online (Sandbox Code Playgroud)

...我需要为每个文件(未知数量)提供多个这些结构,我将分析.我不知道该怎么做.由于数组大小的限制,我不喜欢以下方法:

# define MAX 10
typedef struct _stats_t
{
 int hours[24]; int numPostsInHour;
 int days[7]; int numPostsInDay;
 int weeks[20]; int numPostsInWeek;
 int totNumLinesInPosts;
 int numPostsAnalyzed;

} stats_t[MAX];
Run Code Online (Sandbox Code Playgroud)

那我该怎么创建这个数组呢?此外,指向此数组的指针会看起来像这样吗?

stats_t stats[];
stats_t *statsPtr = &stats[0];
Run Code Online (Sandbox Code Playgroud)

c arrays struct

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

在C++中声明"全局"变量时,"静态"到底意味着什么?

这是我前一个问题范围的扩展.

什么是"静态",它是如何使用的,以及在处理C++时使用"静态"的目的是什么?

谢谢.

c++ static static-variables

5
推荐指数
3
解决办法
3922
查看次数

如何在Java中修改对象时迭代它?

可能重复:
Java:高效等效于在迭代集合时删除从迭代中
删除集合中的项目

我试图循环HashMap:

Map<String, Integer> group0 = new HashMap<String, Integer>();
Run Code Online (Sandbox Code Playgroud)

...并提取每个元素group0.这是我的方法:

// iterate through all Members in group 0 that have not been assigned yet
for (Map.Entry<String, Integer> entry : group0.entrySet()) {

    // determine where to assign 'entry'
    iEntryGroup = hasBeenAccusedByGroup(entry.getKey());
    if (iEntryGroup == 1) {
        assign(entry.getKey(), entry.getValue(), 2);
    } else {
        assign(entry.getKey(), entry.getValue(), 1);
    }
}
Run Code Online (Sandbox Code Playgroud)

这里的问题是每次调用assign()都会从中删除元素group0,从而修改其大小,从而导致以下错误:

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
    at java.util.HashMap$EntryIterator.next(HashMap.java:834)
    at java.util.HashMap$EntryIterator.next(HashMap.java:832) …
Run Code Online (Sandbox Code Playgroud)

java exception hashmap concurrentmodification

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

在加权二叉树中查找最重的长度约束路径

UPDATE

我制定了一个我认为在O(n*k)运行时运行的算法.下面是伪代码:

routine heaviestKPath( T, k )

    // create 2D matrix with n rows and k columns with each element = -?
    // we make it size k+1 because the 0th column must be all 0s for a later 
    // function to work properly and simplicity in our algorithm
    matrix = new array[ T.getVertexCount() ][ k + 1 ] (-?);

    // set all elements in the first column of this matrix = 0
    matrix[ n ][ 0 ] = 0; …
Run Code Online (Sandbox Code Playgroud)

theory algorithm binary-tree

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

在java中创建一个枚举/最终类

我正在试图找出创建一个类的最佳方法,该类的唯一目的是成为全局静态变量的容器.这是一个伪代码,只是一个简单的例子,我的意思是......

public class Symbols {
   public static final String ALPHA = "alpha";
   public static final String BETA = "beta";

   /* ...and so on for a bunch of these */
}
Run Code Online (Sandbox Code Playgroud)

我不需要构造函数或方法.我只需要通过调用以下方式就可以从任何地方访问这些"符号":Symbols.ALPHA;

需要 String的实际值,所以我不能使用枚举类型.实现这一目标的最佳方法是什么?

java enums final global-variables static-variables

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

逐块下载文件客户端块

我正在使用WebRTC将文件发送到已连接的对等方,并且正在将文件分块发送。但是,我在弄清楚如何让对等方逐块地流式传输时保存/下载文件时遇到了麻烦。

我在网上找到的所有示例都建议这样做:

// sender
dataConnection.send({
   'file': file
});

// receiver
dataConnection.on('data', function(fileData) {

    var dataView = new Uint8Array(fileData);
    var dataBlob = new Blob([dataView]);
    var url = window.URL.createObjectURL(dataBlob);

    // create <a>
    var link = document.createElement('a');
    link.href = url;
    link.download = fileName;
    document.body.appendChild(link);

    // trigger the download file dialog
    link.click();
}
Run Code Online (Sandbox Code Playgroud)

但是,这种方法不支持获取文件的大块并在出现每个大块时对其进行写...它必须等待整个文件在发送方读取并发送到接收方。

我想做的是这样的:

// sender
for (var i = 0; i < fileSize; i += chunkSize) {

    var fileReader = new FileReader();

    // read next chunk
    var blob = file.slice(start, end);
    ... …
Run Code Online (Sandbox Code Playgroud)

html5 blob chunking fileapi webrtc

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

跨域请求后如何访问 iframe.contentDocument 以获得响应?

我成功地将文件从 发送localhost:8888localhost:8080(生产中的不同域),但在传输完成后我无法读取 HTTP 响应。

未捕获的安全错误:无法从“HTMLIFrameElement”读取“contentDocument”属性:阻止来源为“ http://localhost:8888 ”的框架访问来源为“ http://localhost:8080 ”的框架。请求访问的框架将“document.domain”设置为“localhost”,但正在访问的框架没有设置。两者都必须将“document.domain”设置为相同的值才能允许访问。

为了发送文件,为了兼容性支持,我试图让它适用于<form>基于文件上传;没有XHR根据。这是基本的 HTML 结构:

<form target="file-iframe" enctype="multipart/form-data" method="POST" action="invalid">
  <input type="file" id="file-input" class="file-input" title="select files">
</form>
<iframe src="javascript:false;" id="file-iframe" name="file-iframe"></iframe>
Run Code Online (Sandbox Code Playgroud)

要将<iframe>元素插入到 DOM 中,我执行以下操作:

document.domain = document.domain;
var domainHack = 'javascript:document.write("<script type=text/javascript>document.domain=document.domain;</script>")';

var html = '<iframe id="file-iframe" name="file-iframe"></iframe>';
var parent = document.getElementById('wrapper');
var iframe = UTILS.createDomElement(html, parent);
iframe.src = domainHack;

UTILS.attachEvent(iframe, 'load', function(e) {

  // this throws the above SecurityError
  var doc = …
Run Code Online (Sandbox Code Playgroud)

javascript iframe cross-domain same-origin-policy

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

如何使用附加属性扩展 Flow 函数类型

流程 v0.57

我想定义 2 个函数类型,FooFancyFoo帮助类型检查以下模式:

const fooFn = () => ({ id: '...', type: '...' });

fooFn['HELLO'] = 'WORLD';
fooFn['test'] = () => {};

...

// now I can do this:
fooFn();
fooFn.HELLO;
fooFn.test();
Run Code Online (Sandbox Code Playgroud)

第一个函数类型看起来很简单:

type Foo = () => {
  id :string,
  type :string
}
Run Code Online (Sandbox Code Playgroud)

我一直在想第二个。我不清楚如何构建函数类型。

我尝试使用交集:

type FancyFoo = Foo & {
  HELLO :string,
  test :() => mixed
}
Run Code Online (Sandbox Code Playgroud)

然而,这种方法失败了:

// Errors:
2: type FancyFoo = Foo & {
                         ^ property `HELLO` of object …
Run Code Online (Sandbox Code Playgroud)

javascript types flowtype

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