By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

Problem with string to variable name conversions

P: 23
Is there anyway to assign a value of type string to be the name of an integer, or convert the value or vise versa?
Example:
int Q = 1;
string member = "member" + Q;
//some kind of type name or conversion takes place?
int member1 = Q;

Anybody have some ideas on how to do this? I tried the streamstring technique that I got off of another thread, but that does not work with changing values, only names of variable types.:(
Mar 8 '07 #1
Share this Question
Share on Google+
10 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Is there anyway to assign a value of type string to be the name of an integer, or convert the value or vise versa?
Example:
int Q = 1;
string member = "member" + Q;
//some kind of type name or conversion takes place?
int member1 = Q;

Anybody have some ideas on how to do this? I tried the streamstring technique that I got off of another thread, but that does not work with changing values, only names of variable types.:(
Do you want to do some sort of dynamic variable naming? (Am I reading that correctly?)

I would recommend using a data structure instead - you can add/remove/manipulate from those only by holding place - you don't necessarily have to name every element...
Mar 8 '07 #2

P: 23
Do you want to do some sort of dynamic variable naming? (Am I reading that correctly?)

I would recommend using a data structure instead - you can add/remove/manipulate from those only by holding place - you don't necessarily have to name every element...
Never heard of it. Could you give me an example of the syntax? I'm pretty new to this.
Mar 8 '07 #3

P: 23
Do you want to do some sort of dynamic variable naming? (Am I reading that correctly?)

I would recommend using a data structure instead - you can add/remove/manipulate from those only by holding place - you don't necessarily have to name every element...
One more thing, I'm not trying to name every element. I'm trying to make a program that finds every prime number until my comps RAM can't handle it anymore. To do such a thing, I need to add the numbers I find to an STL container of some kind, then iterate through it with each one in an attempt to factor each number I come across to check if it's prime by dividing it with previously found numbers. Problem is, I can't do this without a segmentation error because I can't make a new variable name everytime automatically.

example:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. #include <limits>
  4. using namespace std;
  5. int main()
  6. {   unsigned long long int prime = 1;
  7.     vector<unsigned long long int> prime_vec;
  8.     for(i = 0; i != ULONG_LONG_MAX; i++)
  9.     {
  10.         prime_vec.push_back(prime); //ILLIGAL!!!
  11.     }
  12.     return 0;
  13. }
  14.  
It's trying to access the same memory address over and over again that's already occupied. So I need a way to generate new variable names. Namely the way I described above (no pun intended). I just need someone to show me how to do it.
Mar 8 '07 #4

Banfa
Expert Mod 5K+
P: 8,916
The line you have marked as ILLEGAL is not illegal.

However your code is trying to push 18446744073709551615 unsigned long longs into the vector. A unsigned long long is 8 bytes so ignoring overhead of the vector the amount of memory required to store the data is

18446744073709551615 * 8 = 134,217,728 Tera Bytes

Not too surprisingly your computer does not have this much memory so at some point the memory allocation fails and an exception is thrown. Since you code does not catch the exception the system does and terminates your program. You need to catch the exception (or have some other way of working out when you computers memory is full).

The solution to your problem is to use an STL container (although I would recomend for such large quantities of data that you do not use a vector because that keeps it's memory in 1 contiguous block and every time you push_back it has to copy the entire data block to a new buffer) but to correctly handle and exceptions that may occur from running the computer at the limits of its memory.
Mar 9 '07 #5

P: 23
The line you have marked as ILLEGAL is not illegal.

However your code is trying to push 18446744073709551615 unsigned long longs into the vector. A unsigned long long is 8 bytes so ignoring overhead of the vector the amount of memory required to store the data is

18446744073709551615 * 8 = 134,217,728 Tera Bytes

Not too surprisingly your computer does not have this much memory so at some point the memory allocation fails and an exception is thrown. Since you code does not catch the exception the system does and terminates your program. You need to catch the exception (or have some other way of working out when you computers memory is full).

The solution to your problem is to use an STL container (although I would recomend for such large quantities of data that you do not use a vector because that keeps it's memory in 1 contiguous block and every time you push_back it has to copy the entire data block to a new buffer) but to correctly handle and exceptions that may occur from running the computer at the limits of its memory.
I thought a vector was an STL container. What if I just calculated the amount my computer can hold and just limit it to a few megs under that? Would that work?
Mar 9 '07 #6

sicarie
Expert Mod 2.5K+
P: 4,677
My apologies - I was using 'data structure' to refer to an STL container.

Banfa is referring to the properties of a vector - it is an STL container, however, it also has the attributes he mentioned. This is Wikipedia's article on the STL. I personally would recommend a Linked List - I don't believe it needs to be stored in contiguous memory (though there may be another performance downside to it that you might not want). But you can pick the one you prefer out of those.

And I'm not sure how you would calculate the size of the stack/heap your computer is using, so I'm not sure how you would calculate up to that point (except for doing something over it, and printing each one out - though someone else here might...).
Mar 9 '07 #7

P: 23
My apologies - I was using 'data structure' to refer to an STL container.

Banfa is referring to the properties of a vector - it is an STL container, however, it also has the attributes he mentioned. This is Wikipedia's article on the STL. I personally would recommend a Linked List - I don't believe it needs to be stored in contiguous memory (though there may be another performance downside to it that you might not want). But you can pick the one you prefer out of those.

And I'm not sure how you would calculate the size of the stack/heap your computer is using, so I'm not sure how you would calculate up to that point (except for doing something over it, and printing each one out - though someone else here might...).
This is the code for my program, designed to find every prime number up to the maximum intager limit predefined by C++. If you see anything wrong with it, please let me know:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <limits>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     vector<unsigned long long int> prime_nums;
  9.     unsigned long long int A = 2;
  10.     unsigned long long int B = 3;
  11.     prime_nums.push_back(A);
  12.     prime_nums.push_back(B);
  13.     for(unsigned long long int prime = 2; prime != ULONG_LONG_MAX; prime++)
  14.     {
  15.                  for(unsigned long long int i = 0; i != prime_nums.size; i++;
  16.                  {
  17.                               if(((prime % prime_nums[i]) != 0) && (prime != (2 || 3))
  18.                               {
  19.                               prime_nums.push_back(prime);
  20.                               cout << "Latest find: " << prime << ". Total found: " << prime_nums.size() << endl;
  21.                               }
  22.                               if(prime = (2 || 3))
  23.                               {
  24.                               cout "Latest find: " << prime << ". Total found: " << prime_nums.size() << endl;
  25.                               }
  26.                  }
  27.     }
  28.     return 0;
  29.     }
Mar 9 '07 #8

Ganon11
Expert 2.5K+
P: 3,652
Expand|Select|Wrap|Line Numbers
  1. if(((prime % prime_nums[i]) != 0) && (prime != (2 || 3))
  2. {
  3.    prime_nums.push_back(prime);
  4.    cout << "Latest find: " << prime << ". Total found: " << prime_nums.size() << endl;
  5.    }
  6. if(prime = (2 || 3))
  7. {
  8.    cout "Latest find: " << prime << ". Total found: " << prime_nums.size() << endl;
  9. }
These two if...statements will give you trouble. In the first, you check prime!=(2 || 3) to try and check if prime == 2 or 3. However, this portion is evaluated as follows:

(2 || 3) evaluates first. C++ considers any nonzero integer to be true, so the 2 indicates this portion is TRUE.

prime!=TRUE

At this point, any integer value other than 0 will make this portion false, meaning the entire if... condition is false. If it is 0, then this condition is true.

Obviously, this will not give you the results you want. You need to check each number twice (a.k.a. does prime == 2 or does prime == 3?)

In the second statement, you are using = to check if prime equals 2 or 3, but you should use ==. == is the comparison boolean operator, while = is the assignment operator. Also, you make the same mistake as in the first if... condition with (2 || 3).
Mar 9 '07 #9

P: 23
Okay, I've got the updated code. For some reason, it won't iterate to the second number though, only the first on. As a result, it will display numbers that arn't prime like 9 and 21. Can anyone tell me what's keeping it from iterating through the numbers? Here is the code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <limits>
  3. #include <list>
  4. #include <algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     list<unsigned long long int> prime_nums;
  9.     list<unsigned long long int>::iterator iter;
  10.     unsigned long long int A = 2;
  11.     unsigned long long int B = 3;
  12.     prime_nums.push_back(A);
  13.     prime_nums.push_back(B);
  14.     for(unsigned long long int prime = 1; prime != ULONG_LONG_MAX; ++prime)
  15.     {
  16.                  for(iter = prime_nums.begin(); iter != prime_nums.end(); ++iter)
  17.                  {
  18.                           if(((prime % *iter) != 0) && (prime >= 2))
  19.                  {           prime_nums.push_back(prime);
  20.                              cout << prime << prime_nums.size() << endl;
  21.                              cin.get();
  22.                  }
  23.                  else;
  24.                  {break;
  25.                  }
  26.     }                   
  27.  
  28.     }
  29.     return 0;
  30.     }
Mar 9 '07 #10

Banfa
Expert Mod 5K+
P: 8,916
Also your algorithm does not find prime numbers. In the code line
Expand|Select|Wrap|Line Numbers
  1. if(((prime % prime_nums[i]) != 0) && (prime != (2 || 3))
  2.  
ignoring the prime != (2 || 3) error Ganon has already pointed out. prime % prime_nums[i]) != 0 does not test for prime numbers.

Take the case of testing 15 to see if it is prime, prime_nums[0] == 2 so on the first iteration this statement checks (15 % 2 != 0) this statement is true because 15 % 2 == 1 and that is not equal to 0 so 15 will be added to you list of prime numbers but 15 is not prime 15 = 3 * 5.

However worst than that having check 2 (and assuming that it had the list of prime numbers correct up to this point which is a false assumption) it will no on to check all the primes found so far, in this case 3, 5, 7 11 and 13. The result of 15 % these numbers is

15 % 3 = 0
15 % 5 = 0
15 % 7 = 1
15 % 11 = 4
15 % 13 = 2

It checks all of these and in all the cases of 15 % found_prime != 0 it adds 15 to the list, so not only is 15 not a prime number but it will be added to the list of prime numbers 4 times.

You have to complete the entire loop before taking the decision on the primeness of a number, you must check the number under test against all primes found so far (except see below) and only if they all return non-zero results is it prime if a single one of the results is 0 it is not prime.

Additionally you do not actually have to test all primes, you only have to test primes that at <= sqrt(value_under_test), once you have tested all primes then all primes between sqrt(value_under_test) < prime < value_under_test would be the result from a calculation already carried out during the first part of the test.

In you outer loop you start at 2, however you do not need to start at 2 because you already push 2 and 3 onto the vector of primes as known primes. So you can start at 4, however 4 is know not prime as in fact are all even numbers (except 2 but we already put that on the list) so you can start at 5 and instead of doing prime++ in the loop do prime += 2 automatically skipping all the even numbers and reducing the calculations required by 50%. Further than that since you are only checking odd numbers you do not have to check to see if they are divisible by 2 because no odd number is divisible by 2.

Finally you have still not put in any protection against the memory exception that will happen in the STL container when you run out of memory. vector is a bad choice because of its requirement to keep the memory in a contiguous block which means that when you add an entry it has to allocate a new block of vector_size + 1 and copy the data from the old block insert the new enty and release the old block. This means that at the point of copying the process is using twice as much heap as it actually requires to hold the list of primes causing it to stop much earlier. A list does not hold a contiguous block of memory, each entry is individually allocated so it works faster and doesn't stop as soon due to lack of memory as li never uses twice as much heap as required.

And double finally when you have got you algorithm working correctly do not except your program to run to completion. This algorithm works in exponential time, that is the time take to run is proportional to pow(e, largest_test_value). It will a very long time to run to completion. For this reason I also suggest that until you have a working algorithm you reduce the limit in the outer loop to something more sensible (say 1000).
Mar 9 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.