LinuxQuestions.org
Visit Jeremy's Blog.
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-18-2007, 10:21 AM   #1
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Rep: Reputation: 32
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,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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
LQ 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: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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: 207

Original Poster
Rep: Reputation: 32
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



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

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

All times are GMT -5. The time now is 09:26 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