Linked list allocation problem

N

Nikos Mitas

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...
 
A

Alexei A. Frounze

Nikos Mitas said:
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
 
S

Skarmander

Nikos said:
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.
 
P

pete

Nikos said:
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 */
 
N

Nikos Mitas

Alexei said:
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.
 
S

Skarmander

Nikos said:
Alexei said:
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.
 
N

Nikos Mitas

Skarmander said:
Nikos said:
Alexei said:
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
:)
 
B

Barry Schwarz

Alexei said:
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>>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top