各路大牛晚上好!
小弟初次在Linux系统下尝试进行C语音编程,不太顺利!调用一个printf函数,就触发了断言!已经3个晚上了,分别在ChinaUnix和CSDN求助,都没能解决!
请大牛帮忙走查一下代码,帮忙看下为什么会触发断言呢?又应当如何修改呢?先谢谢了!
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "init_array.h"
int variable_length_array(int **parray, int length)
{
if (*parray != NULL)
{
fprintf(stderr, "The array not initiated!\n");
return FAIL;
}
if (length <=0)
{
fprintf(stderr, "The length error!\n");
return FAIL;
}
*parray = (int *)malloc(length);
memset(*parray, 0x00, sizeof(int) * length);
return SUCC;
}
int main(int argv, char *argc[])
{
int *array = NULL;
int length = 0;
scanf("%d", &length);
if (variable_length_array(&array, length) == SUCC)
{
for (int i = 0; i < length; i++)
{
printf("The index: %d, the content: %d, the address: %p.\n", i, array[i], array + i); // 断言所在行!
}
}
else
{
fprintf(stderr, "Initiate array fail!\n");
return 1;
}
return 0;
}
断言信息如下:
init_array.out: malloc.c:2427: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted
冯宝宝