#include <iostream>
usingnamespace std;
longint *generate_random_vector();
int main()
{
longint *p;
longint *another_p;
longint *array = newlongint [n];//n will be obtained after reading
//from a file
p = generate_random_vector();//generate_random_vector gives n
//numbers of values to *p
for (int i = 0; i < n; i++)
{
array[i] = p[i];
another_p[i] = array[i];
}
return 0;
}
Is the code correct? Or am I using the pointers wrongly? I'm asking because my code didn't have SIGTRAP problem until I added these, so I was hoping someone could point out where am I doing wrongly.
There's a lot of parts in my code that uses the p, so I have to change a lot of variables into vector which I tried doing it earlier just now, but couldn't get the file to compile, so I was forced to use this method instead of vectors.
longint *another_p = newlongint [n];
So p doesn't need it?
I mentioned in line 11 and 12 that n can be obtained from reading a file, I've not given the whole code because generate_random_vector needs to call another function, and that function needs to call another function and so on, so the code I posted is indeed not runnable, but in my opinion sums up the things required to answer my question, also there will be other function calls in between the code I've posted, which will change the value of p, and that's why I want to store the value in another variable.
I have to change a lot of variables into vector which I tried doing it earlier just now, but couldn't get the file to compile, so I was forced to use this method instead of vectors.
How about: solve the problem with vectors first.
How about:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <random>
#include <vector>
int main()
{
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution( 1, 6 );
size_t n {};
std::cin >> n;
std::vector<int> p( n );
for ( int& x : p ) {
x = distribution(generator);
}
std::vector<int> array = p;
for ( int x : array ) std::cout << x << '\n';
}
longint *generate_random_vector(longint n)
{
/*
FUNCTION: generates a random vector
INPUT: vector dimension
OUTPUT: returns pointer to vector, free memory when vector is not needed anymore
(SIDE)EFFECTS: none
*/
longint i, j, help;
longint *v;
srand(time(0));
longint seed=rand();
v = newlongint[ n ];
for ( i = 0 ; i < n; i++ )
{
v[i] = i;
}
for ( i = 0 ; i < n-1 ; i++)
{
j = (longint) ( ran01( &seed ) * (n - i));
assert( i + j < n );
help = v[i];
v[i] = v[i+j];
v[i+j] = help;
}
return v;
}
The line v = newlongint [n]; means allocating a dynamic memory right? So I shouldn't do it again in the main function and I should be going to vectors instead, is that right? As I replied salem c, I've tried using vectors but couldn't get the file working, should I post the code I tried with vectors here?
I've tried using vectors, but not the way you show (which I couldn't understand) because I've actually only took course of basic programming language which only covers up to arrays (pointers, vectors not included), so I can't really understand your code and for that I apologise.
I've never used vectors before but this was my attempt earlier: https://pastebin.com/d8FQPhEJ
Please kindly click the link to have a look, I got it to compile but it cannot complete the code, it stopped after finish reading from the file so I switched to using the code I posted in the question.
I don't know what the ran01() is but I guess that it returns a random float from 0.0 .. 1.0
It is used in a loop that looks like shuffe. Overall, there are values {0, 1, 2, .. n-1} and their order is random.
#include <iostream>
#include <vector> // std::vector
#include <numeric> // std::iota
#include <algorithm> // std::shuffle
#include <random> // std::default_random_engine
#include <chrono> // std::chrono::system_clock
std::vector<longint> generate_random_vector( longint n ) {
std::vector<longint> v( n ); // array with n elements
std::iota( v.begin(), v.end(), 0 ); // like the v[i] = i; loop
// obtain a time-based seed and randomize order of values:
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::shuffle( v.begin(), v.end(), std::default_random_engine(seed) );
return v;
}
int main() {
std::vector<longint> p = generate_random_vector( 13 );
for ( longint x : p ) {
std::cout << ' ' << x;
}
std::cout << '\n';
}
Vector behaves like array (except when it behaves better).
1 2 3 4 5 6 7 8
longint* v = newlongint[ n ];
// use v
delete [] v;
// VS
std::vector<longint> v( n );
// use v
// vector handles the deletion
The "use v" is usually identical between the two.
The for ( int x : array ) std::cout << x << '\n'; is just a for loop with range-syntax that was added by C++11. It has been important part of "basic programming language" for 9 years now.
Sorry they were the same functions, I forgot to remove the argument, since I had n as a global variable.
There are so many functions for vector and they prove to be much shorter than the code I have! Thanks for the code, however I have a question regarding this line unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();, since I have usingnamespace std;, will it become chrono::system_clock::now().time_since_epoch().count();? Or something even simpler?
Wow looks like my course I took in my university is much more basic than I thought, I was never taught about range-syntax.
Wow looks like my course I took in my university is much more basic than I thought, I was never taught about range-syntax.
Schools tend to lag behind by a distressing amount. They stick to old material and books and professors who have not kept up with changes. This makes sense when its the bleeding edge (like c++ 2020 changes, don't expect to see them in school before you graduate) but 2017 and 2011 changes are really a bit much to still be making excuses for not teaching. Ranged for loops are 2011 changes... 9 years... Use them, make your prof see them and acknowledge them at least.
Well I took your pastebin code and put the dynamic allocation back in.
Then compiled and saw this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
$ g++ -Wall -g foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:39:14: warning: variable ‘initial_eval’ set but not used [-Wunused-but-set-variable]
longint initial_eval;
^
foo.cpp:47:14: warning: unused variable ‘best_eval’ [-Wunused-variable]
longint best_eval;
^
foo.cpp:48:14: warning: unused variable ‘improvement’ [-Wunused-variable]
longint improvement;
^
foo.cpp: In function ‘void first_2_opt_symmetric()’:
foo.cpp:327:14: warning: variable ‘improve_item’ set but not used [-Wunused-but-set-variable]
longint improve_item = 0;
^
foo.cpp: In function ‘void perturbation(longint)’:
foo.cpp:401:21: warning: ‘hold_value’ may be used uninitialized in this function [-Wmaybe-uninitialized]
hold_value[i] = p[i]; //initialize hold_value with the same value from local search/*
^
The only disaster waiting to happen here is your uninitialised pointer.
That fixed, it runs fine.
<<< snipped >>>
current best eval = 590
620
current best eval = 590
Vector after local search = 9 1 8 2 5 0 10 6 4 3 11 7
current best eval = 590
Vector p = 9 1 8 2 5 0 10 6 4 3 11 7
iteration 4th = 10000
current best eval = 590
current best eval = 590
ILS best vector = 9 1 8 2 5 0 10 6 4 3 11 7
It does however leak memory like a sieve, but that's easy enough to fix by finding a few places to put in appropriate delete[] calls. ==8822==
==8822== HEAP SUMMARY:
==8822== in use at exit: 1,035,392 bytes in 10,029 blocks
==8822== total heap usage: 20,032 allocs, 10,003 frees, 2,005,160 bytes allocated
==8822==
==8822== LEAK SUMMARY:
==8822== definitely lost: 960,096 bytes in 10,001 blocks
==8822== indirectly lost: 0 bytes in 0 blocks
==8822== possibly lost: 0 bytes in 0 blocks
==8822== still reachable: 75,296 bytes in 28 blocks
==8822== suppressed: 0 bytes in 0 blocks
==8822== Rerun with --leak-check=full to see details of leaked memory
==8822==
==8822== For counts of detected and suppressed errors, rerun with: -v
==8822== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Thanks for showing the errors, I sure hope I know how to get these. However, I'm sorry I don't know which of my pointers are not initialised and are causing problem.
However, I do have an updated code, seemingly running without problem but just want to be sure, can you teach me how to get those things you got?