Skip to content

Instantly share code, notes, and snippets.

@TyounanMOTI
Created October 20, 2011 06:18
Show Gist options
  • Save TyounanMOTI/1300548 to your computer and use it in GitHub Desktop.
Save TyounanMOTI/1300548 to your computer and use it in GitHub Desktop.
std::valarray initialization from raw pointer copies deeply.
#include <gtest/gtest.h>
#include <valarray>
TEST(ValarrayTest, InitializeWithRawPointer) {
int *p = (int*)malloc(sizeof(int)*10);
p[0] = 4;
ASSERT_EQ(4, p[0]);
std::valarray<int> subject(p, 10);
ASSERT_EQ(4, subject[0]);
p[0] = 6;
EXPECT_EQ(6, subject[0]); // FAIL!
free(p);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment