sysmalloc的malloc错误

时间:2015-07-23 09:36:37

标签: c malloc

我的C程序出现以下消息错误:

  

a.out:malloc.c:2369:sysmalloc:断言`(old_top ==   (((mbinptr)(((char *)&((av) - > bins [((1) - 1)* 2])) -   __builtin_offsetof(struct malloc_chunk,fd))))&& old_size == 0)|| ((unsigned long)(old_size)> =(unsigned long)(((__ builtin_offsetof   (struct malloc_chunk,fd_nextsize))+((2 *(sizeof(size_t))) - 1))&   〜((2 *(sizeof(size_t))) - 1)))&& ((old_top) - > size& 0x1)&&   ((unsigned long)old_end& pagemask)== 0)'失败。

zsh:abort   (核心倾销)./a.out

此错误已经引发(here),内存已损坏。但有谁能告诉我在我的程序中导致此错误? (当我第一次调用strdup时程序崩溃)

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

char    **cp_env(char **env)
{
    int i;
    char **my_env;

//  Count env size
    i = 0;
    while (env[i])
        i++;

// Malloc env copy
    if (!(my_env = (char**)malloc(sizeof(char*) * i)))
        exit(-1);
    my_env[i] = NULL;

// copy env
    while (i--)
        my_env[i] = strdup(env[i]);

    return(my_env);
}

int         main(int ac, char **av, char **env)
{
    char**  my_env;

    my_env = cp_env(env);

    printf("%s", my_env[0]);

//  free

    return (0);
}

1 个答案:

答案 0 :(得分:2)

我认为,您在

中面临问题
 my_env[i] = NULL;

这是一个接一个。可以使用的最大索引是

my_env[i-1] = NULL;

另外,请malloc() C>php app/console sylius:install Installing Sylius... Step 1 of 4. Checking system requirements. +-------------------------------+----------------------------------------------- ----------------------+ | Issue | Recommendation | +-------------------------------+----------------------------------------------- ----------------------+ | short_open_tag | | | session.auto_start | | | sylius.extensions.accelerator | Install and enable a PHP accelerator like APC (highly recommended). | +-------------------------------+----------------------------------------------- ----------------------+ Success! Your system can run Sylius properly. Step 2 of 4. Setting up the database. Creating Sylius database for environment dev. The command terminated with an error code: 1. [Exception] The command terminated with an error code: 1. 中的家人返回see why not to cast