![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 7
| convert C++ vector into C ************************************************** ************** 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 | |
| | #2 |
| Frequently Quite Prolix 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 | |
| | #3 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,258
| C++ forum that way... --> Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #4 |
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 891
| ;-) 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;
}
__________________ 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 | |
| | #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;
}
Code: current_context = (struct contexts_left *)malloc(sizeof(struct contexts_left)) 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 | |
| | #6 |
| Banned Join Date: Mar 2009
Posts: 533
| so you want to simulate a vector in C?
__________________ ╔╗╔══╦╗ ║║║╔╗║║ ║╚╣╚╝║╚╗ ╚═╩══╩═╝ |
| ಠ_ಠ is offline | |
| | #7 |
| Registered User Join Date: Jul 2009
Posts: 7
| not the vector but correct C code that is equivalent to vector. |
| meerajpatel is offline | |
| | #8 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
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;
}
| |
| tabstop is offline | |
| | #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;
}
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 | |
| | #10 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
| |
| tabstop is offline | |
| | #11 |
| pwning noobs 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |