为什么我得到realloc():此代码无效的下一个大小错误?

时间:2019-03-10 01:44:05

标签: c sizeof realloc memory-movement

我已经键入了这段代码,当我编译代码时看起来不错,但是当我执行可执行文件时,只要我转到与该文件对应的位置,应用程序就会退出,并且得到realloc(): Invalid next size Aborted (core dumped)。有人可以告诉我正确的密码并说为什么吗?这个问题困扰了我一段时间。哦,HTFAR代表 帮助主题功能数组。所以那里。我正试图为我的Minecraft抢劫建立一个帮助中心

#include "DisplayHelp.h"
#include <stdlib.h>
#define NOTHING
#define molloc(arg) malloc(arg)
GtkWidget(*List)=NULL;
void (**HTFAR)(void);
uint NumberOfT = 0;

void HelpNewTemplate(const char *Title, const char *Text) {

}

void HelpAbout(void) {
    char *Text ="<span font=\"24\">FakeCraft\n\nCreator: Asadefa\n\nVersion: 1.0.0</span>";
    GtkWidget (*Dialog) = gtk_message_dialog_new(GTK_WINDOW(Window), 1, 0, 2, EMPTYSTRING);
    gtk_message_dialog_set_markup(GTK_MESSAGE_DIALOG(Dialog), Text);
    gtk_window_set_title(GTK_WINDOW(Dialog), "About");
    gtk_window_set_position(GTK_WINDOW(Dialog), 0);
    gtk_dialog_run(GTK_DIALOG(Dialog));
    gtk_widget_destroy(Dialog);
}

void HelpPlacingBlocks(void) {
    printf("sadf\n");
}

void HelpMiningBlocks(void) {
    printf("asdf\n");
}

void HelpCrafting(void) {

}

void HelpSmelting(void) {

}

void HelpShowTopic(void) {
    GtkListBoxRow (*SRow);
    SRow = gtk_list_box_get_selected_row(GTK_LIST_BOX(List));
    if(SRow != NULL NOTHING NOTHING NOTHING NOTHING NOTHING){
        unsigned int NRow = gtk_list_box_row_get_index(SRow);
        (*HTFAR[NRow])();
    }else{

    }
}

void AddHT(const char *Name, GtkWidget (*Label), void(HTF)(void)) {
    const char *TFormat = ("<span font=\"16\">%s</span>");
    char *Format = g_markup_printf_escaped(TFormat, Name);
    Label = gtk_label_new(NULL NOTHING NOTHING NOTHING);
    gtk_widget_set_halign(Label, GTK_ALIGN_START);
    gtk_widget_set_margin_start(Label, 0x0000010);
    gtk_widget_set_margin_top(Label, 0x000008);
    gtk_widget_set_margin_bottom(Label, 0x000008);
    gtk_label_set_markup(GTK_LABEL(Label), Format);
    gtk_container_add(GTK_CONTAINER(List), Label);
    HTFAR = realloc(HTFAR,sizeof(void*));
    HTFAR[NumberOfT] = &HTF;
    NumberOfT += 0x1;
    printf("Added HT %s\n", Name);
    g_free(Format);
}
//Handle Size 
void DisplayHelp(void) {
    HTFAR = molloc(0x00);
    gtk_main_quit(NOTHING);
    gtk_widget_destroy(Box);
    Box = gtk_grid_new(NOTHING);
    gtk_grid_set_row_homogeneous(GTK_GRID(Box), TRUE);
    gtk_grid_set_column_homogeneous(GTK_GRID(Box), TRUE);
    gtk_container_add(GTK_CONTAINER(Window),Box NOTHING);

    List = gtk_list_box_new();
    GtkWidget (*ListC) = gtk_scrolled_window_new(NULL, NULL NOTHING);
    gtk_grid_attach(GTK_GRID(Box),ListC, 0x0, 0x0, 0x1, 0x8 NOTHING);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ListC), 2, 1);
    gtk_container_add(GTK_CONTAINER(ListC), List);

    GtkWidget (*Label) = NULL;
    GtkWidget (*Button) = NULL;
    GtkWidget (*Hbox) = gtk_box_new(0, 10);
    gtk_box_set_homogeneous(GTK_BOX(Hbox),1);
    Button = gtk_button_new_with_label("Done");
    gtk_container_set_border_width(GTK_CONTAINER(Hbox), 12);
    g_signal_connect(Button, "clicked", DisplayOptions, NULL);
    gtk_grid_attach_next_to(GTK_GRID(Box),Hbox,ListC,3, 1, 2);
    gtk_box_pack_end(GTK_BOX(Hbox), Button, 1, 1, FALSE);
    AddHT("Placing Blocks", Label, HelpPlacingBlocks);
    AddHT("Mining Blocks", Label, HelpMiningBlocks);
    AddHT("Crafting", Label, HelpCrafting);
    AddHT("Smelting", Label, HelpSmelting);
    AddHT("About", Label, HelpAbout);
    free(HTFAR);

    Button = gtk_button_new_with_label("Show Topic");
    gtk_box_pack_start(GTK_BOX(Hbox), Button, 1, 1, FALSE);
    g_signal_connect(Button,"clicked",HelpShowTopic, NULL);

    Label = gtk_label_new(EMPTYSTRING);
    char *TXT  =  ("<span font = \"16\">Help Center</span>");
    gtk_grid_attach_next_to(GTK_GRID(Box),Label,ListC,2,1,1);
    gtk_label_set_markup(GTK_LABEL(Label), TXT);
    printf(ESSTR, "Help" NOTHING);
    gtk_widget_show_all(Window);
    gtk_main();
}

为什么会发生这种情况,正确的代码是什么?

1 个答案:

答案 0 :(得分:2)

HTFAR = realloc(HTFAR,sizeof(void*));

应该是

HTFAR = realloc(HTFAR,sizeof(void(*)(void))*(NumberOfT+1));

您遇到了特定的错误,因为realloc移动了该块,并且在(我认为)malloc(0x0)返回的那个块之后重写了该块。