LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-01-2016, 01:10 PM   #1
errigour
Member
 
Registered: May 2009
Posts: 366

Rep: Reputation: 6
freeing structure lists


say I have a list of structures:
Code:
struct buffer
{
  char *buffer;
  struct buffer *next;
}
Ok I malloced a bunch of structures that link to each other making sure next is null if there aren't anymore commands in the buffer. Can I free iter and still access what iter->next was pointing to if it was malloced data?


Code:
void free_d_buffer_q(struct buffer *q)
{
    struct buffer *iter;
    
    iter = q;
    
    while(iter !=0)
    {
        if( iter->buffer )
          free(iter->buffer);
        q = iter->next;
        free(iter);
        iter = q;
    }
}
 
Old 07-01-2016, 01:58 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by errigour View Post
Ok I malloced a bunch of structures that link to each other making sure next is null if there aren't anymore commands in the buffer. Can I free iter and still access what iter->next was pointing to if it was malloced data?
Yes. You can access the data as long as it's not freed.
 
Old 07-01-2016, 03:19 PM   #3
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
thanks.
 
Old 07-05-2016, 07:01 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Minor addendum - I suggest setting the structure pointers to NULL after releasing what they point to.

This only helps if the structure gets reallocated with malloc, and just happens to give you a structure with existing pointers that are invalid... (which is one reason I use calloc rather than malloc as a habit).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Freeing allocated memory in a recursive data structure Gullible Jones Programming 1 08-29-2013 03:51 AM
LXer: Python - Sorting Lists inside of Lists LXer Syndicated Linux News 0 06-06-2013 01:00 PM
How are opt-in e-mail lists distinguished from spam lists? Travis86 Programming 2 01-29-2012 08:55 PM
Convert directory structure from long file names in Linux to DOS 8.3 structure? manorina Linux - Software 5 09-12-2009 09:18 AM
LXer: Unique Sorting Of Lists And Lists Of Lists With Perl For Linux Or Unix LXer Syndicated Linux News 0 09-05-2008 01:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration