Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-18-2007, 10:21 AM   #1
Registered: Nov 2002
Posts: 207

Rep: Reputation: 32
C advice requested


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

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

bool updateInfo(struct INFO* pInfo)
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.

Old 07-18-2007, 11:18 AM   #2
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,398
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
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:
bool updateInfo(struct INFO* pInfo)


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:


Last edited by paulsm4; 07-18-2007 at 03:51 PM.
Old 07-18-2007, 05:06 PM   #4
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
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:

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.



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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:37 PM.

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