convert C++ vector into C

This is a discussion on convert C++ vector into C within the C Programming forums, part of the General Programming Boards category; first of all sorry for not posting properly since i am first time user. I just want to convert the ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    7

    convert C++ vector into C

    first of all sorry for not posting properly since i am first time user. I just want to convert the below code into C from C++ vector. I tried to use structure and calloc or malloc but didn't work.

    ************************************************** **************
    Code:
    unsigned int current_context = sim_cycle % cores[core_num].context_ids.size();
     int disp_context_id = cores[core_num].context_ids[current_context]; /* the ID of the context currently being examined */
     std::vector<int> contexts_left(cores[core_num].context_ids);
     
    disp_context_id = contexts_left[current_context];
      if(contexts[disp_context_id].ROB_num == 0)
      {
       //if there are no more instructions waiting dispatch for this thread, try another thread
       contexts_left.erase(contexts_left.begin()+current_context);
       if(contexts_left.empty())
        break;
       current_context%=contexts_left.size();
       continue;
      }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Why do you need a C-style array? Unless you're tied to a C interface, you can probably do just about anything you would want to do with a C++ vector directly.

    But if you insist . . . you could use something like this.
    Code:
    /** Converts a C++ vector into a dynamically allocated array.
    
        It is the caller's responsibility to free the returned array with delete[].
    */
    int *vector_to_array(const std::vector<int> &v) {
        int *a = new int[v.size()];
        for(std::vector<int>::size_type x = 0; x < v.size(); x ++) {
            a[x] = v[x];
        }
        return a;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    C++ forum that way... -->


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

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    ;-)
    Code:
    template<typename T>
    T *vector_to_array(const std::vector<T> &v)
    {
        T *arr = new T[v.size()];
        std::copy(v.begin(), v.end(), arr);
        return arr;
    }
    (Note that if you ever use exceptions, neither of these snippets are exception-safe.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    7

    C++ vector to C

    oh I think I didn't explain it right. I need to use it in C program and not convert just C++ vector to array. I need C code as I am using C copiler (GCC) and this structure will be the part of my C program. so I tried to use structure like this.
    Code:
    struct contexts_left{
    int disp_context_id;
    };
    
    if(contexts[disp_context_id].ROB_num == 0)
      {
       //if there are no more instructions waiting dispatch for this thread, try another thread
       contexts_left.erase(contexts_left.begin()+current_context);
       if(contexts_left.empty())
        break;
       current_context%=contexts_left.size();
       continue;
      }
    but the above way of declaring array "contexts_left" was still giving error that contexts_left is undeclared. Also I tried calloc and malloc function to allocate memory like this,
    Code:
    current_context = (struct contexts_left *)malloc(sizeof(struct contexts_left))
    but still can't figure out what is it that I am missing.
    It might be really easy to ask but I have just started to use C programing and not so much familier with it.

    Thank You for your response.

  6. #6
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    so you want to simulate a vector in C?
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    7
    not the vector but correct C code that is equivalent to vector.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by meerajpatel View Post
    not the vector but correct C code that is equivalent to vector.
    What, you mean:
    Code:
    /* initialize */
    myvector = malloc(how_many * sizeof(*myvector));
    
    /* grow */
    temp = realloc(now_how_many * sizeof(*myvector));
    if (temp == NULL) {
        printf("hack cough die\n");
    } else {
        myvector = temp;
    }
    sort of thing? (I mean, obviously that's probably not everything you would want, but.)

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    7
    like I have this code here
    Code:
    unsigned int current_context = sim_cycle % cores[core_num].context_ids.size();
     int disp_context_id = cores[core_num].context_ids[current_context];  std::vector<int> contexts_left(cores[core_num].context_ids);
     
    disp_context_id = contexts_left[current_context];
      if(contexts[disp_context_id].ROB_num == 0)
      {
       //if there are no more instructions waiting dispatch for this thread, try another thread
       contexts_left.erase(contexts_left.begin()+current_context);
       if(contexts_left.empty())
        break;
       current_context%=contexts_left.size();
       continue;
      }
    In this code, contexts_left is the array and it has list of contexts (disp_context_id). current_context is the index that points to any particular disp_context_id. current_context is initialized with next context every cycle. and no of contexts in array( that is size of array) is the number that we can specify at a time of simulation.

    now the if statement is just an example of condition that if that condition is true then we need to go to next context so it erases that particular context and next context will take place of it. so basically all context next to the erased one will move up by one location. current_context will be same during this time.

    so to use this type of structure in C program, I tried to use structure as I mentioned previously, but didn't work.

    I really appreciate help.

    Thank You.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by meerajpatel View Post
    like I have this code here
    Code:
    unsigned int current_context = sim_cycle % cores[core_num].context_ids.size();
     int disp_context_id = cores[core_num].context_ids[current_context];  std::vector<int> contexts_left(cores[core_num].context_ids);
     
    disp_context_id = contexts_left[current_context];
      if(contexts[disp_context_id].ROB_num == 0)
      {
       //if there are no more instructions waiting dispatch for this thread, try another thread
       contexts_left.erase(contexts_left.begin()+current_context);
       if(contexts_left.empty())
        break;
       current_context%=contexts_left.size();
       continue;
      }
    In this code, contexts_left is the array and it has list of contexts (disp_context_id). current_context is the index that points to any particular disp_context_id. current_context is initialized with next context every cycle. and no of contexts in array( that is size of array) is the number that we can specify at a time of simulation.

    now the if statement is just an example of condition that if that condition is true then we need to go to next context so it erases that particular context and next context will take place of it. so basically all context next to the erased one will move up by one location. current_context will be same during this time.

    so to use this type of structure in C program, I tried to use structure as I mentioned previously, but didn't work.

    I really appreciate help.

    Thank You.
    So you'll have to write an erase function.

  11. #11
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Hello.
    Lets see if we're clear on what you want.
    I think you want an abstract data type which acts like a C++ vector, but you want to use it in C so that you can write things like:

    Vector* v = Vector_new(int elementSize, int initialCapacity);
    or maybe even
    Vector* v = Vector_new(int elementSize, int initialCapacity, copyConstructorFunctionPtr, destructorFunctionPtr);

    and
    Vector_pushBack(Vector* v, void* someData);
    Vector_getElementAt(Vector* v, int index, void** someData)
    Vector_eraseElementAt(Vector* v, int index);

    Sorry, if I'm restating what other posters already have.

    Start by summarizing the operations you want. Decide if your vector needs to hold structure elements or just primitive data types (int, double, pointer).

    Then decide how you want to store the data in the vector. An array is probably a good choice, unless you're doing a lot of deletion from the anywhere except the end. But, if that's the case, you wouldn't have used the C++ vector in the first place.

    Then for each operation, write the code to manipulate the internal data structure. It's not hard, but you need to organize your thoughts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 3
    Last Post: 08-21-2006, 06:42 AM
  3. convert double to string problem
    By gandalf_bar in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2004, 04:14 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 11:53 AM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM

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