- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我从这段代码中得到了一些内存泄漏,而且我在 c 方面相当缺乏经验。下面是泄漏内存的代码,下面是 valgrind 输出。我似乎无法缩小未释放的确切内存块的范围,请帮忙!抱歉代码过载,我是新手。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "students.h"
void init_student(struct student* student, char* name, int id, float gpa) {
int name_size = strlen(name);
student->name = malloc(sizeof(char) * (name_size + 1));
strcpy(student->name, name);
student->id = id;
student->gpa = gpa;
}
void free_student(struct student* student) {
free(student->name);
}
struct student* deep_copy_student(struct student* student) {
struct student *copy;
copy = (struct student*) malloc(sizeof(struct student));
init_student(copy, student->name, student->id, student->gpa);
return copy;
}
struct student* create_student_array(int num_students, char** names, int* ids,
float* gpas) {
struct student* students;
students = malloc(sizeof(struct student) * num_students);
for (int i = 0; i < num_students; ++i) {
init_student(&students[i], names[i], ids[i], gpas[i]);
}
return students;
}
void destroy_student_array(struct student* students, int num_students) {
for (int i = 0; i > num_students; i++) {
free_student(&students[i]);
}
free(students);
}
void print_students(struct student* students, int num_students) {
for (int i=0; i < num_students; ++i) {
printf("Name: %s, ID: %d, GPA: %f\n" , students[i].name, students[i].id, students[i].gpa);
}
}
struct student* find_max_gpa(struct student* students, int num_students) {
struct student* best_in_school;
best_in_school = &students[0];
for (int i = 1; i < num_students; ++i) {
if(students[i].gpa > best_in_school->gpa){
best_in_school = &students[i];
}
}
return best_in_school;
}
struct student* find_min_gpa(struct student* students, int num_students) {
struct student* worst_in_school;
worst_in_school = &students[0];
for (int i = 1; i < num_students; ++i) {
if(students[i].gpa < worst_in_school->gpa){
worst_in_school = &students[i];
}
}
return worst_in_school;
}
void sort_by_gpa(struct student* students, int num_students) {
struct student temp;
for (int i = 0; i < (num_students - 1); ++i) {
for (int j = 0; j < (num_students - i); ++j) {
if(students[j].gpa < students[j+1].gpa) {
temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
}
test.c只是一个测试所有功能的框架,是预制的,所以不是问题所在。它似乎源于在前面的代码中制作的 8 个学生的数组。
==31704== HEAP SUMMARY:
==31704== in use at exit: 88 bytes in 8 blocks
==31704== total heap usage: 12 allocs, 4 frees, 262 bytes allocated
==31704==
==31704== 88 bytes in 8 blocks are definitely lost in loss record 1 of 1
==31704== at 0x4C29BE3: malloc (vg_replace_malloc.c:299)
==31704== by 0x40092C: init_student (students.c:50)
==31704== by 0x400A66: create_student_array (students.c:133)
==31704== by 0x4007B5: main (test.c:92)
==31704==
==31704== LEAK SUMMARY:
==31704== definitely lost: 88 bytes in 8 blocks
==31704== indirectly lost: 0 bytes in 0 blocks
==31704== possibly lost: 0 bytes in 0 blocks
==31704== still reachable: 0 bytes in 0 blocks
==31704== suppressed: 0 bytes in 0 blocks
==31704==
==31704== For counts of detected and suppressed errors, rerun with: -v
==31704== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
这是主文件。就像我之前说的,它只是为我提供的测试台。希望对您有所帮助。
#include <stdio.h>
#include <stdlib.h>
#include "students.h"
/*
* This is the total number of students in the testing data set.
*/
#define NUM_TESTING_STUDENTS 8
/*
* These are the names of the students that'll be used for testing.
*/
char* TESTING_NAMES[] = {
"Luke Skywalker",
"Princes Leia",
"Chewbacca",
"Han Solo",
"Lando Calrissian",
"Darth Vader",
"C-3PO",
"R2-D2"
};
/*
* These are the student IDs for the students in the array above that will be
* used for testing.
*/
int TESTING_IDS[] = {
933111111,
933222222,
933333333,
933444444,
933555555,
933666666,
933777777,
933888888
};
/*
* These are the GPAs of the students above that will be used for testing.
*/
float TESTING_GPAS[] = {
3.75,
4.0,
3.0,
2.5,
3.67,
1.33,
3.25,
3.9
};
int main(int argc, char** argv) {
struct student student;
struct student* copy = NULL, * max_gpa, * min_gpa, * students = NULL;
int i;
/*
* Initialize a student using init_student() and print the results. The
* power of pointers lets us use print_students() this way!
*/
init_student(&student, TESTING_NAMES[0], TESTING_IDS[0], TESTING_GPAS[0]);
printf("\n== Here are the results of init_student():\n");
print_students(&student, 1);
/*
* Make a copy of student using deep_copy_student() and compare the results.
*/
copy = deep_copy_student(&student);
printf("\n== Here's that student (left) and its deep copy (right):\n");
if (copy) {
printf("name (value): %s\t%s\n", student.name, copy->name);
printf("name (pointer): %p\t%p\n", student.name, copy->name);
printf("id: %d\t%d\n", student.id, copy->id);
printf("gpa: %f\t%f\n", student.gpa, copy->gpa);
}
/*
* Create an array of students using create_student_array() and print the
* results.
*/
students = create_student_array(NUM_TESTING_STUDENTS, TESTING_NAMES,
TESTING_IDS, TESTING_GPAS);
printf("\n== Here are the results of create_student_array():\n");
print_students(students, NUM_TESTING_STUDENTS);
/*
* Use find_max_gpa() to find the student with the highest GPA and print
* the result.
*/
max_gpa = find_max_gpa(students, NUM_TESTING_STUDENTS);
printf("\n== Here's the student with the highest GPA:\n");
print_students(max_gpa, 1);
/*
* Use find_min_gpa() to find the student with the lowest GPA and print
* the result.
*/
min_gpa = find_min_gpa(students, NUM_TESTING_STUDENTS);
printf("\n== Here's the student with the lowest GPA:\n");
print_students(min_gpa, 1);
/*
* Use sort_by_gpa() to order the students by decreasing GPA and print the
* results.
*/
sort_by_gpa(students, NUM_TESTING_STUDENTS);
printf("\n== Here are the students ordered by decreasing GPA:\n");
print_students(students, NUM_TESTING_STUDENTS);
/*
* Free all of the memory we allocated here. You should use valgrind to
* verify that you don't have memory leaks.
*/
free_student(&student);
free_student(copy);
free(copy);
destroy_student_array(students, NUM_TESTING_STUDENTS);
return 0;
}
最佳答案
更改 for
来自 i > num_students
的结束表达式至 i < num_students
在destroy_student_array
功能。对于正数的学生,循环永远不会进入 - 它可能是泄漏的来源。
关于找不到内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48481482/
IntentReceiver 正在泄漏 由于 onDetachedFromWindow 在某些情况下未被调用。 @Override protected void onDetachedFromWind
好吧,我很难追踪这个内存泄漏。运行此脚本时,我没有看到任何内存泄漏,但我的 objectalloc 正在攀升。 Instruments 指向 CGBitmapContextCreateImage >
我编写了一个测试代码来检查如何使用 Instrument(Leaks)。我创建了一个单一 View 应用程序,单击按钮后我加载了一个像这样的新 View ... - (IBAction)btn_clk
我正在使用这个简单的代码并观察单调增加的内存使用量。我正在使用这个小模块将内容转储到磁盘。我观察到它发生在 unicode 字符串上而不是整数上,我做错了什么吗? 当我这样做时: >>> from u
我有以下泄漏的代码。 Instruments 表示,泄漏的是 rssParser 对象。我“刷新”了 XML 提要,它运行了该 block 并且发生了泄漏...... 文件.h @interface
我在我编写的以下代码片段中发现了内存泄漏 NSFileManager *fileManager=[[NSFileManager alloc] init]; fileList=[[fileManager
因此,我正在开发HTML5 / javascript rts游戏。观察一直有几种声音在播放。因此,对我来说,是一段时间后声音听起来像是“崩溃”,并且此浏览器选项卡上的所有声音都停止了工作。我只能通过重
下面是我正在使用的一段代码及其输出。 my $handle; my $enterCount = Devel::Leak::NoteSV($handle); print "$date entry $en
在这篇关于 go-routines 泄漏的帖子之后,https://www.ardanlabs.com/blog/2018/11/goroutine-leaks-the-forgotten-sende
我想知道为什么在执行 ./a.out 后随机得到以下结果。有什么想法我做错了吗?谢谢 http://img710.imageshack.us/img710/8708/trasht.png 最佳答案 正
我正在 Swift 中开发一个应用程序,在呈现捕获我放在一起的二维码的自定义 ViewController 后,我注意到出现了巨大的内存跳跃。 该代码本质上基于以下示例:http://www.appc
下面是我的 javascript 代码片段。它没有按预期运行,请帮我解决这个问题。 function getCurrentLocation() { console.log("insi
我们在生产环境中部署了 3 个代理 Kafka 0.10.1.0。有些应用程序嵌入了 Kafka Producer,它们将应用程序日志发送到某个主题。该主题有 10 个分区,复制因子为 3。 我们观察
我正在使用仪器来检测一些泄漏,但有一些泄漏我无法解决; NSMutableString *textedetails = [[NSMutableString alloc] init];
如果我使用性能工具测试我的代码 - 泄漏,它没有检测到任何泄漏。这是否意味着代码没有泄漏任何内存? 我有一个越狱的 iPhone,我可以监控可用内存。如果有人知道,那就是 SBSettings。我测试
我在从 AddressBook 中获取图像时遇到了很大的问题,下面我粘贴了我的代码。此 imageData 从未被释放,在我的 Allocations Instruments 上它看起来总是在内存中它
- (NSMutableArray *)getArrayValue:(NSArray *)array{ NSMutableArray *valueArray = [NSMutableArra
Instruments 工具说这是一个泄漏,有什么想法吗? 我在 for 循环结束时释放变量对象 在上述方法的开头,这就是我设置变量对象的方式,即自动释放; NSMutableArray *varia
我正在跟踪我的 iOS 应用程序的内存泄漏,我有一个奇怪的泄漏导致我的应用程序崩溃......负责的框架是:CGImageMergeXMPPropsWhithLegacyProps。在某些时候,我的应
我正在尝试使用 NSOperationQueue 在后台线程中执行一个方法,如下所示: NSOperationQueue *queue = [NSOperationQueue new]; NS
我是一名优秀的程序员,十分优秀!