linked lists. 
Author Message
 linked lists.

I was wondering if someone would mind explaining to me why the following
code does not work.

struct
{
  int number;
  char *name;
  this_struct *next;

Quote:
} this_struct;

void this_function()
{
  struct this_struct *head, *test, *test2;
  test = (this_struct *) malloc (sizeof (this_struct));
  test->number = 8;
  strcpy (test->name, "testing");
  test->next = NULL;
  head = test;

  test2 = (this_struct *) malloc (sizeof (this_struct));      /* It
usually fails here */
  test->next = test2;
  test = test2;

Quote:
}

I believe that the problem is related to me declaring a char * as part
of the struct.  Do I need to malloc() for the space of the char * as
well as for the structure?  When, I switched name to a char[100], the
problem went away.

Thanks,

Jeff Hanks



Sat, 21 Apr 2001 03:00:00 GMT  
 linked lists.
Quote:

>I was wondering if someone would mind explaining to me why the following
>code does not work.

>I believe that the problem is related to me declaring a char * as part
>of the struct.  Do I need to malloc() for the space of the char * as
>well as for the structure?  When, I switched name to a char[100], the
>problem went away.

Exactly ....
The strdup() function is perfect for this application.  It allocates the mem
and does the copy also.  Don't forget to free it when you are done though :)


Sat, 21 Apr 2001 03:00:00 GMT  
 linked lists.
i commented out some code.
see below

Quote:

> I was wondering if someone would mind explaining to me why the following
> code does not work.

> /*struct
> {
>   int number;
>   char *name;
>   this_struct *next;
> } this_struct;*/

struct this_struct
{
    int number;
    char *name;
    struct this_struct *next;

Quote:
};

> /*void this_function()*/

void this_function(void)

Quote:
> {
>   struct this_struct *head, *test, *test2;
>   /*test = (this_struct *) malloc (sizeof (this_struct));*/

    test = malloc(sizeof(struct this_struct));    if(test == 0)
    {
        fprintf(stderr, "malloc() return 0\n");
        return EXIT_FAILURE;
    }

Quote:
>   test->number = 8;

    test->name = malloc(sizeof("testing"));

Quote:
>   strcpy (test->name, "testing");
>   test->next = NULL;
>   head = test;

>   /*test2 = (this_struct *) malloc (sizeof (this_struct));*/      /* It
> usually fails here */

    test2 = malloc(sizeof(struct this_struct));    if(test2 == 0)
    {
        fprintf(stderr, "malloc() return 0\n");
        return EXIT_FAILURE;
    }

/* is a good idea:    test2->next = NULL;  to find the end of the list*/

Quote:
>   test->next = test2;
>   test = test2;
> }

> I believe that the problem is related to me declaring a char * as part
> of the struct.  Do I need to malloc() for the space of the char * as
> well as for the structure?

yes.see the code above.

Quote:
>                       When, I switched name to a char[100], the
> problem went away.

> Thanks,

> Jeff Hanks


good luck.
sorry about my english.
--
---------------------------------------------------------

CCG - Centro de Computa?ao Grafica   http://www.ccg.uc.pt
---------------------------------------------------------



Sat, 21 Apr 2001 03:00:00 GMT  
 linked lists.
Of course you need to allocate space for name, otherwise
 strcpy (test->name, "testing");
will most likely crash your code.
And ALWAYS remember to check for malloc returning NULL and to free memory
when no longer in use, ok?
Good luck,
Cristian



Quote:
> I was wondering if someone would mind explaining to me why the following
> code does not work.

> struct
> {
>   int number;
>   char *name;
>   this_struct *next;
> } this_struct;

> void this_function()
> {
>   struct this_struct *head, *test, *test2;
>   test = (this_struct *) malloc (sizeof (this_struct));
>   test->number = 8;
>   strcpy (test->name, "testing");
>   test->next = NULL;
>   head = test;

>   test2 = (this_struct *) malloc (sizeof (this_struct));      /* It
> usually fails here */
>   test->next = test2;
>   test = test2;
> }

> I believe that the problem is related to me declaring a char * as part
> of the struct.  Do I need to malloc() for the space of the char * as
> well as for the structure?  When, I switched name to a char[100], the
> problem went away.

> Thanks,

> Jeff Hanks




Sat, 21 Apr 2001 03:00:00 GMT  
 linked lists.

Quote:

>I was wondering if someone would mind explaining to me why the following
>code does not work.

>struct
>{
>  int number;
>  char *name;
>  this_struct *next;
>} this_struct;

>void this_function()
>{
>  struct this_struct *head, *test, *test2;
>  test = (this_struct *) malloc (sizeof (this_struct));
>  test->number = 8;
>  strcpy (test->name, "testing");

/* test->name is a pointer to character that has not been
** initialized with a valid value. Thus, the strcpy()
** above will copy the string "testing" to some unknown
** block of memory
*/
Quote:
>  test->next = NULL;
>  head = test;

>  test2 = (this_struct *) malloc (sizeof (this_struct));      /* It
>usually fails here */

/* If your comment above is correct, then the strcpy() mentioned
** previously probably has overwritten some critical value
** required by malloc().
*/
Quote:
>  test->next = test2;
>  test = test2;
>}

>I believe that the problem is related to me declaring a char * as part
>of the struct.  Do I need to malloc() for the space of the char * as
>well as for the structure?

Yes, you've only allocated the space for a pointer. If you want to
embed a character string into your structure, you will have to allocate
space for a character string. This may be by malloc()ing space for
the string and storing the address in test->name or by making
test->name a character string.
Quote:
>When, I switched name to a char[100], the
>problem went away.

That's consistant with the observed program bug.

Quote:

>Thanks,

>Jeff Hanks


Lew Pitcher
System Consultant, Delivery Systems Architecture
Toronto Dominion Bank


(Opinions expressed are my own, not my employer's.)



Sat, 21 Apr 2001 03:00:00 GMT  
 linked lists.

Quote:


>>I was wondering if someone would mind explaining to me why the following
>>code does not work.

>>I believe that the problem is related to me declaring a char * as part
>>of the struct.  Do I need to malloc() for the space of the char * as
>>well as for the structure?  When, I switched name to a char[100], the
>>problem went away.

>Exactly ....
>The strdup() function is perfect for this application.  It allocates the mem
>and does the copy also.  Don't forget to free it when you are done though :)

Unfortunately, the strdup() function is not part of the C language, and its name
is reserved, and can't be used safely in portable C code.
--

Kenan Systems Corporation


Sat, 21 Apr 2001 03:00:00 GMT  
 linked lists.

Quote:
>>Exactly ....
>>The strdup() function is perfect for this application.  It allocates the
mem
>>and does the copy also.  Don't forget to free it when you are done though
:)

>Unfortunately, the strdup() function is not part of the C language, and its
name
>is reserved, and can't be used safely in portable C code.
>--

>Kenan Systems Corporation

True that strdup() is not ANSI approved yet, but I have not run across a
compiler that doesn't have it.  That include gcc, sunworkshop, borland 5.0,
microsoft.  Chances are fair to good that it will port OK.  IMHO.


Sat, 21 Apr 2001 03:00:00 GMT  
 linked lists.

Quote:

> Do not cast the return of malloc. In thiw newsgroup is considered poor
> programming practice

Just curious to know why ?
I mean, wont the compiler throw a wobbly if you try and assign a void
pointer to a pointer declared as pointing to something other than void ?
and it doesn't have to be void* to test for NULL return from malloc -
right ?

--
...Andrew...

From time to time I might take part in discussions in this forum in
which there are points of disagreement. Potential protagonists in those
discussions should note that lack of a response on my part in respect of
any or all of the points raised does not imply a realignment of my
position or opinions to those of said protagonist.

Opinions expressed here are my own, not those of my employer.



Sun, 22 Apr 2001 03:00:00 GMT  
 linked lists.


...

Quote:
>True that strdup() is not ANSI approved yet, but I have not run across a
>compiler that doesn't have it.  That include gcc, sunworkshop, borland 5.0,
>microsoft.  Chances are fair to good that it will port OK.  IMHO.

gcc is a freestanding compiler, it doesn't provide any library functions
(it needs to be built with a separate library package). The compilers
you have listed are a vanishingly small proportion of the number of
platforms/compilers in existence. In no way does this form anything like
a representative sample.

--
-----------------------------------------


-----------------------------------------



Sun, 22 Apr 2001 03:00:00 GMT  
 linked lists.


Quote:

>> Do not cast the return of malloc. In thiw newsgroup is considered poor
>> programming practice

>Just curious to know why ?
>I mean, wont the compiler throw a wobbly if you try and assign a void
>pointer to a pointer declared as pointing to something other than void ?

No, One of the main properties of void * that distinguishes it from, say,
char * is that the compiler will convert implicitly between it and
other pointer to object or incomplete types (i.e. anything except pointers
to functions). Any compiler that doesn't do this is eithjer extremely old
(i.e. not ANSI conforming) or broken (or a compiler for a different
language such as C++).

Quote:
>and it doesn't have to be void* to test for NULL return from malloc -
>right ?

Wrong. Conversion between different pointer types is guaranteed to
preserve a null pointer. malloc returns a null pointer or a pointer to
memory that is suitably aligned for any object type. So if malloc returns
a non-null pointer then converting it to a (non-function) pointer will
preserve the property that it is not null.

--
-----------------------------------------


-----------------------------------------



Sun, 22 Apr 2001 03:00:00 GMT  
 
 [ 23 post ]  Go to page: [1] [2]

 Relevant Pages 

1. link list of a linked list (chained links?)

2. Incompatible NULL Assignments || Linked List inside Linked List

3. Clearing Memory || Linked List INSIDE of a Linked List

4. Freeing a Linked List inside of a Linked List

5. Linked List of Linked Lists

6. Define a linked list of a linked list

7. Link List to Link List, HELP friends

8. linked lists (searching/moving a link)

9. I/O and a linked list of linked lists! HELP

10. Link-list sort algorithm question

11. Need help with doubly linked lists

12. Disapearing linked lists

 

 
Powered by phpBB® Forum Software