Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

set an default struct?!

...NoiK...NoiK Member Posts: 34
hey,

ATM i'm trying to generate a menu build up from a desktop file. Now i'm trying to make an struct in which the info from the .desktop will be initialized. Initializing and parsing i haven't started yet but i'm trying to make an GTK app which can show them.

Therefore i'm using a init function which will initialize a struct with dummy data so i can test it. But when making the struct i get some errors:
warning: 'blaat' is used uninitialized in this function

[code]
//header file:
typedef struct _menu_thing menu_thing;

struct _menu_thing
{
gchar* name;
gchar* tooltip;
GtkAction* action;
//bool is_menu;
int is_menu;
menu_thing *menu;
menu_thing *next;
};

//C-file:
menu_thing* init_menu()
{
menu_thing *blaat;
blaat->name = "test";
blaat->tooltip= "tooltip";
blaat->action = NULL;
blaat->is_menu = 1;
blaat->menu = NULL;
blaat>next = NULL;
return blaat;
}
[/code]
What is the thing i'm doing wrong when making the struct?

Comments

  • JonathanJonathan Member Posts: 2,914
    Hi,

    You need to allocate memory for the struct, otherwise you have an uninitialized pointer (basically, something pointing to...well, who knows where...)

    menu_thing *blaat;

    Should be:

    menu_thing *blaat = malloc(sizeof(struct _menu_thing));

    You need to call free on it sometime later, when you're done with it, too. Do read up on malloc/free and pointers too, it's hard to write much in C without understanding these. :-)

    Jonathan
    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
  • ...NoiK...NoiK Member Posts: 34
    : menu_thing *blaat;
    :
    : Should be:
    :
    : menu_thing *blaat = malloc(sizeof(struct _menu_thing));

    Ah thanx this was the solution, i will try to find more info behind this.
    But it definitly worked so i can keep going :D
  • ...NoiK...NoiK Member Posts: 34
    Maybe it's the same error maybe not,
    i've now created a function which creates models:
    [code]Model *model_get()
    {
    Model* model2;
    model2 = malloc(sizeof(Model));
    model2->name = "test2";
    Model* model;
    model = malloc(sizeof(Model));
    model->name = "test";
    model->menu = model2;
    return model;
    };[/code]
    As you can see model2 is in model.

    But now when i do his in another function:
    [code]static void
    menu_action_menu_shown (GtkWidget *menu,
    MenuTemplatesAction *menu_action,
    Model *items)
    {
    /*...*/
    if(items->menu != NULL)
    {
    g_message(items->name);
    /*...*/
    }
    /*...*/
    }[/code]
    this gives me an segmentation fault:
    ./script2: line 35: 19608 Segmentation fault (core dumped) bash -e $HOME/my program/

    can anyone give me a hint what i'm missing?
  • ...NoiK...NoiK Member Posts: 34
    : Hi,
    :
    : You need to allocate memory for the struct, otherwise you have an
    : uninitialized pointer (basically, something pointing to...well, who
    : knows where...)
    :
    : menu_thing *blaat;
    :
    : Should be:
    :
    : menu_thing *blaat = malloc(sizeof(struct _menu_thing));
    :
    : You need to call free on it sometime later, when you're done with
    : it, too. Do read up on malloc/free and pointers too, it's hard to
    : write much in C without understanding these. :-)
    :
    : Jonathan
    : ###
    : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
    do you have an link for me to read about this?
Sign In or Register to comment.