Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 21-10-2009
Member
 
Join Date: Jul 2009
Posts: 122
Addition of a structure

I created a code to identify different objects, each contained in a link. So far, so good, here is the code:

Code:
#include <stdio.h>
typedef struct object {
int a;
int b;
} an_object;
typedef struct link {
an_object * infos;
struct link * next;
} an_link;
an_link *addElement(an_link *the_link, an_object *l_object) {
an_link *new_link = malloc(sizeof(un_link));
new_link->next = NULL;
new_link->infos = malloc(sizeof(an_object));
new_link->infos->a = l_object->a;
new_link->infos->b = l_object->b;
if (the_link== NULL)
 return new_link;
else {
 an_link *link_common = the_link;
 while (link_common->next != NULL)
  link_common= link_common->next;
 link_common->next = new_link;
 return the_link;
}
}
int main(void)
{
an_link*the_link= NULL;
an_object l_object = { 12, 56 };
the_link = addElement(the_link, &l_object);
printf("%d\t%d", the_link->infos->a, the_link->infos->b);
return 0;
}
But it imposes on me to include in my list Code structure further defined as:

Code:
struct list{
an_link * premier_link;
an_link * last_link;
an_link * link_common;
};
I tried to implement the code above this structure and the changes it entails, but curiously, I do not succeed ... Can you show me please?
Reply With Quote
  #2  
Old 21-10-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Addition of a structure

Since you have the possibility to browse your list in one direction, do you really need a pointer to the last link?

How do you do if you want to travel the same list twice in the same time? You'll add a link to your current structure?

This structure is required? That sounds really dubious. The last link pointer can invite you to make a doubly linked list, but the current link, I really doubt that this is a good idea
Reply With Quote
  #3  
Old 21-10-2009
Member
 
Join Date: Jul 2009
Posts: 122
Re: Addition of a structure

Do not I make doubly linked list? Regarding the current link, here's what it makes me want to do:

"Given that we have to traverse the list to change iteratively each object, it is included in the list structure pointer "current" to move through the list and keep the location where we are. "

Is this clearer?
Reply With Quote
  #4  
Old 21-10-2009
Member
 
Join Date: May 2008
Posts: 2,008
Re: Addition of a structure

Each time the function is called, it allocates memory and stores the address allocated to a local variable that is lost in the end function => memory leak ensured

As for your question it is almost the same reason. If you change list you lose your pointer early.

Now, if you really want to make lists correctly, then I advise the following method: you create a structure to hold the list. The structure may very well not hold the pointer to start it does not matter. It may seem silly to create a structure just for a pointer but then, nothing prevents you from adding other elements such as
- The number of members
- Pointer during treatment
- A pointer to the end (to go in the opposite direction)
etc.

If you plan early this kind of case, you will have less work later to change your code. And also it will avoid going through pointers to pointers when you must edit an item
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Addition of a structure"
Thread Thread Starter Forum Replies Last Post
Need some MS addition in pes 2012 Chuck Connors Video Games 7 22-09-2011 03:15 PM
Multiplayer moddability addition in Torchlight 2 Namkar Video Games 4 05-08-2011 10:31 PM
Addition domain with windows 2008 Ryker Networking & Security 7 25-09-2010 07:43 PM
Do Apps run on computer in addition to the iphone Galimberti Portable Devices 5 08-07-2010 03:09 AM
Java Script Addition Problem Magnus Software Development 3 01-06-2009 11:38 AM


All times are GMT +5.5. The time now is 02:50 PM.