- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
正如标题所说,每当我尝试复制字符串(使用我自己的字符串类型和库)时,我都会遇到段错误。根据调试器的说法,它发生在源代码的第 29 行(调用 s_strcpy4() 的地方),该行转到 sstring.h 中带有“_dest[i] = _sour[i];”的行。 i 在崩溃时似乎有时会有一些不同的值,但有时会坚持一个值(还不确定为什么)。截至撰写本文时,它卡在 16896。
有人知道我在哪里可能导致代码中的段错误吗?
源文件.c
#include "sstring.h"
#include <string.h>
#include <stdio.h>
#include <time.h>
int main(void) {
s_string ss1,
ss2;
long long int start, end;
struct timespec time;
FILE *LoremIpsum;
s_init(&ss1, NULL, 65536);
s_init(&ss2, NULL, 65536);
LoremIpsum = fopen("Lorem ipsum", "r");
if(LoremIpsum == NULL) {
perror("Error opening file ");
return 1;
}
fgets(ss2.string, 65536, LoremIpsum);
if(fclose(LoremIpsum) == EOF) {
perror("Error closing file ");
return 2;
}
s_strcpy4(&ss1, &ss2);
return 0;
}
字符串.h
#include <stdlib.h>
#include <stdint.h>
enter code here
typedef struct {
int length;
char *string;
} s_string;
s_string *s_init(s_string *str, char *array, size_t num) {
int i;
if(str == NULL)
str = malloc(sizeof(s_string));
if(array == NULL) {
(*str).length = num;
if(num != 0)
(*str).string = malloc(num);
} else {
if(num == 0) {
(*str).string = NULL;
for(i = 0; array[i] != '\0'; i++) {
(*str).string = realloc((void *)(*str).string, i + 1);
(*str).string[i] = array[i];
}
(*str).length = i;
} else {
(*str).string = (char *)malloc(num);
(*str).length = num;
for(i = 0; i < num; i++)
(*str).string[i] = array[i];
}
}
return str;
}
s_string *s_strcpy4(s_string *__restrict__ destination, const s_string *__restrict__ source) {
int i;
long long *_dest = (long long *)(destination->string),
*_sour = (long long *)(source->string);
for(i = 0; i < source->length - 8; i+=8)
_dest[i] = _sour[i];
for( ; i < source->length; i++)
destination->string[i] = source->string[i];
destination->length = i;
return destination;
}
最佳答案
_dest[i] = _sour[i]; 应该 _dest[i/8] = _sour[i/8];
或类似的东西。
代码还有很多其他问题。例如,在需要正确对齐指针的体系结构上,尝试访问不以八的倍数地址开头的一组八个字符将导致总线错误。
我假设您这样做是为了练习,而不是在生产中使用。只需使用 memcpy()
关于使用指向 uint64_t 的指针复制 C 中的自定义字符串会导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17914976/
我有以下代码 unsigned int headerbytes = 0U; headerbytes = (unsigned int*)strtoull(packet_space->header
我有这段无法编译的代码: public struct MyStruct { private fixed uint myUints[32]; public uint[] MyUints
在 Go 中,从函数返回哪个更有效:返回 uint 还是返回 *uint? 该函数在 cpu 密集型库的内部 for 循环中调用。 最佳答案 一般来说,只要效率是个问题,您就应该运行基准测试。 让我们
int 加上 unsigned int 返回一个 unsigned int。应该这样吗? 考虑这段代码: #include #include #include class test {
我正在尝试从可通过 URL 访问的内容中初始化一个字符串: actualresponse.response = String(contentsOfURL: url, usedEncoding: NSU
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 提供事实和引用来回答它. 1年前关闭。 Improve this
我从函数 Swift 得到类型为 UnsafeMutablePointer 的结果 我可以把它转换到UInt吗? ? 最佳答案 只需使用memory 属性来访问底层数据。 let ptr: Unsaf
我深入了解了 List并发现了以下代码: public T this[int index] { get { // Following trick can red
我在 this page on bit twiddling 的帮助下编写了这个函数: uint16_t *decode(uint64_t instr) { // decode instr (thi
我正在从微 Controller 读取两个寄存器。一个具有 4 位 MSB(前 4 位有一些其他内容),另一个具有 8 位 LSB。我想将其转换为一个 12 位 uint(准确地说是 16 位)。到目
要演示的示例代码: public int FindComplement(int num) { //uint mask = ~0; //<-- error CS0031 //
$ rustc --test mapAsMapKey.rs mapAsMapKey.rs:18:43: 18:52 error: mismatched types: expected `fn@(&&@
一般问题:我有一个很大的二维点空间,里面稀疏地分布着点。把它想象成一 block 撒满黑点的白色大 Canvas 。我必须多次迭代和搜索这些点。 Canvas (点空间)可能很大,接近极限int 的值
假设我们只是调用一个普通数字,数字会启动什么。 uint256 plainNumber 我明白它是零。但是我要问的是,有没有办法检测该数字是由编译器还是用户变量设置的。例如... uint256 pl
我试图在 leetcode.com ( https://leetcode.com/problems/number-of-1-bits/ ) 上解决一个简单的问题,我遇到了一个奇怪的行为,这可能是我缺乏
uint number = 0x418 in bits : 0000010000011000 uint number1 = 0x8041 in bits: 1000000001000001 uint
我如何在 C# 中生成具有某个最大值的伪随机 uint? (不需要最低限度。)似乎有很多问题要求完全随机,但没有上限。 澄清:此上限可能大于 int.MaxValue,因此仅强制转换 Random.N
我已经用私有(private)数据成员围绕 ulong 编写了一个简单的包装器。我希望能够将包装器转换为 ulong 以检索数据。我希望强制转换为 uint 并丢失数据是非法的,因此我没有编写对 ui
哪些是“Uint”变量?就是有“Uint8”、“Uint16”等…… 但是它们是什么? 现在我有一些时间使用 C++,但我从来不需要使用这些变量并引起我的好奇。 提前致谢。 最佳答案 uint 不是标
我有一个 native 方法,它需要一个指针来写出一个双字(uint)。 现在我需要从 (Int) 指针中获取实际的 uint 值,但是 Marshal 类只有方便的方法来读取(有符号)整数。 如何从
我是一名优秀的程序员,十分优秀!