- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我将元素放入其中时,我试图在空间不足时重新分配结构数组,但我不断收到重新分配的标准错误。 struct 数组最终将包含 235,000 个元素。当我将初始启动大小设置为 100,000 时,我在尝试重新分配时收到了 stderr。如果我将初始启动大小设置为 300,000,它不会给出错误,因为它永远不会到达 realloc 语句。
#define _XOPEN_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#define BUFFERLEN 200
#define START_SIZE 100000
#define GROW 1000
#define TRUE 1
#define FALSE 0
typedef struct{
char *forw;
char *back;
} word;
typedef struct{
char *entry;
} single_words;
FILE *words;
/*-------------Function Prototypes------------*/
void reverse(char* string, char* revstr, int len);
int search_struct(char* find, word* words, int length);
int compare(const void* eventa, const void* eventb);
int length(char* string);
int main(void)
{
char *buffer;
int letter_index[26];
char alpha[] = "abcdefghijklmnopqrstuvwxyz";
int i=0, found = FALSE, strlen=0, letter=0;
word *word_storage;
single_words *output_storage;
int num_words = 0, size = 0;
int num_output = 0, output_size = 0;
/*buffer for the input strings of the words in the input file*/
buffer = (char*) malloc (sizeof(char)*BUFFERLEN);
if(!buffer){
fprintf(stderr, "Error in buffer string mem alloc\n");
exit(1);
}
/*Initializing the array of structs to store the forward and reverse of each word*/
word_storage = (word*) malloc (sizeof(word)*START_SIZE);
if(!word_storage){
fprintf(stderr, "Error in word_storage string mem alloc\n");
exit(1);
}
size = START_SIZE;
/*Initializing the array of structs for the output*/
output_storage = (single_words*) malloc (sizeof(single_words)*START_SIZE);
if(!output_storage){
fprintf(stderr, "Error in output_storage mem alloc\n");
exit(1);
}
output_size = START_SIZE;
/*Set the letter index 0(which is a) to the first character*/
letter_index[0] = 0;
words = fopen("words", "r");
/*Read the words(forward and reverse) in from stdin into the word_storage*/
while(fgets(buffer, BUFFERLEN, words) != NULL){
buffer = strtok(buffer, "\n");
strlen = length(buffer);
if (num_words < size){
/*Allocate memory for the forward and reverse strings*/
word_storage[num_words].forw = (char *) malloc (sizeof(char) * strlen);
if(!word_storage[num_words].forw){
free(word_storage[num_words].forw);
fprintf(stderr, "word_storage forward string malloc was unsuccessful");
exit(1);
}
word_storage[num_words].back = (char *) malloc (sizeof(char) * strlen);
if(!word_storage[num_words].back){
free(word_storage[num_words].back);
fprintf(stderr, "word_storage forward string malloc was unsuccessful");
exit(1);;
}
/*Store the forward and reverse in the strings*/
strncpy(word_storage[num_words].forw, buffer, strlen);
reverse(word_storage[num_words].forw, word_storage[num_words].back, strlen);
printf("%d: %s %s\n", num_words, word_storage[num_words].forw, word_storage[num_words].back);
/*Increment the letter if it changes*/
if(word_storage[num_words].forw[0] != alpha[letter]){
letter++;
letter_index[letter] = num_words + 1;
}
num_words++;
}
else{
/*Increase the size of word_storage*/
word_storage = (word*) realloc (word_storage, sizeof(word) * size * GROW);
if(!word_storage){
free(word_storage);
fprintf(stderr, "Error in word_storage realloc string mem realloc\n");
exit(1);
}
size = size * GROW;
}
}
return 0;
}
此处发生重新分配错误:
word_storage = (word*) realloc (word_storage, sizeof(word) * size * GROW);
if(!word_storage){
free(word_storage);
fprintf(stderr, "Error in word_storage realloc string mem realloc\n");
exit(1);
}
size = size * GROW;
最佳答案
因此,您最初将 size
设置为 START_SIZE
,即 100,000。然后,当您用完它时,您尝试分配 sizeof(word) * size * GROW
字节。 sizeof(word)
大概是 16 个字节;我们知道 size
是 100000,GROW
是 1000。因此,这足以容纳 100,000,000 个条目,您说您将使用其中的 235,000 个条目。看来分配有点慷慨了。
100,000,000 个条目的总空间为 1,600,000,000 字节。这似乎很多,尽管现在许多台式机都可以处理这个问题。但 realloc
失败似乎并不太令人惊讶。
也许你应该让 GROW
更合理,比如 2。
顺便说一句,一旦确定 word_storage 为 NULL,调用 free(word_storage)
就没有意义了。它没有任何害处,因为 free(NULL)
是一个无操作,但出于同样的原因它也没有任何好处。
关于c - 结构体数组内存 realloc stderr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19206005/
我想阅读 php://stderr。怎么做到的? php://stderr 和 STDERR 是否写入同一个文件?因为在写入 php://stderr 后,我尝试使用 stream_get_conte
我不确定这个问题是 Python 还是 shell 问题。 我有一个 Python 程序,它在命令上使用子进程调用,该命令可以在 stderr 上发出错误消息。我自己的程序也使用 sys.stderr
如何重定向命令的输出,以便 stdout 和 stderr 都记录在文件中,并且我仍然希望 stderr 显示为输出。 我也不想使用 bash 来执行此操作。有这样的办法吗? 最佳答案 这很简单: $
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this qu
我得到了以下批处理命令 echo 1 & echo 2 1>&2 & echo 3 有时这会打印 1 2 3有时 132 我怎样才能控制顺序?我必须得到订单。 是否有启用以下功能的命令? echo 1
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Ruby $stdout vs. STDOUT STDERR 通常比使用 $stderr 更受青睐,还是相反
这是我经常尝试完成的任务。我想将 stderr 和 stdout 都记录到日志文件中。但我只想打印到控制台 stderr。 我尝试过使用 tee,但是一旦我使用“2>&1”合并了 stderr 和 s
我想要做的是将 stderr 重定向到 stdout,而不更改 stderr 的输出。 比如说,命令在stderr中有输出,我想将stderr中的所有内容输出到屏幕,同时还通过grep处理信息并将其保
我正在尝试重定向一些 bash 脚本输出。我想做的是: ./some_script.sh 2> error.log >> all_output.log 2>&1 我想将 stderr 放在一个文件中,
我想将 stdout 和 stderr 的输出重定向到一个公共(public)文件: ./foo.sh >stdout_and_stderr.txt 2>&1 但也只是将 stderr 重定向到一个单
我想运行几个命令,并将所有输出捕获到日志文件中。我还想将任何错误打印到屏幕上(或者可以选择将输出邮寄给某人)。 这是一个例子。以下命令将运行三个命令,并将所有输出(STDOUT 和 STDERR)写入
在其他语言中(如 bash 和 Python),当我们生成一个子进程时,这个新进程将从父进程继承 stdout 和 stderr。这意味着子进程的任何输出都将打印到终端以及父进程的输出。 我们如何在
这个问题在这里已经有了答案: IO Redirection - Swapping stdout and stderr (4 个答案) 关闭 7 年前。 我想将应该转到 stdout 的所有内容重定向
我有一个 shell 脚本,我想将其 stdout 和 stderr 写入日志文件。我知道这可以通过 sh script.sh >> both.log 2>&1 但是,我还想同时将 stderr 写入
git clone 将其输出写入 stderr,如记录 here .我可以使用以下命令重定向它: git clone https://myrepo c:\repo 2>&1 但这会将所有输出(包括错误
以下将 stdout 写入日志文件并打印 stderr: bash script.sh >> out.log 这再次将 stdout 和 stderr 写入日志文件: bash script.sh >
我正在调试一个在 PHP 5.4 上使用 Slim 和 NotORM 的项目。将 NotORM 设置为 Debug模式时,NotORM 跟踪语句: fwrite(STDERR, "$backtrace
到目前为止我所做的是: #!/bin/bash exec 2> >(sed 's/^/ERROR= /') var=$( sleep 1 ; hostname ;
我在远程机器上通过 SSH 执行一系列操作,我正在传输它的标准输出和标准错误,然后由写入器使用它,写入本地标准输出和标准错误,以及字节缓冲区。 就在编写器使用它之前,我想对其执行一系列字符串操作,然后
现在我有一些使用 Popen.communicate() 的代码从子进程(设置 stdin=PIPE 和 stderr=PIPE)运行命令并捕获 stderr 和 stdout。 问题在于 commu
我是一名优秀的程序员,十分优秀!