struct/malloc failure

Discussion in 'C Programming' started by Sandy Beech, Oct 16, 2010.

  1. Sandy Beech

    Sandy Beech Guest

    Hey

    I'm trying to dynamically allocate space for a struct, and I get this
    error:

    "appCheck.c", line 159: warning(1515): a value of type "struct Minipath
    *"
    cannot be assigned to an entity of type "struct MiniPath *"

    which makes no sense to me. I have to use cc to compile because it is
    the only thing available on some machines where this needs to run. On my
    machine I get the above warning, but if I run it, it works correctly.
    Problem is, I need it to run on a different machine where that is an
    error, not a warning. What am I missing? Any help?
    It's declared:

    struct MiniPath
    {
    char third [15];
    char* fourth [15];
    int places;

    };

    Then in another struct:

    struct Path
    {
    char second [15];
    struct MiniPath* path [15];
    int splits;

    } pathList[200];

    And later:

    pathList[currPath].path[pathList[currPath].splits] =
    (struct Minipath *)malloc(sizeof(char[15])+sizeof(struct MiniPath*[15])
    +sizeof(int));
     
    Sandy Beech, Oct 16, 2010
    #1
    1. Advertising

  2. Sandy Beech

    Seebs Guest

    On 2010-10-16, Sandy Beech <> wrote:
    > Hey
    >
    > I'm trying to dynamically allocate space for a struct, and I get this
    > error:
    >
    > "appCheck.c", line 159: warning(1515): a value of type "struct Minipath
    > *"
    > cannot be assigned to an entity of type "struct MiniPath *"
    > which makes no sense to me.


    C is case sensitive.

    > pathList[currPath].path[pathList[currPath].splits] =
    > (struct Minipath *)malloc(sizeof(char[15])+sizeof(struct MiniPath*[15])
    > +sizeof(int));


    This looks very, very, confused. First, the cast is probably useless. So
    let's break this down. We have apparently something which has a .path[]
    member, so let's assume it's a struct Path. x.path[SOMETHING] is an
    object of type "struct MiniPath *". You are trying to assign to it
    the result of a malloc operation, but instead of "sizeof(struct MiniPath)",
    you're requesting the size of an array of 15 characters plus the size of an
    array of fifteen pointers plus the size of an int. Why?

    Be aware that on many systems:

    struct foo { char x[15]; struct foo *bar[15]; int i; }
    will NOT have that size -- it can have padding.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Oct 16, 2010
    #2
    1. Advertising

  3. Sandy Beech wrote:
    > Hey
    >
    > I'm trying to dynamically allocate space for a struct, and I get this
    > error:
    >
    > "appCheck.c", line 159: warning(1515): a value of type "struct Minipath
    > *"
    > cannot be assigned to an entity of type "struct MiniPath *"
    >
    > which makes no sense to me. I have to use cc to compile because it is
    > the only thing available on some machines where this needs to run. On my
    > machine I get the above warning, but if I run it, it works correctly.
    > Problem is, I need it to run on a different machine where that is an
    > error, not a warning. What am I missing? Any help?
    > It's declared:
    >
    > struct MiniPath
    > {
    > char third [15];
    > char* fourth [15];
    > int places;
    >
    > };
    >
    > Then in another struct:
    >
    > struct Path
    > {
    > char second [15];
    > struct MiniPath* path [15];
    > int splits;
    >
    > } pathList[200];
    >
    > And later:
    >
    > pathList[currPath].path[pathList[currPath].splits] =
    > (struct Minipath *)malloc(sizeof(char[15])+sizeof(struct MiniPath*[15])
    > +sizeof(int));


    The cast, which you don't need, has got a typo in it. Either correct the
    typo or, much better, delete the cast.

    As an aside, the size you're allocating will often happen to be the same
    as the size of a struct MiniPath (though it's not guaranteed to be), but
    it would be much cleaner to just malloc(sizeof struct MiniPath)
     
    J. J. Farrell, Oct 17, 2010
    #3
  4. On 16 Oct, 21:40, Sandy Beech <> wrote:
    > Hey
    >
    > I'm trying to dynamically allocate space for a struct, and I get this
    > error:
    >
    > "appCheck.c", line 159: warning(1515): a value of type "struct Minipath
    > *"
    >           cannot be assigned to an entity of type "struct MiniPath *"


    you specify two different types. Check the spelling.

    > which makes no sense to me. I have to use cc to compile because it is
    > the only thing available on some machines where this needs to run. On my
    > machine I get the above warning, but if I run it, it works correctly.
    > Problem is, I need it to run on a different machine where that is an
    > error, not a warning. What am I missing? Any help?
    > It's declared:
    >
    > struct MiniPath
    > {
    >   char third [15];
    >   char* fourth [15];
    >   int places;
    >
    > };
    >
    > Then in another struct:
    >
    > struct Path
    > {
    >   char second [15];
    >   struct MiniPath* path [15];
    >   int splits;
    >
    > } pathList[200];
    >
    > And later:
    >
    > pathList[currPath].path[pathList[currPath].splits] =
    > (struct Minipath *)malloc(sizeof(char[15])+sizeof(struct MiniPath*[15])
    > +sizeof(int));


    as someone else pointed out, this is a mess. Remove the cast and use
    the sizeof value as the malloc parameter and things look much simpler

    pathList[currPath].path[pathList[currPath].splits] =
    malloc(sizeof(struct MiniPath*));

    some prefer a variable instead of a type in the sizeof, though in this
    case I'm not convinced it's any clearer.

    pathList[currPath].path[pathList[currPath].splits] =
    malloc(sizeof
    *pathList[currPath].path[pathList[currPath].splits]);
     
    Nick Keighley, Oct 17, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    0
    Views:
    582
    Ilias Lazaridis
    Feb 1, 2005
  2. John Eskie

    Non-failure guarantied malloc/new

    John Eskie, Dec 24, 2003, in forum: C++
    Replies:
    11
    Views:
    594
    Jeff Schwab
    Dec 26, 2003
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,391
    Chris Fogelklou
    Apr 20, 2004
  4. gooch

    malloc behavior on failure

    gooch, Feb 28, 2006, in forum: C Programming
    Replies:
    12
    Views:
    506
    Christian Bau
    Mar 1, 2006
  5. Replies:
    0
    Views:
    481
Loading...

Share This Page