- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我最近一直在努力学习 C,以便更好地理解(相对)低级编程,因为我通常使用 Python、PHP、Swift、Java、Javascript 等来完成我的大部分工作。我想我明白了指针的概念以及如何分配/释放内存,但在一个特定的实例中我很困惑。似乎有时(可能 < 10% 的时间)我必须测试的一些代码 strlcpy
会产生意想不到的结果。其他 90% 以上的时间,它按预期工作。以下是我运行的代码,其中包含描述我的意图的注释:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void populate(int length, size_t size, char **strings) {
char test[5] = "test"; //the string we use to populate our incoming array
for (int i = 0; i < length; ++i) //iterate through the length of the string array
{
strings[i] = malloc(sizeof(*strings) * size); //allocate memory for this string to the size of {size}
strlcpy(*(strings + i), test, size); //copy our test string to the string at index {i} of our array
printf("%p - ", (void *) strings[i]); //print the address as a dummy check
printf("String %d: %s\n", i, strings[i]); //confirm our string is being copied as expected
}
}
int main() {
int length = 3; //the length of our array of strings
size_t size = 5; //the length of each string
char **strings = malloc(sizeof(**strings) * length); //a pointer to a {length}-element array of char pointers
printf("Start iteration 1\n");
populate(length, size, strings); //a function which fills **strings with {length} strings of size {size}
printf("Start iteration 2\n");
for (int i = 0; i < length; ++i) //iterate through each string to validate the result
{
printf("%p - ", (void *) strings[i]); //print the address as a dummy check
printf("String %d: %s\n", i, strings[i]); //display our string at index {i}
free(strings[i]); //free the memory of this string
}
free(strings); //free the memory of our string array
return 0;
}
我构建它是为了更好地理解 **
指针到指针的语法和逻辑。在大多数情况下,一切似乎都运行良好,我将在我的控制台中获得以下打印输出:
Start iteration 1
0x7ffd70c027a0 - String 0: test
0x7ffd70c027b0 - String 1: test
0x7ffd70c027c0 - String 2: test
Start iteration 2
0x7ffd70c027a0 - String 0: test
0x7ffd70c027b0 - String 1: test
0x7ffd70c027c0 - String 2: test
Process finished with exit code 0
然而,有时,仅在第二次迭代中显示的一些值不正确或导致我认为是段错误:
Start iteration 1
0x7f9f78f00840 - String 0: test
0x7f9f78f00850 - String 1: test
0x7f9f78f00860 - String 2: test
Start iteration 2
0x7f9f78f00840 - String 0: �x�
0x7f9f78f00850 - String 1: test
Process finished with exit code 11
我不太明白为什么会这样,尽管我认为根据我的结果我可以安全地假设它超出了 populate
函数的范围。我的指针算法有问题吗?在某些情况下我没有分配足够的内存吗?
最初,我认为这可能与我在 main
函数末尾释放内存的方式有关,但我在 SO 中查找了几个问题,发现它似乎我通过在释放外部数组 (strings
) 之前先释放内部数组 (*strings
) 来正确地做到这一点。但是,根据错误的位置,我仍然有理由相信问题可能出在我释放内存的方式上。我只是不知道我做错了什么。
如有任何建议,我们将不胜感激。谢谢!
最佳答案
问题是这样的:char **strings = malloc(sizeof(**strings) * length);
请注意,您正在分配 sizeof(**strings)
。 char **
两次解引用只是一个 char
,所以 sizeof(**strings)
是 1。所以你只分配了 3 个字节。这不是你想要的。
相反,您需要 sizeof(*strings)
,它存储在 strings
中。这将是一个 char *
并且将是 4 或 8,具体取决于您是 32 位还是 64 位。
关于c - 在将 strlcpy 与 char ** 类型一起使用后,值有时会被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42634203/
我正在尝试编写一个相当多态的库。我遇到了一种更容易表现出来却很难说出来的情况。它看起来有点像这样: {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE
谁能解释一下这个表达式是如何工作的? type = type || 'any'; 这是否意味着如果类型未定义则使用“任意”? 最佳答案 如果 type 为“falsy”(即 false,或 undef
我有一个界面,在IAnimal.fs中, namespace Kingdom type IAnimal = abstract member Eat : Food -> unit 以及另一个成功
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the difference between (type)value and type(va
在 C# 中,default(Nullable) 之间有区别吗? (或 default(long?) )和 default(long) ? Long只是一个例子,它可以是任何其他struct类型。 最
假设我有一个案例类: case class Foo(num: Int, str: String, bool: Boolean) 现在我还有一个简单的包装器: sealed trait Wrapper[
这个问题在这里已经有了答案: Create C# delegate type with ref parameter at runtime (1 个回答) 关闭 2 年前。 为了即时创建委托(dele
我正在尝试获取图像的 dct。一开始我遇到了错误 The function/feature is not implemented (Odd-size DCT's are not implemented
我正在尝试使用 AFNetworking 的 AFPropertyListRequestOperation,但是当我尝试下载它时,出现错误 预期的内容类型{( “应用程序/x-plist” )}, 得
我在下面收到错误。我知道这段代码的意思,但我不知道界面应该是什么样子: Element implicitly has an 'any' type because index expression is
我尝试将 SignalType 从 ReactiveCocoa 扩展为自定义 ErrorType,代码如下所示 enum MyError: ErrorType { // .. cases }
我无法在任何其他问题中找到答案。假设我有一个抽象父类(super class) Abstract0,它有两个子类 Concrete1 和 Concrete1。我希望能够在 Abstract0 中定义类
我想知道为什么这个索引没有用在 RANGE 类型中,而是用在 INDEX 中: 索引: CREATE INDEX myindex ON orders(order_date); 查询: EXPLAIN
我正在使用 RxJava,现在我尝试通过提供 lambda 来订阅可观察对象: observableProvider.stringForKey(CURRENT_DELETED_ID) .sub
我已经尝试了几乎所有解决问题的方法,其中包括。为 提供类型使用app.use(express.static('public'))还有更多,但我似乎无法为此找到解决方案。 index.js : imp
以下哪个 CSS 选择器更快? input[type="submit"] { /* styles */ } 或 [type="submit"] { /* styles */ } 只是好
我不知道这个设置有什么问题,我在 IDEA 中获得了所有注释(@Controller、@Repository、@Service),它在行号左侧显示 bean,然后转到该 bean。 这是错误: 14-
我听从了建议 registering java function as a callback in C function并且可以使用“简单”类型(例如整数和字符串)进行回调,例如: jstring j
有一些 java 类,加载到 Oracle 数据库(版本 11g)和 pl/sql 函数包装器: create or replace function getDataFromJava( in_uLis
我已经从 David Walsh 的 css 动画回调中获取代码并将其修改为 TypeScript。但是,我收到一个错误,我不知道为什么: interface IBrowserPrefix { [
我是一名优秀的程序员,十分优秀!