- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有和这个几乎相同的问题: Getting data from pointer in struct "Invalid read/write"
但是当我尝试遵循这些建议时,我仍然遇到相同的无效读取大小。
我的结构如下:
typedef struct{
int lenght;
int max_lenght;
int extract;
int inserting;
void** structure;
} queue_t;
我的循环缓冲区代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "queue.h"
/* creates a new queue with a given size */
queue_t* create_queue(int capacity){
queue_t* queue = malloc (sizeof(queue_t));
queue->lenght = 0;
queue -> max_lenght = capacity;
queue -> extract = 0;
queue -> inserting = 0;
queue -> structure = malloc(sizeof(void*) * capacity);
return queue;
}
/* deletes the queue and all allocated memory */
void delete_queue(queue_t *queue){
free(queue->structure);
free(queue);
}
/*
* inserts a reference to the element into the queue
* returns: true on success; false otherwise
*/
bool push_to_queue(queue_t* queue, void* data){
bool succes;
if ((queue -> max_lenght) <= (queue -> lenght)){
succes = false;
}
else{
if (queue -> inserting == queue->max_lenght){
queue -> inserting = 0;
}
queue -> structure[queue -> inserting] = data;
queue -> inserting += 1;
queue -> lenght += 1;
succes = true;
}
return succes;
}
/*
* gets the first element from the queue and removes it from the queue
* returns: the first element on success; NULL otherwise
*/
void* pop_from_queue(queue_t *queue){
void* element;
if ((queue->lenght) <= 0){
element = NULL;
}
else{
element = queue -> structure[queue-> extract];
queue -> extract += 1;
queue -> lenght -= 1;
if(queue -> extract == queue -> max_lenght){
queue -> extract = 0;
}
}
return element;
}
/*
* gets idx-th element from the queue
* returns: the idx-th element on success; NULL otherwise
*/
void* get_from_queue(queue_t *queue, int idx){
void* element;
if(idx >= queue -> lenght){
element = NULL;
}
else{
if (queue -> extract + idx >= queue->max_lenght){
element = &queue -> structure[queue->extract+idx - queue-> max_lenght];
}
else{
element = &queue -> structure[queue-> extract+idx];
}
}
return element;
}
/* gets number of stored elements */
int get_queue_size(queue_t *q){
return q -> lenght;
}
当我尝试调用 pop_from_queue 时,我仍然收到来自 valgring 的消息,表明我位于数组之外。例如:
==236== Invalid read of size 4
==236== at 0x4009C8: pop_from_queue (queue.c:53)
==236== by 0x400721: pop (main.c:33)
==236== by 0x400817: main (main.c:78)
==236== Address 0x51fc040 is 0 bytes inside a block of size 24 free'd
==236== at 0x4C2BD57: free (vg_replace_malloc.c:530)
==236== by 0x40073D: pop (main.c:35)
==236== by 0x400817: main (main.c:78)
==236== Block was alloc'd at
==236== at 0x4C2AC3D: malloc (vg_replace_malloc.c:299)
==236== by 0x4008B8: create_queue (queue.c:10)
==236== by 0x400798: main (main.c:57)
==236==
==236== Invalid read of size 8
==236== at 0x4009DC: pop_from_queue (queue.c:57)
==236== by 0x400721: pop (main.c:33)
==236== by 0x400817: main (main.c:78)
==236== Address 0x51fc050 is 16 bytes inside a block of size 24 free'd
==236== at 0x4C2BD57: free (vg_replace_malloc.c:530)
==236== by 0x40073D: pop (main.c:35)
==236== by 0x400817: main (main.c:78)
==236== Block was alloc'd at
==236== at 0x4C2AC3D: malloc (vg_replace_malloc.c:299)
==236== by 0x4008B8: create_queue (queue.c:10)
==236== by 0x400798: main (main.c:57)
==236==
==236== Invalid read of size 4
==236== at 0x4009E4: pop_from_queue (queue.c:57)
==236== by 0x400721: pop (main.c:33)
==236== by 0x400817: main (main.c:78)
==236== Address 0x51fc048 is 8 bytes inside a block of size 24 free'd
==236== at 0x4C2BD57: free (vg_replace_malloc.c:530)
==236== by 0x40073D: pop (main.c:35)
==236== by 0x400817: main (main.c:78)
==236== Block was alloc'd at
==236== at 0x4C2AC3D: malloc (vg_replace_malloc.c:299)
==236== by 0x4008B8: create_queue (queue.c:10)
==236== by 0x400798: main (main.c:57)
==236==
==236== Invalid read of size 4
==236== at 0x4009FB: pop_from_queue (queue.c:58)
==236== by 0x400721: pop (main.c:33)
==236== by 0x400817: main (main.c:78)
==236== Address 0x51fc048 is 8 bytes inside a block of size 24 free'd
==236== at 0x4C2BD57: free (vg_replace_malloc.c:530)
==236== by 0x40073D: pop (main.c:35)
==236== by 0x400817: main (main.c:78)
==236== Block was alloc'd at
==236== at 0x4C2AC3D: malloc (vg_replace_malloc.c:299)
==236== by 0x4008B8: create_queue (queue.c:10)
==236== by 0x400798: main (main.c:57)
==236==
etc.
我是结构初学者,因此欢迎任何帮助。
编辑:这些错误位于以下行:
53 if ((queue->lenght) <= 0){
57 element = queue -> structure[queue-> extract];
58 queue -> extract += 1;
59 queue -> lenght -= 1;
60 if(queue -> extract == queue -> max_lenght){
89 return q -> lenght;
我的程序主要方法:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "queue.h"
/* allocate new integer with value a and add it to the queue */
void add(int a, queue_t *queue)
{
int *p = (int*)malloc(sizeof(int));
*p = a;
bool ret = push_to_queue(queue, (void*)p);
if (!ret) {
// free memory on failure
free(p);
}
}
/* print the int value on pointer p */
void print_int(void *p)
{
if(p != NULL){
printf("%d\n", *((int*)p));
} else {
printf("NULL\n");
}
}
/* pop from the queue, print and free the element */
void pop(queue_t *queue)
{
void *p = pop_from_queue(queue);
print_int(p);
free(queue);
}
/* get i-th element and print it (do not remove them) */
void get(int idx, queue_t *queue)
{
print_int(get_from_queue(queue, idx));
}
/*
* TEST PROGRAM
* - reads commands from stdin and executes them in the queue
*/
int main(int argc, char *argv[])
{
int n;
/* the tested queue */
queue_t *queue;
// read the size of the queue
scanf("%d", &n);
// create queue
queue = create_queue(n);
while (true) {
char s[2];
// read one command
int ret = scanf("%1s", s);
if (ret != 1) {
break;
}
// add command
if (s[0] == 'a') {
int a;
// read the argument of the command
ret = scanf("%d", &a);
if (ret != 1) {
break;
}
add(a, queue);
// remove command
} else if (s[0] == 'r') {
pop(queue);
// get command
} else if (s[0] == 'g') {
int a;
// read the argument of the command
ret = scanf("%d", &a);
if (ret != 1) {
break;
}
get(a, queue);
}
}
// remove rest of the elements in the queue
while (get_queue_size(queue)) {
void *p = pop_from_queue(queue);
free(p);
}
// free memory
delete_queue(queue);
queue = NULL;
// return 0 on succes
return 0;
}
最佳答案
通过肉眼查看代码,我发现了一些直接的问题。
/* pop from the queue, print and free the element */
void pop(queue_t *queue)
{
void *p = pop_from_queue(queue);
print_int(p);
free(queue);
}
我不认为你的意思是释放整个 queue
在这里,而是p
.
void delete_queue(queue_t *queue){
free(queue->structure);
free(queue);
}
自 queue->structure
是一个指针列表,这只释放列表的内存。它指向的内存仍然需要释放。这可能是调用者的责任,但也可以将其转移到队列上。
对于这样的通用结构,您通常会为该结构提供一个函数指针,该指针知道如何释放队列中的内存。一个很好的例子,看看 the initializer of GLib's pointer arrays take a destroy function .
<小时/>bool push_to_queue(queue_t* queue, void* data){
bool succes;
if ((queue -> max_lenght) <= (queue -> lenght)){
succes = false;
}
else{
if (queue -> inserting == queue->max_lenght){
queue -> inserting = 0;
}
queue -> structure[queue -> inserting] = data;
queue -> inserting += 1;
queue -> lenght += 1;
succes = true;
}
return succes;
}
if ((queue -> max_lenght) <= (queue -> lenght))
包含无效状态,其中 queue -> max_lenght
小于 queue->lenght
。这绝对不应该发生。
最好使用 assert
明确检查所有内容是否有效。 。这是一个调试语句,断言必须为真,如 assert( queue->length <= queue->max_length )
。如果不是,程序将崩溃并通知您断言失败。否则,您的代码会将尝试插入太多元素和已经拥有太多元素视为相同。
在push_to_queue
开头处放置该断言您可以查看if( queue->max_length == queue->length )
.
我建议您在尝试在更大的程序中使用队列库之前对其进行单元测试。使用正常情况和边缘情况测试每种方法。例如...
void test_delete_queue() {
queue_t *q = create_queue(3);
int nums[3] = {4,5,6};
for( int i = 0; i < 3; i++ ) {
push_to_queue(q, &num);
}
delete_queue(q);
}
虽然这似乎不包含任何测试,但它可以让您知道 delete_queue
不会出现段错误,并且使用 valgrind 运行它会检测到任何泄漏。
再举个例子,在阅读你的代码时,我对 queue->inserting
高度怀疑。和queue->extracting
。在我看来,如果你推和弹出足够多,它们就会不同步。所以我测试了它。而且,令我惊讶的是,它有效!现在我们确信这不是问题。
void test_push_pop() {
queue_t *q = create_queue(3);
int nums[4] = {10, 20, 30, 40};
/* Push twice then pop once */
assert( push_to_queue(q, &nums[0]) );
assert( push_to_queue(q, &nums[1]) );
assert( (int*)pop_from_queue(q) == &nums[0] );
/* Push and pop again */
assert( push_to_queue(q, &nums[2]) );
assert( (int*)pop_from_queue(q) == &nums[1] );
/* Push one more than the max length. This should be ok
as we've already popped twice */
assert( push_to_queue(q, &nums[3] ) );
assert( (int*)pop_from_queue(q) == &nums[2] );
assert( (int*)pop_from_queue(q) == &nums[3] );
assert( get_queue_size(q) == 0 );
delete_queue(q);
}
但是对 pop
进行了类似的测试行不通,因为它会释放整个队列。
关于c - 结构中大小的读取无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41173881/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!