C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-07-2009, 10:15 PM   #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;
  }
meerajpatel is offline   Reply With Quote
Old 07-07-2009, 10:50 PM   #2
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,629
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, etc.

New project: nort
dwks is offline   Reply With Quote
Old 07-08-2009, 12:28 AM   #3
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,179
C++ forum that way... -->


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 07-08-2009, 05:02 AM   #4
int x = *((int *) NULL);
 
Cactus_Hugger's Avatar
 
Join Date: Jul 2003
Location: Banks of the River Styx
Posts: 890
;-)
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)
Cactus_Hugger is offline   Reply With Quote
Old 07-08-2009, 05:32 PM   #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.
meerajpatel is offline   Reply With Quote
Old 07-08-2009, 05:46 PM   #6
Banned
 
ಠ_ಠ's Avatar
 
Join Date: Mar 2009
Posts: 533
so you want to simulate a vector in C?
__________________
╔╗╔══╦╗
║║║╔╗║║
║╚╣╚╝║╚╗
╚═╩══╩═╝
ಠ_ಠ is offline   Reply With Quote
Old 07-08-2009, 08:53 PM   #7
Registered User
 
Join Date: Jul 2009
Posts: 7
not the vector but correct C code that is equivalent to vector.
meerajpatel is offline   Reply With Quote
Old 07-08-2009, 09:10 PM   #8
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.)
tabstop is offline   Reply With Quote
Old 07-08-2009, 09:53 PM   #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.
meerajpatel is offline   Reply With Quote
Old 07-08-2009, 10:02 PM   #10
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 07-09-2009, 05:09 AM   #11
pwning noobs
 
Zlatko's Avatar
 
Join Date: Jun 2009
Location: The Great White North
Posts: 125
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.
__________________
Sun Certified Java Programmer / Developer
IEEE CSDP
Zlatko is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Another syntax error caldeira C Programming 31 09-05-2008 01:01 AM
Convert to Roman Numerals - Program not working, suggest a workaround! duffmckagan C Programming 3 08-21-2006 06:42 AM
convert double to string problem gandalf_bar C++ Programming 6 03-15-2004 05:14 AM
Convert Char to Int Function drdroid C++ Programming 9 02-19-2003 12:53 PM
please help ... to convert date to string mel C Programming 1 06-12-2002 10:26 AM


All times are GMT -6. The time now is 01:17 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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