LinuxQuestions.org
Register a domain and help support LQ
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-18-2007, 10:21 AM   #1
freeindy
Member
 
Registered: Nov 2002
Posts: 196

Rep: Reputation: 30
C advice requested


Hi,

I have a simple question, hopefully with a simple answer.

I have a structure:
Code:
struct INFO
{
   int ID;
   struct INFO* pNext;
}
And, I would like to have a function in this manner:

Code:
bool updateInfo(struct INFO* pInfo)
{
 //UPDATE...
}
The question is, should the caller be recommended for allocating the memory OR should the memory be allocated within updateInfo() and all the info should be copied?

Any help is appreciated.

Indy
 
Old 07-18-2007, 11:18 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Use the greatest possible flexibility:
In updateInfo(), if the pointer is non-null, assume it has been allocated, else allocate storage from malloc()/calloc(). Wherever the storage is allocated, assign NULL to the pNext member.
--- rod.
 
Old 07-18-2007, 03:45 PM   #3
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Uh, no.

The way you have it designed, the caller *must* allocate the space; the callee *cannot* allocate it:
Code:
bool updateInfo(struct INFO* pInfo)
{
 //UPDATE...
}

...

void someOtherProc ()
{
  struct INFO *p = (struct INFO *)malloc (sizeof (struct INFO));
  if (p != NULL)
    updateInfo (p);
    ...
Suppose you did the "malloc()" inside updateInfo()? How is the caller going to get the new pointer, the one returned by "malloc()"? Certainly not through the "pInfo" parameter. You can change the *local* value of "pInfo" in "updateInfo()" ... but you cannot return the new value back to the caller.

Two alternatives might be:
a) make pInfo a pointer to a pointer ("struct INFO **ppInfo")
... or ...
b) make "udpateInfo()" return the pointer (instead of a "bool").

If your function simply "updates existing data", then like your original design better. Just allocate the struct before you call "updateInfo()", and Life is Good.

Otherwise, if your function is actually "add new member to list", then I'd allocate inside the function ... and pass a pointer to a pointer. Here's an example:

http://vergil.chemistry.gatech.edu/r...ial/lists.html


IMHO .. PSM

Last edited by paulsm4; 07-18-2007 at 03:51 PM.
 
Old 07-18-2007, 05:06 PM   #4
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,483

Rep: Reputation: 66
From what I can tell he is just updating the info in an existing list element in that case you could just modify the info directly. But then you ask about allocating memory and that is confusing as you wouldn't need to.
 
Old 07-19-2007, 05:19 AM   #5
freeindy
Member
 
Registered: Nov 2002
Posts: 196

Original Poster
Rep: Reputation: 30
Thanks lads,

actually, paulsm4, i am allocating a new structure inside the updateInfo. So basically, I'm adding a structure inside a structure like:

Code:
struct DATA
{
   ...
   struct INFO* info;
   ...
   struct DATA* next;
   struct DATA* prev;
}
So exvor, thanks. It makes sense. I wanted to be sure. The caller should allocate.

Indy
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
FC6 Noobie, advice requested for yum & grub herbietheferret Fedora 2 02-27-2007 03:33 PM
Advice requested for Linux installation (more interesting than it sounds!) Napalm Llama Linux - General 20 06-29-2006 06:49 PM
Presentation on Linux - advice requested pattyz Linux - General 2 03-16-2006 08:38 AM
Partition advice requested bamatmac Linux - Newbie 1 12-30-2005 01:28 PM
LAMP advice requested thorn168 *BSD 2 04-19-2005 12:04 AM


All times are GMT -5. The time now is 03:59 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration