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 12-26-2006, 08:56 AM   #1
suresh_rupineni
LQ Newbie
 
Registered: Jul 2006
Posts: 11

Rep: Reputation: 0
memory free in c langfuage


hi i am using the linked list like this.how i will free this linked list.
struct ipa__serv_s
{
char *serv_name;
int port;
};
typedef struct ipa__serv_s ipa__serv_t;
struct ipa__data_s
{
ipa__serv_t service;
long bytes;
long packets;
};
typedef struct ipa__data_s ipa__data_t;

struct ipa__data_list_s
{
ipa__data_t *__ptr;
int __size;
};
typedef struct ipa__data_list_s ipa__data_list_t;


struct ipa__obj_serv_s // Individual record in the above list
{
char *src_obj_name;
char *dest_obj_name;
char *src_obj;
char *dest_obj;
ipa__data_list_t data_list;
};
typedef struct ipa__obj_serv_s ipa__obj_serv_t;

struct ipa__rule_acct_s // List of all accounted data rules of the form "SRC_OBJ -- DEST_OBJ -- SERVICE -- BYTES -- PKTS"
{
ipa__obj_serv_t *__ptr;
int __size;
};
typedef struct ipa__rule_acct_s ipa__rule_acct_t;

struct ipa__rule_data_s
{
long long total_bytes;
long long total_packets;
char *timeStamp;
ipa__rule_acct_t rule_data;
};
typedef struct ipa__rule_data_s ipa__rule_data_t;

i was allocated memory for the structure and fill the structure .i want to free the memory allocated for this this structure.if any body having idea please suggest me.
regards
suresh
 
Old 12-26-2006, 09:29 AM   #2
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
I dont see the problem. If it was allocated with malloc, it should be deallocated with free. There is no more magic than those two functions. Did you have any errores or problemas while you were deallocating the list?

By the way, post your code between CODE tags (otherwise your code is kinda unreadable).

Cheers!
 
Old 12-26-2006, 09:36 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Let me start by explaining what (a sample of) this "C" code means:
Code:
struct ipa__data_s {
 ipa__serv_t service;
 long bytes;
 long packets;
};
typedef struct ipa__data_s ipa__data_t;
We see that:
  • A memory structure called ipa__data_s consists of three elements: (1) a pointer to a thing of type ipa__data_t, which is described previously; (2) a long-intger named bytes; and (3) another long-integer named packets.
  • When we declare a variable to be "of type ipa__data_t," we mean that it is an instance of the struct(ure) ipa__data_s.
  • None of these structures contains "a pointer to another instance of itself." Instead, we see a single pointer of the appropriate type, and a long-integer with a name like __size. So, what the designer intends for us to do is to malloc() a single contiguous block of memory that is big enough to contain ("an array of") n instances of this thing, and to remember the number of elements ("n") in __size.
  • This is an experienced designer, who has adopted the nomenclature that structure-names end with _s (for "structure") and each structure is then declared as a typedef ending in _t (for "type"). This designer also observes the convention that, when variable-names begin with one or two underscores, as in __size, she wishes to thereby convey that this element is intended "for internal use only," i.e. for managing this thing, not as a piece of information someone else might wish to know. ("C" does not have objects, with formal visibility rules, methods, constructors and destructors, and so on.)
If you review man malloc, you will see definitions of functions with parameter-lists like these:
Code:
 void *calloc(size_t nmemb, size_t size);
This is designed to allocate a block of memory consisting of nmemb instances of a structure of size size. In the "C" language, this allocates "an array."

I strongly recommend that you check to see if this unit does not already define some routines which are intended to build and dismantle these structures: if not, you should build them yourself and use them throughout. Write one or more "C" units which encapsulate the entire process of correctly building, dismantling, and using these structures, so that the remainder of your code can treat them "opaquely," that is, without concerning itself with structural details: "I want to remember this, so I call this routine to do that. I want to know this answer, so I call this routine to get it. In both cases, I don't care how it works." Thus, if a bug appears, there is one and only one place to look for it.

It might be most useful for you to do some browsing through, say, some of the include-files in /usr/include to see first-hand some examples of the techniques I am talking about.

Last edited by sundialsvcs; 12-26-2006 at 09:38 AM.
 
  


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
Does free( ) immediatly free the memory? Rayven Programming 24 11-06-2006 02:24 AM
Free memory in percentage kinetik Programming 6 03-31-2006 12:16 AM
free physical memory saavik Solaris / OpenSolaris 7 08-26-2005 12:22 PM
How much memory is really 'free'? robbiemorgan Linux - Newbie 4 09-19-2004 07:05 PM
free up unused memory im1crazyassmofo Linux - Hardware 1 01-20-2004 06:39 AM

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

All times are GMT -5. The time now is 11:17 AM.

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