Problem with my linked list - probably pointers

Problem with my linked list - probably pointers

Post by Soriso » Wed, 13 Oct 1999 04:00:00




> I'm getting file statistics (just name and size for now) and sticking them
> in a linked list. The linked list is ordered in descending size and based on
> a program from a text book explaining linked lists and how to insert new
> records into them.

> The file path prints out garbage but the sizes look OK and in order. I think
> I've screwed up a string or a pointer. The individual records seem to get
> created OK as I've put test points all over the place and got the expect
> results. Can anyone see what's wrong with the list.

There's nothing wrong with the list itself. The problem is that you forget
to allocate space for the pathnames of each file. You have your pointer point
to the local array declared in your function, which will be invalid once you
leave the function. Thus a working version would look like:

else {
  ptr->path = strcpy (calloc (sizeof (char), strlen (path_name) + 1),
path_name);
  ptr->size = statbuf.st_size;
  ptr->next = NULL;

Quote:}

This explicitely allocates enough dynamic memory to fill in the string (plus
the terminating \0) and then copies the string into it.

Same applies if you wanted to free the list. You'd have to free those pathname
strings first and then the list item itself.

Cheers!

 
 
 

Problem with my linked list - probably pointers

Post by Paul » Thu, 14 Oct 1999 04:00:00


Thanks for the help. That should get me out of trouble.



>> I'm getting file statistics (just name and size for now) and sticking
them
>> in a linked list. The linked list is ordered in descending size and based
on
>> a program from a text book explaining linked lists and how to insert new
>> records into them.

>> The file path prints out garbage but the sizes look OK and in order. I
think
>> I've screwed up a string or a pointer. The individual records seem to get
>> created OK as I've put test points all over the place and got the expect
>> results. Can anyone see what's wrong with the list.

>There's nothing wrong with the list itself. The problem is that you forget
>to allocate space for the pathnames of each file. You have your pointer
point
>to the local array declared in your function, which will be invalid once
you
>leave the function. Thus a working version would look like:

>else {
>  ptr->path = strcpy (calloc (sizeof (char), strlen (path_name) + 1),
>path_name);
>  ptr->size = statbuf.st_size;
>  ptr->next = NULL;
>}

>This explicitely allocates enough dynamic memory to fill in the string
(plus
>the terminating \0) and then copies the string into it.

>Same applies if you wanted to free the list. You'd have to free those
pathname
>strings first and then the list item itself.

>Cheers!


 
 
 

Problem with my linked list - probably pointers

Post by Paul » Thu, 14 Oct 1999 04:00:00


It worked. I got a warning about pointer types and casting but it still
worked. I then figured out that I need to cast the calloc return as a
pointer to char and the warning was gone.

Thanks again for the help.


>Thanks for the help. That should get me out of trouble.



>>> I'm getting file statistics (just name and size for now) and sticking
>them
>>> in a linked list. The linked list is ordered in descending size and
based
>on
>>> a program from a text book explaining linked lists and how to insert new
>>> records into them.

>>> The file path prints out garbage but the sizes look OK and in order. I
>think
>>> I've screwed up a string or a pointer. The individual records seem to
get
>>> created OK as I've put test points all over the place and got the expect
>>> results. Can anyone see what's wrong with the list.

>>There's nothing wrong with the list itself. The problem is that you forget
>>to allocate space for the pathnames of each file. You have your pointer
>point
>>to the local array declared in your function, which will be invalid once
>you
>>leave the function. Thus a working version would look like:

>>else {
>>  ptr->path = strcpy (calloc (sizeof (char), strlen (path_name) + 1),
>>path_name);
>>  ptr->size = statbuf.st_size;
>>  ptr->next = NULL;
>>}

>>This explicitely allocates enough dynamic memory to fill in the string
>(plus
>>the terminating \0) and then copies the string into it.

>>Same applies if you wanted to free the list. You'd have to free those
>pathname
>>strings first and then the list item itself.

>>Cheers!

 
 
 

1. Linked list problem

  I am working on a program which has a series of linked lists in it.  It turns
out that when a certain statement is reached (depends on how I organize a
code: sometimes an fprintf statement (fprec,"%ld ",n) -- all types are indeed
correct (I checked), sometimes assignment statements, sometimes others --
one or more of the data fields in the cells change from numbers like 2 to
numbers like 4234563245.  The other field (the next field pointing to the next
cell) often does not change, leaving a list which looks like 1 234542352 3 4.
In instances where the error occurs after the fprintf, the fprintf works
normally and saves the file to disk correctly.

  The position(s) of the culprit(s) remains the same from graph to graph,
but the number changes.  This is giving me the impression that it's some kind
of address.  However, addresses are never passed (at least explicitly) to the
procedure, and at any rate that does not explain screwing up in an fprintf.

  The only thing I learned is that in some cases, the error occurs in a
statement where the computer "cannot access that memory".  However, I am
not using lots of memory, so there is no way it could have run out.  Besides,
I check to see if a malloc returns 0 before I do anything.  In this case, the
2 changes to a 34534534 in the middle of a MALLOC statement, one which has
nothing to do with the linked lists (in a completely different structure, in
fact).

                                Any ideas?  Thanks in advance,

                                                ACG  

2. Gateway solo 9100 and DVD II (can use as CDrom?)

3. linked list problem

4. RedHat vs. Suse

5. Netscape Problem (user problem, more probably)

6. Linux port of Crisp 2.2e: Keyboard mapping?

7. Shared memory and pointer' list

8. PPP - timeouts

9. pointer to latest XConfig monitor list.

10. Pointers to a list of Graphics Cards for X386

11. AIX 4.2 ppp link to NT, and pointers ??

12. 2.4.5-ac9 console NULL pointer pointer dereference

13. allocating memory for pointer to pointers