Tech Support > Computers & Technology > Programming > pointers to structs
pointers to structs
Posted by John Hanley on July 14th, 2004


I am creating a linked list for a DOS program. I have 2 structs:

struct data_record
{
struct data_record * next;
};

struct data_list
{
struct data_record * head;
struct data_record * tail;
};

int main()
{
struct data_list * list;

list->head = NULL;
list->tail = NULL;
}

When I compile then run this, I get: "Exiting due to Signal SIGSEGV. Page
Fault at ..."

Can I not assign NULL to these pointers? If I take the 2 assignments out,
it runs ok.

I am using the djgpp 32 bit compiler for DOS.

Any suggestions as to why I can't initialize my head and tail to NULL?

Thanks!

John


Posted by Willem on July 14th, 2004


John wrote:
) I am creating a linked list for a DOS program. I have 2 structs:
)
) struct data_record
) {
) struct data_record * next;
) };
)
) struct data_list
) {
) struct data_record * head;
) struct data_record * tail;
) };
)
) int main()
) {
) struct data_list * list;
)
) list->head = NULL;
) list->tail = NULL;
) }

list is a pointer that doesn't point anywhere.

You probably want to take out the * in the variable declaration and put
dots where the arrows are.

) When I compile then run this, I get: "Exiting due to Signal SIGSEGV. Page
) Fault at ..."
)
) Can I not assign NULL to these pointers? If I take the 2 assignments out,
) it runs ok.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Posted by Jens.Toerring@physik.fu-berlin.de on July 15th, 2004


John Hanley <jdhanley@telusplanet.net> wrote:
Here you define a pointer to a structure data_list. But that pointer
points to nothing you "own" yet, just to some completely random location
in memory.

And here try to you assign a value to a member of zhe structure that
doesn't exist yet.

And here again, of course.

Well, then there's nothing left of your program;-) Problem is that
you don't assign to the pointer ('left') but to what it's pointing to.
But it's not pointing to anything useful yet. At the moment it just
points to some random location in memory. Before you can assign to
something via the pointer (and that is what you try to do when you
write "list->head = NULL;") you first have to make 'list' point to
some memory you own. There are basically two ways to make 'list'
point to to some "real" memory, i.e. memory you own: 1) define a
structure and assign its address to 'list' or 2) call malloc() to
ask the operating system to give you some memory and then assign
the return value to the pointer.

Your program would work quite fine if you wouldn't use a pointer
but a "real" structure:

int main( void )
{
struct data_list list;

list.head = NULL
list.tail = NULL;

return EXIT_SUCCESS;
}

but I guess that's not what you're looking for (I guess you're
trying to create a linked list and then you have to deal with
pointers, but also with the methods of obtaining memory for the
elements of the list...)
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de

Posted by John Hanley on July 15th, 2004


Yikes! I forgot to allocate memory. I feel silly now. I haven't done much
linked list/memory allocation type stuff for a while.

Thanks for pointing out my error.

Best regards,
John



Posted by Darrell Grainger on July 16th, 2004


On Wed, 14 Jul 2004, John Hanley wrote:

The variable list is a pointer that has not been initialized. It is
therefore pointing to a random memory location. When you see something
like:

list->head = NULL;

the compiler will take this to mean, "Go to the memory location list has
been initialized with, then add the offset for the field head and
initialize that memory location to NULL." The crash is happening because
list does not have a valid memory location.

The easiest solution is to change list to:

struct data_list list;

then you can use:

list.head = NULL;
list.tail = NULL;

A little more advanced would be to keep your code the same and allocate
memory for list to use. This involves using the malloc function.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vice.president@whitehouse.gov


Similar Posts