malloc realloc free @ Linux C (invalid address: 0x1010000)
来源:互联网 发布:福特嘉年华轮毂15数据 编辑:程序博客网 时间:2024/05/16 00:04
static int add_single_element_to_array (msg_count_type ** source_array,
int * arr_count_ptr
int * arr_reserved_cnt_ptr
msg_count_type * need_added_element)
{
int status = 0;
int i = 0;
msg_count_type * new_ptr = NULL;
/* Return if the id exsits in the array */
for ( i = 0; i < * arr_count_ptr; i ++, source_array ++ )
{
if (strcmp(need_added_element->id, source_array->id) == 0)
{
return -1;
}
}
/* If get here, add the element to the array */
new_ptr = (*msg_count_type)
block_realloc ((void **) source_array,
arr_count_ptr,
arr_reserved_cnt_ptr,
BLOCK_SIZE,
sizeof (msg_count_type));
strcpy (new_ptr -> id,
need_added_element -> id,
sizeof (new_ptr -> id) - 1);
/* Return */
return status;
}
static void process (Bool is_orign)
{
message_count_type *source_array;
int message_count;
int reserved_cnt;
message_count_type *msg_item;
for (...)
{
add_single_element_to_array (&source_array,
&message_count,
&reserved_cnt,
msg_item);
}
/* Make sure release memory */
free (source_array); /* Runtime error like - invalid address: 0x1010000.... */
}
问题出在 source_array ++ ,经过第一次调用add_single_element_to_array,source_array 已经不再指向首地址,而是尾地址;当调用free释放内存时- 无效地址。使用下标方式取值就好使, source_array[i]->id .
(还没搞明白为什么非要指向首地址才能free?)
- malloc realloc free @ Linux C (invalid address: 0x1010000)
- linux C 之动态内存分配malloc ,calloc,realloc,free
- c 内存动态分配 malloc free calloc realloc
- C++malloc,calloc,realloc,free函数
- C笔记 动态分配 malloc,free,calloc,realloc
- malloc/free/calloc/realloc函数-linux
- calloc(), malloc(), realloc(), free()
- malloc ? realloc ? free ? calloc ?
- malloc/realloc/calloc/free
- malloc ,calloc,free,realloc
- malloc,calloc,realloc,free
- 动态分配malloc realloc free
- malloc、calloc、realloc、free
- malloc(),free()与realloc()
- C语言中alloca,calloc,malloc,free,realloc,sbrk简介
- C malloc()、free()、calloc() 与 realloc() 关系区别
- C语言 calloc, malloc, realloc ,alloca以及 free的用法
- C语言动态内存开辟malloc, realloc ,calloc,free详解
- X264的参数设置
- 开博记
- 从零开始学习OpenGL ES之七 – 变换和矩阵
- inux必学的60个命令
- gethibernatetemplate find条件查询方法
- malloc realloc free @ Linux C (invalid address: 0x1010000)
- Apple's OpenCL
- sed用法入门
- 3.2 表、栈和队列_表ADT
- JAVA中setDate()与setTimestamp()区别
- Oracle10g下jdbc驱动包有什么区别
- C++宏在子类类型“识别”的一种方法
- 单元测试
- 面试题目