![]() |
|
|
#1 |
|
Bemusing Prompter
"Danny"
Dec 2002
California
2·5·239 Posts |
I recently started working on small improvements for mfakto. I'm a little embarrassed to ask these questions, but it's been quite a while since I worked with C/C++.
1. How can I do a foreach loop through an array of C strings in C++? I'm defining strings using char pointers (to keep consistency with mfakto) and arrays of strings like this: Code:
char *str_arr[] = {
"blah",
"blah blah",
...
}
2. I also want to get the number of elements in an array of strings. However, sizeof() always returns 8 as I'm defining strings using char pointers. sizeof(str_arr)/sizeof(str_arr[0]) doesn't work either and always returns 1. Is there another solution? 3. This is more of a question about style: I have an array of strings (mainly GPU names) that only need to be accessed once. Is it OK to move them to the header file? Thanks! Last fiddled with by ixfd64 on 2019-06-26 at 22:51 |
|
|
|
|
|
#2 | |
|
"Mark"
Apr 2003
Between here and the
18CB16 Posts |
Quote:
Code:
vector<string>::iterator it = myVector.begin();
string element;
while (it != myVector.end())
{
element = *it;
. . . do something with element . . .
it++;
}
You could also use: Code:
char *str_arr[MY_ARRAY_SIZE] = {
"blah",
"blah blah",
...
}
Question, why do you need an array of GPU names? Assuming that you are writing some logic in your C/C++ to determine which GPU code to execute, there must be some APIs where you can ask the GPU what features it supports. BTW, is your source open? Is it possible that you could build your code upon the mtsieve framework (which supports OpenCL workers)? That might save you a lot of work if you are rewriting your C/C++. |
|
|
|
|
|
|
#3 |
|
Dec 2012
The Netherlands
6A616 Posts |
1. At a C level, if you mark the end of the array with a 0 (or NULL) then you could do it like this:
Code:
char *str_arr[ ] = { "blah", "blah blah", ...,0 };
int n;
for (n=0;str_arr[n];++n)
{ ...use str_arr[n]...
}
The basic C level array does not remember its own size. You would have to store it separately (or use a constant value if it doesn't change). 3. If I remember correctly, putting initial values in a variable makes the declaration a definition which therefore reserves storage. You don't want this to be done by every file including the header! Last fiddled with by Nick on 2019-06-27 at 07:13 Reason: Typo |
|
|
|
|
|
#4 | |
|
Aug 2004
2×5×13 Posts |
Quote:
Code:
#include <iostream>
int main(int argc, char* argv[])
{
const char* str_arr[] = { "blah", "blah blah", "a b c d e" };
for (const char* s : str_arr)
{
std::cout << s << std::endl;
}
return 0;
}
|
|
|
|
|
|
|
#5 | ||
|
Bemusing Prompter
"Danny"
Dec 2002
California
2·5·239 Posts |
Quote:
Code:
if (strstr(deviceinfo.d_name, "Capeverde") || // 7730, 7750, 7770, 8760, 8740, R7 250X
strstr(deviceinfo.d_name, "Pitcairn") || // 7850, 7870, 8870
strstr(deviceinfo.d_name, "Bonaire") || // 7790, R7 260, R7 260X
strstr(deviceinfo.d_name, "Oland") || // 8670, 8570, R9 240, R9 250
strstr(deviceinfo.d_name, "Sun") || // 85x0M
strstr(deviceinfo.d_name, "Mars") || // 86x0M, 87x0M
strstr(deviceinfo.d_name, "Venus") || // 88x0M
strstr(deviceinfo.d_name, "Saturn") || // 8930M, 8950M
strstr(deviceinfo.d_name, "Neptune") || // 8970M, 8990M
strstr(deviceinfo.d_name, "Curacao") || // R9 265, R9 270, R9 270X
strstr(deviceinfo.d_name, "Tonga") || // R9 285
strstr(deviceinfo.d_name, "Hainan") || // R9 285
strstr(deviceinfo.d_name, "Kalindi") // GCN APU, Kabini, R7 ???
)
{
mystuff.gpu_type = GPU_GCN;
}
Quote:
Code:
cannot build range expression with array function
parameter 'device_class' since parameter with array type 'char *[]' is
treated as pointer type 'char **'
Code:
const char *TYPE_GCN[] = {
"Capeverde",
"Pitcairn",
"Bonaire",
"Oland",
"Sun",
"Mars",
"Venus",
"Saturn",
"Neptune",
"Curacao",
"Tonga",
"Hainan",
"Kalindi"
};
[...]
/* check if device name contains a string in the array */
int name_contains(char *device_name, char *device_class[]) {
for (const char *device : device_class) {
if (strstr(device_name, device)) {
return 1;
}
}
return 0;
}
Last fiddled with by ixfd64 on 2019-06-27 at 23:40 |
||
|
|
|
|
|
#6 |
|
∂2ω=0
Sep 2002
República de California
103×113 Posts |
As others have noted sizeof() is useless for getting number of array elements. In that sense, a C++ vector<string> is preferable. Strictly in C, you need some kind of end-of-array sentinel. Here an example using an empty string (which will still occupy storage via the resulting string-terminator char), with 2 ways to iterate over the string array:
Code:
#include <stdio.h>
#include <string.h>
#define STRNEQ(s1,s2) ( strcmp(s1,s2))
int main()
{
char *str_arr[] = {"foo","bar",""};
char **curr_str;
for(curr_str = str_arr; STRNEQ(*curr_str,""); curr_str++)
printf("curr_strA = %s\n",*curr_str);
int i;
for(i = 0; STRNEQ(str_arr[i],""); i++)
printf("curr_strB = %s\n",str_arr[i]);
return 0;
}
|
|
|
|
|
|
#7 |
|
"Mark"
Apr 2003
Between here and the
11·577 Posts |
Making a check for specific GPU names is not future-proof.
For OpenCL you can use clGetDeviceInfo() to determine the features available to the GPU. You can then use those features to determine which kernel you want to execute. My knowledge of OpenCL is only strong enough for me to use it with some of the programs using the mtsieve framework. I wouldn't be surprised if your OpenCL skills were much better. |
|
|
|
|
|
#8 | ||
|
Bemusing Prompter
"Danny"
Dec 2002
California
2·5·239 Posts |
Quote:
Quote:
Last fiddled with by ixfd64 on 2019-06-28 at 01:29 |
||
|
|
|
|
|
#9 | |
|
Jan 2008
France
2×52×11 Posts |
Quote:
Code:
#include <stdio.h>
int main()
{
char *str_arr[] = {"foo","bar"};
for (int i = 0; i < sizeof(str_arr) / sizeof(str_arr[0]); i++)
printf("curr_strA = %s\n", str_arr[i]);
return 0;
}
Last fiddled with by ldesnogu on 2019-06-28 at 08:04 |
|
|
|
|
|
|
#10 |
|
"Mark"
Apr 2003
Between here and the
11·577 Posts |
|
|
|
|
|
|
#11 | |
|
Bemusing Prompter
"Danny"
Dec 2002
California
2·5·239 Posts |
Quote:
|
|
|
|
|
![]() |
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Simple factoring challenge + questions | siegert81 | Factoring | 12 | 2016-05-28 18:36 |
| Programming questions | Xyzzy | Programming | 76 | 2015-01-24 04:35 |
| Need some help on php programming | pinhodecarlos | Programming | 2 | 2012-07-23 18:17 |
| New to programming. What to do? | lorgix | Miscellaneous Math | 9 | 2010-12-08 22:22 |
| plz, help me in c programming | alaa | Homework Help | 12 | 2007-06-12 22:17 |