473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Problem with string to variable name conversions

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
10 2793
sicarie
4,677 Expert Mod 4TB
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
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
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
9,065 Expert Mod 8TB
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
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
4,677 Expert Mod 4TB
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
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
3,652 Expert 2GB
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
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
9,065 Expert Mod 8TB
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

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

Similar topics

0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
2
by: grubbymaster | last post by:
Hello The problem i have is this : i want to use the following structure in a DLL that i created: typedef struct _NDIS_802_11_SSID { ULONG SsidLength; UCHAR Ssid ; } NDIS_802_11_SSID,...
4
by: skavan | last post by:
Use Case: We have music files that describe, in their filename, attributes of the music. We do not know a general pattern that applies to all filenames -- but we do know that filenames that are...
18
by: Kyro | last post by:
New to C# (migrating from Delphi) and I'm not sure how to get a delimited string into an array of string. input: string looks like - 01-85-78-15-Q11 output: an array with elements - ...
3
by: bob | last post by:
Hi, I'm looking at a legacy string class thats been in use here for a while and I'd like to check out any options available to optimise it. I see a couple of constructors that look dubious....
4
by: Jean-François Michaud | last post by:
Hello, I've been looking at this for a bit now and I don't see what's wrong with the code. Can anybody see a problem with this? Here is an XSLT snippet I use. <xsl:template match="graphic">...
1
by: ahobart | last post by:
Hello, I have this code in C# and it works perfect: protected string GetUniqueName(string filename) { string temp = filename; for (int x=1; System.IO.File.Exists(temp); x++){ temp =...
11
by: =?ISO-8859-1?Q?Jean=2DFran=E7ois_Michaud?= | last post by:
Context: I'm trying to compare XML tree fragments and I'm doing so by outputting the attributes of each element in the tree and outputting it to a string then normalizing the strings. Then I'm...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.