Results 1 to 4 of 4

Thread: Addition of a structure

  1. #1
    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?

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    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

  3. #3
    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?

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    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

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,878,577.73094 seconds with 16 queries