vector with reinterpret_cast - c++

The following code inserts only one value to the vector col.
The code is extracted from DBMS code base (for importing files), specifically, it is from 1
The code uses void* to be able to read any field type (int, float, and so on).
#include <iostream>
#include <vector>
using namespace std;
void add(std::vector<void*> &col){
reinterpret_cast<std::vector<int>&>(col).push_back( 1);
reinterpret_cast<std::vector<int>&>(col).push_back( 2);
reinterpret_cast<std::vector<int>&>(col).push_back( 13);
}
int main() {
std::vector<void*> col;
add(col);
cout << col.size() << endl;
for(int i=0;i<col.size();i++)
cout <<reinterpret_cast<std::vector<int>&> (col)[i] <<endl;
return 0;
}
I am not sure how this code work?

Your code is exhibiting undefined behavior.
std::vector<void*> and std::vector<int> are two completely separate and unrelated types, you can't safely cast between them the way you are, especially since there is no guarantee that void* and int are the same byte size.
Cast the values you are pushing, don't cast the vector itself, eg:
#include <iostream>
#include <vector>
#include <cstdint>
using namespace std;
void add(std::vector<void*> &col) {
col.push_back(reinterpret_cast<void*>(static_cast<intptr_t>(1)));
col.push_back(reinterpret_cast<void*>(static_cast<intptr_t>(2)));
col.push_back(reinterpret_cast<void*>(static_cast<intptr_t>(13)));
}
int main() {
std::vector<void*> col;
add(col);
cout << col.size() << endl;
for(int i=0;i<col.size();i++)
cout << reinterpret_cast<intptr_t>(col[i]) << endl;
return 0;
}
Of course, you really should be using the proper container type to begin with:
#include <iostream>
#include <vector>
using namespace std;
void add(std::vector<int> &col) {
col.push_back(1);
col.push_back(2);
col.push_back(13);
}
int main() {
std::vector<int> col;
add(col);
cout << col.size() << endl;
for(int i=0;i<col.size();i++)
cout << col[i] << endl;
return 0;
}

Related

Declaring sqrt(var) as a compile time constant in c++

I have a c++ program where I need to pass the square root of a number in a for loop.
#include<random>
#include<iostream>
#include<algorithm>
#include<string>
#include<math.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
#include <omp.h>
using namespace std;
int main()
{
vector<int>inputDataBits(49); // vector of randomly generated input data bits
#ifdef printDebug
std::cout << "the input data bits are" << endl;
std::cout << "-------------------------" << endl << endl;
int var =49;
const int r=(int)sqrt(var);
float input2d[r][r];
for (int i = 0; i < r; i++)
{
for (int j = 0; j < r; j++)
{
input2d[i][j] = inputDataBits[(j %r) + (i *r)];
std::cout << input2d[i][j] << "\t";
}
std::cout << endl << endl;
}
std::cout << endl << endl;
#endif
return 0;
}
I get an error 'expression must have a constant value'. Is there a way to do this in c++?
This is the purpose of the constexpr keyword (make the value known at compile time).
constexpr int var=49;
constexpr int r=(int)sqrt(var);
Unfortunately, in the documentation sqrt() is not declared as a constexpr function.
Only gcc seems to consider it as constexpr but it is not portable.
The size of an array needs to be known at compile-time.
Instead you can use a std::vector, which has a dynamic size.
std::vector<std::vector<float>> input2d(std::vector<float>(r), r);

the vector constructor doesn't change the end_file istream_iterator

i'm studying C++ for C programmers course (coursera) and in module 4 there is an example for how to use istream iterators to load data to STL vector ..but when i tried the code it only printed the first number from the file. i can't find the mistake in the code.
note :the instructor didn't run the code, he Taught is using PDF. so maybe there something missing in it.
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
fstream data_file("data.txt");
istream_iterator<int> start_file(data_file), end_file;
vector<int> data(start_file, end_file);
int sum = 0;
for (auto i = start_file; i != end_file; i++)
{
sum += *i;
cout << *i << endl;
}
cout << data.size()<<endl;
cout << sum << endl;
cout << (sum* 1.0) / data.size() << endl;
return 0;
}

My function does not modify its inputs

I'm trying to learn C++ and have this small beginner question:
why does the standardize function not modify its inputs?
To help with the answers, I have posted an executing code at Coliru
here
and the sources of my program below.
Referring to the code, the question would be: why isn't what's
printed after outside the same as what's printed after inside?
#include <cstdlib>
#include <ctime>
#include <algorithm> // std::copy
#include <iostream>
using namespace std;
void standardize(const int n,const float x[],float ave,float sct){
float acc=0.0f,sum=0.0f;
sum=std::accumulate(x,x+n,0.0f);
ave=sum/(float)n;
std::for_each(x,x+n,[&](const float d){acc+=(d-ave)*(d-ave);});
sct=std::sqrt(acc/(float)(n-1));
std::cout << "inside" << std::endl;
std::cout << ave << std::endl;
std::cout << sct << std::endl;
return;
}
int main(){
const int n=1024;
float a2[n];
float part0=0.0f,part1=0.0f;
std::srand(std::time(0));
for(int i=0;i<n;i++) a2[i]=std::rand()/(float)RAND_MAX;
standardize(n,a2,part0,part1);
std::cout << "outside" << std::endl;
std::cout << part0 << std::endl;
std::cout << part1 << std::endl;
}
You are passing ave and sct by values. Your standardize method modifies copies of those arguments, letting unchanged the original ones declared in main()
Consider passing them by reference:
void standardize(const int n,const float x[],float& ave,float& sct)

Pointer and declaration difficulty

Here's a piece of my code that I divided. I basically need to create 3 functions:
Fill array with random numbers
Print array on the screen
Is not included in this piece.
My problem is that I get the
C2664 error:cannot convert parameter 1 from int[6][6] to int(*)[].
And I cant figure what's wrong with my code. I would also like to check if how I wrote my pointers to fill and print the array is correct.
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <time.h>
#include <iomanip>
#include <array>
#include <algorithm>
using namespace std;
const int AS = 6;
void FillingRandomly(int *);
void printing(int *);
int c;
int main()
{
int funny = 0;
int timpa = 0;
int counter = 0;
int Array[AS][AS];
srand(time(0));
FillingRandomly(Array);
cout << "The unsorted array is" << endl << endl;
printing(Array);
cout << "The sorted array is" << endl << endl;
system("PAUSE");
return 0;
}
void FillingRandomly(int *Array) {
*Array = rand()%87 +12;
*Array ++;
}
void printing(int *ArrayPtr) {
int counter = 0;
while(*ArrayPtr<AS*AS) {
cout<<*ArrayPtr;
*ArrayPtr++;
if (*ArrayPtr%AS == 0)
cout << endl << endl;
}
}
Don't use pointers to int, because you want to pass an array, not a pointer, to your functions.
In addition, pass it by reference. To do this, change the declarations for your functions:
void FillingRandomly(int (&Array)[AS][AS]);
void printing(const int (&Array)[AS][AS]);
(the declaration is different for printing because it doesn't need to change the array)
The definition must be written in the same manner:
void FillingRandomly(int (&Array)[AS][AS])
{
...
}
Note also that in C++ arrays are sometimes (or most always - depends on whom you ask) represented by the standard-library classes, like std::vector or std::array. I'll not show the code for using them to keep my answer focused.

Program to initialize a vector from an array of ints. Cout is getting error?

Is the first element of arr1 getting added?
cout is giving me an error. What am I doing wrong?
#include <iostream>
using std::cin; using std::cout; using std::endl;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <cstddef>
using std::size_t;
int main ()
{
vector <int> ivec1; //defines a vector named ivec1 to hold values not yet defined
int arr1 [5] = {10, 20, 30 , 40, 50}; // defines array named arr1 with 5 values
ivec1.push_back (arr1 [0]);
cout << ivec1 << endl;
return 0;
}
The answer is quite simple: The operation you are invoking is simply not defined. The IO stream library is blissfully unaware of C++ standard library containers (besides std::string) and does not know how to print them. You will need to do that yourself.
std::vector<int> v;
for(auto& x : v)
std::cout << v << " "; // print each element
std::cout << std::endl; // and a linebreak
I guess cout is not able to work with vectors. I'd implement something like this (I'm sorry for my C++ I didn't write in C++ since 2006...
#include "stdafx.h"
#include <iostream>
using std::cin; using std::cout; using std::endl;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <cstddef>
using std::size_t;
void fillVector(int output[], vector<int>& input, int size)
{
for(int i=0;i<size; i++)
{
input.push_back(output[i]);
}
}
void printVector(vector<int>& input)
{
for(int i=0; i<input.size(); i++)
{
cout << input.at(i);
if(i!=input.size()-1)
{
cout << ",";
}
}
cout << endl;
}
int main ()
{
vector <int> ivec1; //defines a vector named ivec1 to hold values not yet defined
int arr1 [5] = {10, 20, 30 , 40, 50}; // defines array named arr1 with 5 values
int sz = sizeof(arr1) / sizeof(int);
fillVector(arr1, ivec1, sz);
printVector(ivec1);
return 0;
}