Linked list allocation problem

Discussion in 'C Programming' started by Nikos Mitas, Oct 8, 2005.

  1. Nikos Mitas

    Nikos Mitas Guest

    I have the following structure:

    struct DIRECTORY{

    char nameDir[40];
    struct DIRECTORY *parent;
    int numSubDir;
    struct DIRECTORY *subDir[10];
    };

    when i did this

    struct DIRECTORY *currentDir;
    currentDir = malloc(sizeof(struct DIRECTORY) );

    didn't i allocate all the linked structures?
    Meaning is "currentDir->subDir[0]->nameDir" valid or not?

    The problem is that when i do this:

    strcpy(currentDir->subDir[0]->nameDir,newDirName);


    It produces a runtime error which i can not find.No warnings,no errors
    on compilation.

    newDirName is a pointer with some data...
     
    Nikos Mitas, Oct 8, 2005
    #1
    1. Advertising

  2. "Nikos Mitas" <> wrote in message
    news:7fb84$4347f670$915e9d42$...
    > I have the following structure:
    >
    > struct DIRECTORY{
    >
    > char nameDir[40];
    > struct DIRECTORY *parent;
    > int numSubDir;
    > struct DIRECTORY *subDir[10];
    > };
    >
    > when i did this
    >
    > struct DIRECTORY *currentDir;
    > currentDir = malloc(sizeof(struct DIRECTORY) );
    >
    > didn't i allocate all the linked structures?


    No, you did not. You never even told what and where the linked structures
    are, so how would anyone or anything know what to link?
    There's no magic, you must make your hands dirty anyway. Come down on earth.

    Alex
     
    Alexei A. Frounze, Oct 8, 2005
    #2
    1. Advertising

  3. Nikos Mitas

    Skarmander Guest

    Nikos Mitas wrote:
    > I have the following structure:
    >
    > struct DIRECTORY{
    >
    > char nameDir[40];
    > struct DIRECTORY *parent;
    > int numSubDir;
    > struct DIRECTORY *subDir[10];
    > };
    >
    > when i did this
    >
    > struct DIRECTORY *currentDir;
    > currentDir = malloc(sizeof(struct DIRECTORY) );
    >
    > didn't i allocate all the linked structures?
    > Meaning is "currentDir->subDir[0]->nameDir" valid or not?
    >

    Not unless you've assigned something to currentDir -> subDir[0] first,
    possibly a pointer to an existing struct DIRECTORY.

    I can't tell if you did, of course, since you post no code for this.

    > The problem is that when i do this:
    >
    > strcpy(currentDir->subDir[0]->nameDir,newDirName);
    >

    Valid when currentDir -> subDir[0] is a pointer to valid memory and
    newDirName a pointer to a null-terminated string no longer than 39
    characters. Again, I can't tell if they are.

    > It produces a runtime error which i can not find.No warnings,no errors
    > on compilation.
    >

    Because the code you posted does not contain any errors. The code you
    didn't post no doubt does.

    > newDirName is a pointer with some data...


    No doubt. Doesn't say much.

    S.
     
    Skarmander, Oct 8, 2005
    #3
  4. Nikos Mitas

    pete Guest

    Nikos Mitas wrote:
    >
    > I have the following structure:
    >
    > struct DIRECTORY{
    >
    > char nameDir[40];
    > struct DIRECTORY *parent;
    > int numSubDir;
    > struct DIRECTORY *subDir[10];
    > };
    >
    > when i did this
    >
    > struct DIRECTORY *currentDir;
    > currentDir = malloc(sizeof(struct DIRECTORY) );
    >
    > didn't i allocate all the linked structures?
    > Meaning is "currentDir->subDir[0]->nameDir" valid or not?


    No.
    currentDir->subDir is an array of pointers.
    currentDir->subDir[0] is an unallocated pointer.

    /* BEGIN new.c */

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

    #define DIRS 10

    int main(void)
    {
    struct DIRECTORY {
    char nameDir[40];
    struct DIRECTORY *parent;
    int numSubDir;
    struct DIRECTORY *subDir[DIRS];
    };
    struct DIRECTORY *currentDir;
    char *newDirName = "Does newDirName point to a string?";

    currentDir = malloc(sizeof *currentDir);
    if (currentDir != NULL) {
    int index;

    for (index = 0; index != DIRS; ++index) {
    currentDir -> subDir[index]
    = malloc(sizeof *currentDir -> subDir[index]);
    if (currentDir -> subDir[index] == NULL) {
    printf("malloc[%d]\n", index);
    exit(EXIT_FAILURE);
    }
    }
    strcpy(currentDir -> subDir[0] -> nameDir, newDirName);
    puts (currentDir -> subDir[0] -> nameDir);
    for (index = 0; index != DIRS; ++index) {
    free(currentDir -> subDir[index]);
    }
    free(currentDir);
    } else {
    puts("malloc");
    }
    return 0;
    }

    /* END new.c */


    --
    pete
     
    pete, Oct 8, 2005
    #4
  5. Nikos Mitas

    Nikos Mitas Guest

    Alexei A. Frounze wrote:
    > "Nikos Mitas" <> wrote in message
    > news:7fb84$4347f670$915e9d42$...
    >
    >>I have the following structure:
    >>
    >>struct DIRECTORY{
    >>
    >>char nameDir[40];
    >>struct DIRECTORY *parent;
    >>int numSubDir;
    >>struct DIRECTORY *subDir[10];
    >>};
    >>
    >>when i did this
    >>
    >>struct DIRECTORY *currentDir;
    >> currentDir = malloc(sizeof(struct DIRECTORY) );
    >>
    >>didn't i allocate all the linked structures?

    >
    >
    > No, you did not. You never even told what and where the linked structures
    > are, so how would anyone or anything know what to link?
    > There's no magic, you must make your hands dirty anyway. Come down on earth.
    >
    > Alex
    >
    >

    Actually i think it is rather stupid.I have already declared that i want
    a structure that will have 10 other structures of the same kind in it.So
    why does it allocate only the root structure.There is no reason to not
    allocate them since i will use them.If i didnt want all of them i
    wouldnt declare them.But if that the way it works then that is how i
    must do it.

    Nevertheless thank you it worked.
     
    Nikos Mitas, Oct 8, 2005
    #5
  6. Nikos Mitas

    Skarmander Guest

    Nikos Mitas wrote:
    > Alexei A. Frounze wrote:
    >
    >> "Nikos Mitas" <> wrote in message
    >> news:7fb84$4347f670$915e9d42$...
    >>
    >>> I have the following structure:
    >>>
    >>> struct DIRECTORY{
    >>>
    >>> char nameDir[40];
    >>> struct DIRECTORY *parent;
    >>> int numSubDir;
    >>> struct DIRECTORY *subDir[10];
    >>> };
    >>>
    >>> when i did this
    >>>
    >>> struct DIRECTORY *currentDir;
    >>> currentDir = malloc(sizeof(struct DIRECTORY) );
    >>>
    >>> didn't i allocate all the linked structures?

    >>
    >>
    >>
    >> No, you did not. You never even told what and where the linked structures
    >> are, so how would anyone or anything know what to link?
    >> There's no magic, you must make your hands dirty anyway. Come down on
    >> earth.
    >>
    >> Alex
    >>
    >>

    > Actually i think it is rather stupid.I have already declared that i want
    > a structure that will have 10 other structures of the same kind in it.So
    > why does it allocate only the root structure.

    <snip>

    Because if it has to allocate those 10 structures, it has to do so by
    allocating room for the 10 structures each of those structures contains,
    which it has to do by allocating room for the 10 structures each of
    those structures contains, which...

    Think about what you're asking for a moment, and I hope you'll see why
    your approach makes no sense.

    S.
     
    Skarmander, Oct 8, 2005
    #6
  7. Nikos Mitas

    Nikos Mitas Guest

    Skarmander wrote:
    > Nikos Mitas wrote:
    >
    >> Alexei A. Frounze wrote:
    >>
    >>> "Nikos Mitas" <> wrote in message
    >>> news:7fb84$4347f670$915e9d42$...
    >>>
    >>>> I have the following structure:
    >>>>
    >>>> struct DIRECTORY{
    >>>>
    >>>> char nameDir[40];
    >>>> struct DIRECTORY *parent;
    >>>> int numSubDir;
    >>>> struct DIRECTORY *subDir[10];
    >>>> };
    >>>>
    >>>> when i did this
    >>>>
    >>>> struct DIRECTORY *currentDir;
    >>>> currentDir = malloc(sizeof(struct DIRECTORY) );
    >>>>
    >>>> didn't i allocate all the linked structures?
    >>>
    >>>
    >>>
    >>>
    >>> No, you did not. You never even told what and where the linked
    >>> structures
    >>> are, so how would anyone or anything know what to link?
    >>> There's no magic, you must make your hands dirty anyway. Come down on
    >>> earth.
    >>>

    I wasnt referring on the where everything is.I would of course do the
    linking of the lists.It didnt do the allocation of what i was asking.

    > <snip>
    >
    > Because if it has to allocate those 10 structures, it has to do so by
    > allocating room for the 10 structures each of those structures contains,
    > which it has to do by allocating room for the 10 structures each of
    > those structures contains, which...
    >
    > Think about what you're asking for a moment, and I hope you'll see why
    > your approach makes no sense.
    >
    > S.

    I was just about to write my apologies on that, you got there first.Yes
    it doesnt.So sorry
    :)
     
    Nikos Mitas, Oct 8, 2005
    #7
  8. On Sat, 08 Oct 2005 19:17:47 +0200, Nikos Mitas <>
    wrote:

    >Alexei A. Frounze wrote:
    >> "Nikos Mitas" <> wrote in message
    >> news:7fb84$4347f670$915e9d42$...
    >>
    >>>I have the following structure:
    >>>
    >>>struct DIRECTORY{
    >>>
    >>>char nameDir[40];
    >>>struct DIRECTORY *parent;
    >>>int numSubDir;
    >>>struct DIRECTORY *subDir[10];
    >>>};
    >>>
    >>>when i did this
    >>>
    >>>struct DIRECTORY *currentDir;
    >>> currentDir = malloc(sizeof(struct DIRECTORY) );
    >>>
    >>>didn't i allocate all the linked structures?

    >>
    >>
    >> No, you did not. You never even told what and where the linked structures
    >> are, so how would anyone or anything know what to link?
    >> There's no magic, you must make your hands dirty anyway. Come down on earth.
    >>
    >> Alex
    >>
    >>

    >Actually i think it is rather stupid.I have already declared that i want
    >a structure that will have 10 other structures of the same kind in it.So


    No you did not. You declared you wanted a structure that had 10
    ***pointers*** to structures in it. Each of those pointers could
    point to the start of an array of structures and you would not be
    limited to 10. In any event, a pointer to struct is different that
    the struct itself. You allocation did allocate space for the pointers
    but not space for the pointers to point to.

    You didn't have this mental lapse when you defined currentDir. First
    you defined it, creating the pointer, then you used malloc to create
    space for it to point to.

    Furthermore, it is not possible for a structure to have within it a
    nested structure of the same type. You can nest a structure of a
    different type but not the same type.

    >why does it allocate only the root structure.There is no reason to not
    >allocate them since i will use them.If i didnt want all of them i
    >wouldnt declare them.But if that the way it works then that is how i
    >must do it.
    >
    >Nevertheless thank you it worked.



    <<Remove the del for email>>
     
    Barry Schwarz, Oct 8, 2005
    #8
    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. Chris Ritchey
    Replies:
    7
    Views:
    492
    emerth
    Jul 10, 2003
  2. Henk
    Replies:
    4
    Views:
    857
  3. fool
    Replies:
    14
    Views:
    526
    Barry Schwarz
    Jul 3, 2006
  4. joshd
    Replies:
    12
    Views:
    684
    John Carson
    Oct 2, 2006
  5. Jeff Bown

    Linked list allocation

    Jeff Bown, Dec 17, 2007, in forum: C Programming
    Replies:
    8
    Views:
    1,515
    Richard Harter
    Dec 27, 2007
Loading...

Share This Page