From Python's Linked Lists to Dynamic Arrays

This is a discussion on From Python's Linked Lists to Dynamic Arrays within the C Programming forums, part of the General Programming Boards category; Hi there, I'm doing some conversion of python to C code. I'm having quite a bit of problems converting what ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    6

    From Python's Linked Lists to Dynamic Arrays

    Hi there,

    I'm doing some conversion of python to C code. I'm having quite a bit of problems converting what used to be linked lists in to C arrays.

    In the python code, the programmer loves to declare an empty list, appending items while he iterates through some set of tasks, and return the list as a function's output. This causes me some trouble..

    It's a bit of a pain to determine the size of the list before having to work with it, but the real trouble I'm having is managing the storage of these sizes. Should my function return the size of the array as well as a pointer to the array itself? Should I declare new structs which contain the array as well as the array's size?

    Any advice is appreciated; I tend to think object oriented-ly, so this is all new to me

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So why are you converting to arrays at all? Use a linked list.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    Quote Originally Posted by quzah
    So why are you converting to arrays at all?
    Because an array is faster than a linked list, basically.

    The C code is eventually going to be parallelized and run on a beowulf cluster. I know that certain MPI functions require contiguous arrays for passing between processes, so using linked lists makes me nervous.. I may end up using them, though, just to save myself some sanity.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Should my function return the size of the array as well as a pointer to the array itself?
    Well, since a C function can only return one value you're not really even left with this option. One thing you might look at is doing it something like this:
    Code:
    int fill_array(int **array)
    {
      int num = 7; // Some arbitrary number that's unknown to the calling function
    
      if(!(*array = malloc(sizeof(int) * num)))
        return -1;
    
      // Fill the array with whatever
      (*array)[0] = 1;
      (*array)[1] = 9;
      ...
      (*array)[6] = 4;
    
      return num;
    }
    
    int main(void)
    {
      int *array = NULL;
      int n_array;
    
      if((n_array = fill_array(&array)) == -1)
      {
        puts("There was a problem!");
        return 1;
      }
    
      // Now the array is filled with data and you know how big it is
    
      free(array);
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists
    By xddxogm3 in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2003, 03:14 PM
  2. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 03:36 PM
  3. linked lists / arrays
    By akira in forum C++ Programming
    Replies: 0
    Last Post: 11-27-2001, 12:01 AM
  4. arrays, linear linked lists and binary trees
    By jasingle in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2001, 10:12 AM
  5. Linked lists
    By sballew in forum C Programming
    Replies: 6
    Last Post: 10-24-2001, 08:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21