I really need some help. this is a simple program, its supposed to create a vector of c style strings. something is wrong though, if you look at the output, it is filling my vector of c-style strings with the last cstyle string that i generated.
PS. i am using c++11 so if you have older versions you wont b able to run this becouse of my random number generator.
#include<random>
#include<vector>
#include<functional>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include <cstring>
#include <string>
// generates random c style strings, and stores them in a vector, at least it is supposed to do that
usingnamespace std;
int main( )
{
auto gen = bind(normal_distribution<double>(13,4),default_random_engine()); // Normal distribution random number generator
int vectorsize =10;
char strcnv[9]={'\0'};
char* cstrcnv;
vector <char*> cst (vectorsize); //string vector
for (int j =0; j<(vectorsize); ++j) //populate the string vector
{
for (int k = 0; k<8; ++k)
{
int charname = gen();
strcnv[k] = (char)(((int) 'A') + charname);
if (k%8==0)
{
cstrcnv = strcnv;
cst[j] = cstrcnv ;
}
}
cout<< " inside the loop "<<j<<" "<<cst[j]<<endl;
}
for (int i = 0; i < vectorsize; i++)
{
cout<<" outside the loop "<<i<<" "<<cst[i]<<" "<<endl;
}
return 0;
}
my output outside of the loop is just the last generated random c-style string that has populated my vector.
This is the only space allocated for any c-style strings.
What you are doing is continually reusing that space to generate different strings (overwriting previously generated strings,) while you insert the same address into the vector multiple times.
Since every pointer in the vector is pointing to the same memory, the output is not surprising.
I realized that, but i cant think of how to this task correctly. i tried diffirent things, im a novice. any suggestions would be extremely appreciated.
i forgot to mention, even though it is a vector of only 10 elements here, I made it small for the sake of debugging. it is potentially a very large vector.
#include<random>
#include<vector>
#include<functional>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include <cstring>
#include <string>
// generates random c style strings, and stores them in a vector, at least it is supposed to do that
usingnamespace std;
int main( )
{
auto gen = bind(normal_distribution<double>(13,4),default_random_engine()); // Normal distribution random number generator
int vectorsize =10;
char strcnv[9]={'\0'};
char* cstrcnv;
vector <char*> cst (vectorsize); //string vector
for (int j =0; j<(vectorsize); ++j) //populate the string vector
{
for (int k = 0; k<8; ++k)
{
int charname = gen();
strcnv[k] = (char)(((int) 'A') + charname);
if (k%8==0)
{
cst[ j ] = newchar[ 9 ];
cstrcnv = strcnv;
cst[j] = cstrcnv ;
}
}
cout<< " inside the loop "<<j<<" "<<cst[j]<<endl;
}
for (int i = 0; i < vectorsize; i++)
{
cout<<" outside the loop "<<i<<" "<<cst[i]<<" "<<endl;
}
return 0;
}
What do you expect cst[ j ] to be after these 2 lines are run ? (you are not creating a copy) You are also creating a memory leak by overwriting the pointer before freeing the memory.
#include<random>
#include<vector>
#include<functional>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include <cstring>
#include <string>
// generates random c style strings, and stores them in a vector, at least it is supposed to do that
usingnamespace std;
int main( )
{
auto gen = bind(normal_distribution<double>(13,4),default_random_engine()); // Normal distribution random number generator
int vectorsize =10;
// char strcnv[9]={'\0'};
char* cstrcnv;
vector <char*> cst (vectorsize); //string vector
for (int j =0; j<(vectorsize); ++j) //populate the string vector
{
cstrcnv = newchar[1000];
for (int k = 0; k<8; ++k)
{
int charname = gen();
cstrcnv[k] = (char)(((int) 'A') + charname);
strcat(cstrcnv, "\0");
}
cst[ j ] = cstrcnv;
cout<< " inside the loop "<<j<<" "<<cst[j]<<endl;
}
for (int i = 0; i < vectorsize; i++)
{
cout<<" outside the loop "<<i<<" "<<cst[i]<<" "<<endl;
}
return 0;
}
There is a memory leak as you never deallocate the memory you allocated. Also allocating a 1000 char array for each 9 character string seems a little wasteful. If you can't go with std::string for whatever reason, you should definitely use std::unique_ptr.