Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

being self taught leaves too many holes...

Status
Not open for further replies.

zanza

Programmer
Feb 18, 2002
89
US
so the topic is dynamic allocation.

this is the struct im working with
Code:
typedef struct
{
    int RegionCount, NameLength;
    char *name;

    struct
    {
        int TownCount, NameLength;
        char *name;
    }*region;
}*world;
and this is the code that uses it
Code:
    world gia = (world) malloc(sizeof(gia));

    gia->NameLength = 4;
    gia->name = (char *) malloc(sizeof(char) * gia->NameLength);
    sprintf(gia->name, "Gia");

    gia->RegionCount = 3;
    gia->region = malloc(sizeof(gia->region) * gia->RegionCount);

    gia->region[0].NameLength = 7; 
    gia->region[0].name = (char *) malloc(sizeof(char) * gia->region[0].NameLength);
    sprintf(gia->region[0].name, "Plains");

    gia->region[0].NameLength = 7; 
    gia->region[1].name = (char *) malloc(sizeof(char) * gia->region[1].NameLength);
    sprintf(gia->region[1].name, "Forest");

    gia->region[0].NameLength = 10; 
    gia->region[2].name = (char *) malloc(sizeof(char) * gia->region[2].NameLength);
    sprintf(gia->region[2].name, "Mountains");

so... yeah. salem, i know youve told me something about the typecasting way i do malloc()'s, but obviously i didnt listen. ^^;; sorry.

the problem is this: it crashes when sprintf()'ing "Mountains" and prints jibberish instead of "Plains". yet "Forest" works just fine.

foolishness, im sure. however, im self taught and the one person i know in this town thats better than i am is across the continent. so i have no idea where to turn aside from the forums. :)

žÅNžÅ
 
So if I say every other line is wrong, you wouldn't pay any attention? [sad]

> world gia = (world) malloc(sizeof(gia));
You've allocated the size of the pointer, not what it should point to
[tt]world gia = malloc(sizeof(*gia));[/tt]

I've never been a fan of hiding pointers inside typedefs. You should typedef a [tt]world[/tt], not a [tt]*world[/tt].
In fact, it rapidly becomes a mess if you start using const in a meaningful way in your programs.

> gia->region = malloc(sizeof(gia->region) * gia->RegionCount);
Ditto with this one - you need the size of the object, not the size of the pointer to the object
Strangely, you've correctly omitted the cast on this one...

> gia->region[[red]0[/red]].NameLength = 7;
> gia->region[1].name = (char *) malloc(sizeof(char) * gia->region[[red]1[/red]].NameLength);
Give yourself a good kicking when you see this [wink]

Try a separate function to do the repetetive work for you
Code:
struct region setname ( char *name ) {
  struct region result;
  result.NameLength = strlen( name ) + 1;
  result.name = malloc( result.NameLength );
  result.TownCount = 0;
  strcpy( result.name, name );
}
Then you can just do
[tt]gia->region[2] = setname( "Mountains" );[/tt]

Though you will need to extract region as a separate named structure, rather than the anonymous one you have at present.

--
 
hmm... does loading myself into a trebuchet count as a kick? ^_-

well, i suppose it doesnt help that i seem to only want to code when im sleepy. but being 17 and self taught isnt friendly either. oh well, expernience comes through errors... but only if you listen to those who correct them. ^^;;

thank you salem, and ill go try the above suggestions.

žÅNžÅ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top