- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正在学习编程,并且是第一年。我们目前正在学习 C,我们的任务是创建一个有点像 getline() 的函数,但不同之处在于该函数仅将文件描述符作为参数并返回每个调用一个 malloc'd 字符串,代表文件中对应于文件描述符的下一行。
函数返回的行不能包含任何'\n'字符,当没有任何内容可读或函数遇到错误时必须返回NULL。我们还限于以下功能:read、malloc 和 free。
我写了下面的代码(抱歉,如果它难以阅读我还在学习,我们在编写代码时有一个“编码风格”可以遵循):
#include "get_next_line.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int find_n(char *str)
{
int i = 0;
while (str[i] != '\0') {
if (str[i] == '\n')
return (i);
i++;
}
return (0);
}
static char *my_realloc(char *str, unsigned int size, unsigned int start_at)
{
int i = 0;
char *new_str;
new_str = malloc(size);
while (str[i + start_at] != '\0') {
new_str[i] = str[i + start_at];
i++;
}
if (str[i + start_at] == '\0')
new_str[i] == '\0';
free(str);
return (new_str);
}
static char *get_line(char *str, unsigned int *total_read)
{
int i = find_n(str);
int j = 0;
int len = 0;
char *line = malloc(i + 1);
while (j < i) {
line[j] = str[j];
j++;
}
line[j] = '\0';
while (str[i + 1 + len] != '\0') {
len++;
}
*total_read = len;
str = my_realloc(str, len + 1, i + 1);
return (line);
}
static void my_strcat(char *dest, char const *src)
{
int dest_len = 0;
int i = 0;
while (dest[i] != '\0') {
i++;
}
dest_len = i;
i = 0;
while (src[i] != '\0') {
dest[dest_len + i] = src[i];
i++;
}
dest[dest_len + i] = '\0';
}
static char *get_text(int fd, char *str)
{
char *buf;
int n_read = READ_SIZE;
static unsigned int total_read = 0;
int loop_started = 0;
buf = malloc(READ_SIZE + 1);
buf[0] = '\0';
while (n_read == READ_SIZE) {
n_read = read(fd, buf, READ_SIZE);
if (n_read == -1 || (n_read == 0 && !loop_started)) {
free(str);
free(buf);
return (NULL);
}
buf[n_read] = '\0';
total_read += n_read;
str = my_realloc(str, total_read + 1, 0);
my_strcat(str, buf);
loop_started = 1;
if (find_n(buf)) {
break;
}
}
free(buf);
return (get_line(str, &total_read));
}
char *get_next_line(int fd)
{
static char *str;
int is_str_malloced = 0;
if (!is_str_malloced) {
str = malloc(1);
str[0] = '\0';
is_str_malloced = 1;
}
return(get_text(fd, str));
}
int main(void)
{
char *str;
int fd;
fd = open("script", O_RDONLY);
while (1) {
str = get_next_line(fd);
if (str == NULL) {
close(fd);
return (0);
}
printf("%s\n", str);
}
close(fd);
return (0);
}
主要功能几乎仅用于测试目的。当我编译和测试它时,我得到:
*** Error in `./a.out': free(): invalid next size (normal): 0x0000000000ed71b0 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x7cbac)[0x7f67d51a5bac]
/lib64/libc.so.6(+0x87a59)[0x7f67d51b0a59]
/lib64/libc.so.6(cfree+0x16e)[0x7f67d51b63be]
./a.out[0x40071b]
./a.out[0x40095f]
./a.out[0x400a07]
./a.out[0x400a32]
/lib64/libc.so.6(__libc_start_main+0xea)[0x7f67d514988a]
./a.out[0x40059a]
======= Memory map: ========
00400000-00401000 r-xp 00000000 103:05 22939320 /home/ldidierjean/delivery/CPE_getnextline_2017/a.out
00600000-00601000 r--p 00000000 103:05 22939320 /home/ldidierjean/delivery/CPE_getnextline_2017/a.out
00601000-00602000 rw-p 00001000 103:05 22939320 /home/ldidierjean/delivery/CPE_getnextline_2017/a.out
00ed7000-00ef8000 rw-p 00000000 00:00 0 [heap]
7f67d0000000-7f67d0021000 rw-p 00000000 00:00 0
7f67d0021000-7f67d4000000 ---p 00000000 00:00 0
7f67d4f12000-7f67d4f28000 r-xp 00000000 103:05 7210485 /usr/lib64/libgcc_s-7-20170915.so.1
7f67d4f28000-7f67d5127000 ---p 00016000 103:05 7210485 /usr/lib64/libgcc_s-7-20170915.so.1
7f67d5127000-7f67d5128000 r--p 00015000 103:05 7210485 /usr/lib64/libgcc_s-7-20170915.so.1
7f67d5128000-7f67d5129000 rw-p 00016000 103:05 7210485 /usr/lib64/libgcc_s-7-20170915.so.1
7f67d5129000-7f67d52f4000 r-xp 00000000 103:05 7217643 /usr/lib64/libc-2.25.so
7f67d52f4000-7f67d54f4000 ---p 001cb000 103:05 7217643 /usr/lib64/libc-2.25.so
7f67d54f4000-7f67d54f8000 r--p 001cb000 103:05 7217643 /usr/lib64/libc-2.25.so
7f67d54f8000-7f67d54fa000 rw-p 001cf000 103:05 7217643 /usr/lib64/libc-2.25.so
7f67d54fa000-7f67d54fe000 rw-p 00000000 00:00 0
7f67d54fe000-7f67d5525000 r-xp 00000000 103:05 7219338 /usr/lib64/ld-2.25.so
7f67d56f6000-7f67d56f9000 rw-p 00000000 00:00 0
7f67d5721000-7f67d5724000 rw-p 00000000 00:00 0
7f67d5724000-7f67d5725000 r--p 00026000 103:05 7219338 /usr/lib64/ld-2.25.so
7f67d5725000-7f67d5727000 rw-p 00027000 103:05 7219338 /usr/lib64/ld-2.25.so
7fff51a9b000-7fff51abd000 rw-p 00000000 00:00 0 [stack]
7fff51ac2000-7fff51ac5000 r--p 00000000 00:00 0 [vvar]
7fff51ac5000-7fff51ac7000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Abandon (core dumped)
Valgrind 给我:
==6142== Memcheck, a memory error detector
==6142== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==6142== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==6142== Command: ./a.out --track-origins=yes
==6142==
==6142== Conditional jump or move depends on uninitialised value(s)
==6142== at 0x400824: my_strcat (get_next_line.c:67)
==6142== by 0x400960: get_text (get_next_line.c:98)
==6142== by 0x4009F1: get_next_line (get_next_line.c:118)
==6142== by 0x400A1C: main (get_next_line.c:128)
==6142==
==6142== Invalid read of size 1
==6142== at 0x40081F: my_strcat (get_next_line.c:67)
==6142== by 0x400960: get_text (get_next_line.c:98)
==6142== by 0x4009F1: get_next_line (get_next_line.c:118)
==6142== by 0x400A1C: main (get_next_line.c:128)
==6142== Address 0x5214257 is 0 bytes after a block of size 16,759 alloc'd
==6142== at 0x4C2EB6B: malloc (vg_replace_malloc.c:299)
==6142== by 0x4006B3: my_realloc (get_next_line.c:31)
==6142== by 0x400949: get_text (get_next_line.c:97)
==6142== by 0x4009F1: get_next_line (get_next_line.c:118)
==6142== by 0x400A1C: main (get_next_line.c:128)
==6142==
==6142== Invalid write of size 1
==6142== at 0x400857: my_strcat (get_next_line.c:73)
==6142== by 0x400960: get_text (get_next_line.c:98)
==6142== by 0x4009F1: get_next_line (get_next_line.c:118)
==6142== by 0x400A1C: main (get_next_line.c:128)
==6142== Address 0x5214270 is 16 bytes after a block of size 16,768 in arena "client"
==6142==
==6142== Invalid write of size 1
==6142== at 0x400883: my_strcat (get_next_line.c:76)
==6142== by 0x400960: get_text (get_next_line.c:98)
==6142== by 0x4009F1: get_next_line (get_next_line.c:118)
==6142== by 0x400A1C: main (get_next_line.c:128)
==6142== Address 0x5214277 is 23 bytes after a block of size 16,768 in arena "client"
==6142==
==6142== Conditional jump or move depends on uninitialised value(s)
==6142== at 0x4006F7: my_realloc (get_next_line.c:32)
==6142== by 0x400949: get_text (get_next_line.c:97)
==6142== by 0x4009F1: get_next_line (get_next_line.c:118)
==6142== by 0x400A1C: main (get_next_line.c:128)
==6142==
==6142== Invalid read of size 1
==6142== at 0x4006F2: my_realloc (get_next_line.c:32)
==6142== by 0x400949: get_text (get_next_line.c:97)
==6142== by 0x4009F1: get_next_line (get_next_line.c:118)
==6142== by 0x400A1C: main (get_next_line.c:128)
==6142== Address 0x5214257 is 0 bytes after a block of size 16,759 alloc'd
==6142== at 0x4C2EB6B: malloc (vg_replace_malloc.c:299)
==6142== by 0x4006B3: my_realloc (get_next_line.c:31)
==6142== by 0x400949: get_text (get_next_line.c:97)
==6142== by 0x4009F1: get_next_line (get_next_line.c:118)
==6142== by 0x400A1C: main (get_next_line.c:128)
==6142==
==6142== Invalid read of size 1
==6142== at 0x4006D8: my_realloc (get_next_line.c:33)
==6142== by 0x400949: get_text (get_next_line.c:97)
==6142== by 0x4009F1: get_next_line (get_next_line.c:118)
==6142== by 0x400A1C: main (get_next_line.c:128)
==6142== Address 0x5214257 is 0 bytes after a block of size 16,759 alloc'd
==6142== at 0x4C2EB6B: malloc (vg_replace_malloc.c:299)
==6142== by 0x4006B3: my_realloc (get_next_line.c:31)
==6142== by 0x400949: get_text (get_next_line.c:97)
==6142== by 0x4009F1: get_next_line (get_next_line.c:118)
==6142== by 0x400A1C: main (get_next_line.c:128)
==6142==
==6142== Invalid write of size 1
==6142== at 0x4006DB: my_realloc (get_next_line.c:33)
==6142== by 0x400949: get_text (get_next_line.c:97)
==6142== by 0x4009F1: get_next_line (get_next_line.c:118)
==6142== by 0x400A1C: main (get_next_line.c:128)
==6142== Address 0x521841e is 0 bytes after a block of size 16,766 alloc'd
==6142== at 0x4C2EB6B: malloc (vg_replace_malloc.c:299)
==6142== by 0x4006B3: my_realloc (get_next_line.c:31)
==6142== by 0x400949: get_text (get_next_line.c:97)
==6142== by 0x4009F1: get_next_line (get_next_line.c:118)
==6142== by 0x400A1C: main (get_next_line.c:128)
==6142==
==6142== Invalid read of size 1
==6142== at 0x40070A: my_realloc (get_next_line.c:36)
==6142== by 0x400949: get_text (get_next_line.c:97)
==6142== by 0x4009F1: get_next_line (get_next_line.c:118)
==6142== by 0x400A1C: main (get_next_line.c:128)
==6142== Address 0x5214277 is 23 bytes after a block of size 16,768 in arena "client"
==6142==
--6142-- VALGRIND INTERNAL ERROR: Valgrind received a signal 11 (SIGSEGV) - exiting
--6142-- si_code=1; Faulting address: 0x206991B288; sp: 0x1002ba9e30
valgrind: the 'impossible' happened:
Killed by fatal signal
host stacktrace:
==6142== at 0x5804F2DC: ??? (in /usr/lib64/valgrind/memcheck-amd64-linux)
==6142== by 0x5800B304: ??? (in /usr/lib64/valgrind/memcheck-amd64-linux)
==6142== by 0x5800B4D2: ??? (in /usr/lib64/valgrind/memcheck-amd64-linux)
==6142== by 0x58098653: ??? (in /usr/lib64/valgrind/memcheck-amd64-linux)
==6142== by 0x580A7256: ??? (in /usr/lib64/valgrind/memcheck-amd64-linux)
sched status:
running_tid=1
Thread 1: status = VgTs_Runnable (lwpid 6142)
==6142== at 0x4C2EB6B: malloc (vg_replace_malloc.c:299)
==6142== by 0x4006B3: my_realloc (get_next_line.c:31)
==6142== by 0x400949: get_text (get_next_line.c:97)
==6142== by 0x4009F1: get_next_line (get_next_line.c:118)
==6142== by 0x400A1C: main (get_next_line.c:128)
Note: see also the FAQ in the source distribution.
It contains workarounds to several common problems.
In particular, if Valgrind aborted or crashed after
identifying problems in your program, there's a good chance
that fixing those problems will prevent Valgrind aborting or
crashing, especially if it happened in m_mallocfree.c.
If that doesn't help, please report this bug to: www.valgrind.org
In the bug report, send all the above text, the valgrind
version, and what OS and version you are using. Thanks.
看起来 my_realloc 函数中的 free() 导致程序崩溃,但我根本找不到原因,有人可以帮助我吗?
最佳答案
重建您的 realloc
可能是个好主意!
考虑一下您为 realloc
赋予的 start_at
值。
更简单的重新分配方法:
char *my_realloc(char *old, int size)
{
int i = 0;
char *new;
new = malloc(sizeof(char) * (my_strlen(old) + size));
while (old[i] != '\0')
{
new[i] = old[i];
i++;
}
free(old);
return (new);
}
此外,我更喜欢像这样malloc
:
malloc(sizeof(char) * len);
这个项目最好有一个get_char
:
char get_char(const int fd)
{
static char buff[READ_MAX];
static char* pointer;
static int len = 0;
char caracter;
if (len == 0)
{
len = read(fd, buff, READ_MAX);
pointer = (char*)&buff;
if (len == 0)
return (0);
}
caracter = *pointer;
pointer++;
len--;
return caracter;
}
祝你好运!
关于c - 类似 Getline 的函数崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48306394/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!