diff options
Diffstat (limited to 'c,cc')
64 files changed, 2083 insertions, 0 deletions
diff --git a/c,cc/Makefile b/c,cc/Makefile new file mode 100644 index 0000000..64008d1 --- /dev/null +++ b/c,cc/Makefile @@ -0,0 +1,17 @@ +CFLAGS += -pedantic -Wall -std=c99 + +compression_test: compression_test.cc + g++ compression_test.cc -lboost_filesystem -lboost_iostreams -o compression_test + +vector_addition: vector_addition.c + gcc -Os vector_addition.c -o vector_addition + +vector_addition_cuda: vector_addition.cu + nvcc vector_addition.cu -o vector_addition_cuda + +vector_addition_cuda1: vector_addition1.cu + nvcc vector_addition1.cu -o vector_addition_cuda1 + +thread_test: thread_test.c + gcc thread_test.c -lpthread -o thread_test + diff --git a/c,cc/animal.cc b/c,cc/animal.cc new file mode 100644 index 0000000..3bad1b1 --- /dev/null +++ b/c,cc/animal.cc @@ -0,0 +1,37 @@ +#include <iostream> + +using namespace std; + + +class Animal +{ + public: + Animal() { // Konstruktor + cout << "Der Konstruktor wird aufgerufen." << endl; + m_alive = true; + } + + ~Animal() {cout << "Der Destruktor wird aufgerufen." << endl;} + + bool isAlive() { // Methode + return m_alive; + } + + void kill() { // Methode + m_alive=false; + } + + private: + bool m_alive; // Member-Variable +}; + + +int main(void) +{ + int i; + bool alive; + + Animal a; // Instanz von Animal + Animal b; // noch eine ^ +} + diff --git a/c,cc/base-member.cc b/c,cc/base-member.cc new file mode 100644 index 0000000..4c3a86f --- /dev/null +++ b/c,cc/base-member.cc @@ -0,0 +1,32 @@ +/* + * template.cpp + * + * Patrick Simianer <p@simianer.de> + * YYYY-MM-DD + */ + +#include <iostream> + +using namespace std; + +class A +{ + public: + int a; +}; + +class B : public A +{ + public: + int b; +}; + +int main(int argc, char const* argv[]) +{ + B b; + b.b = 1; + b.a = 2; + cout << b.a << endl; + return 0; +} + diff --git a/c,cc/base_derived_vector.cc b/c,cc/base_derived_vector.cc new file mode 100644 index 0000000..e20a43d --- /dev/null +++ b/c,cc/base_derived_vector.cc @@ -0,0 +1,35 @@ +#include <iostream> +#include <vector> + +class Base { +public: + virtual void + f() + { + std::cout << "Base\n"; + } +}; + +class Derived: public Base { +public: + virtual void + f() + { + std::cout << "Derived\n"; + } +}; + +int +main() +{ + std::vector<Base> s; + Base a; + s.push_back(a); + Derived b; + s.push_back(b); + for (std::vector<Base>::iterator it = s.begin(); it != s.end(); ++it) + it->f(); + + return 0; +} + diff --git a/c,cc/bitwise.c b/c,cc/bitwise.c new file mode 100644 index 0000000..c90bfbd --- /dev/null +++ b/c,cc/bitwise.c @@ -0,0 +1,51 @@ +#include <stdio.h> + + +void +test1() +{ + int foo[4] = {147, 142, 207, 19}; + unsigned long result = 0; + + for( int i = 0; i < 4; i += 1 ) + { + result = result<<8; // 8 bit left + result = foo[i]|result; + } + + printf("%lx\n", result); +} + +void +test2(void) +{ + int foo[4]; + unsigned long bar = 0x938ecf13; + + for( int i = 0; i < 4; i += 1 ) + { + foo[i] = (bar>>i*8)&255; + + printf("%d\n", foo[i]); + } +} + +void +test3(void) +{ + int s = 12; + unsigned long foo = 0xffffffff; + + foo = foo<<s; + printf("%lx\n", foo); +} + +int main(void) +{ + test1(); + test2(); + test3(); + + return 0; +} + diff --git a/c,cc/cast.cc b/c,cc/cast.cc new file mode 100644 index 0000000..2e84a64 --- /dev/null +++ b/c,cc/cast.cc @@ -0,0 +1,27 @@ +#include <iostream> + +using namespace std; + + +struct A { + virtual void p() { cout << "A" << endl; } + void q() { cout << "q" << endl; } +}; + +struct Ba : public A { + void p() { cout << "Ba" << endl; } +}; + +struct Bb : public A { + void p() { cout << "Bb" << endl; } +}; + +int main(void) +{ + A* x = new A; + x = dynamic_cast<Ba*>(new Ba); + cout << "-" << endl; + x->q(); + x->p(); +} + diff --git a/c,cc/cast_char.cc b/c,cc/cast_char.cc new file mode 100644 index 0000000..c3a3315 --- /dev/null +++ b/c,cc/cast_char.cc @@ -0,0 +1,21 @@ +#include <iostream> + +using namespace std; + +int main(void) +{ + int i = 255; + char c = 'x'; + char d = -1; + char e = 255; + char f = 'f'; + char q = 127; + + std::cout << (char)i << std::endl; + std::cout << (int)c << std::endl; + std::cout << (int)d << std::endl; + std::cout << (int)e << std::endl; + std::cout << (int)f << std::endl; + std::cout << (int)q << std::endl; +} + diff --git a/c,cc/cat_char.cc b/c,cc/cat_char.cc new file mode 100644 index 0000000..3daf564 --- /dev/null +++ b/c,cc/cat_char.cc @@ -0,0 +1,16 @@ +#include <iostream> +#include <string.h> + +using namespace std; + + +int main(void) +{ + char c[] = "c"; + char x[1024]; + string s("s"); + strcpy(x, s.c_str()); + strcat(x, c); + cout << x << endl; +} + diff --git a/c,cc/class.cc b/c,cc/class.cc new file mode 100644 index 0000000..b493b51 --- /dev/null +++ b/c,cc/class.cc @@ -0,0 +1,31 @@ +#include <iostream> + +using namespace std; + + +class A { + public: + A(int a):_a(a) {} + int geta() { + return _a; + } + static int _b; + private: + int _a; +}; + +int A::_b = 0; + +int main(void){ + A a(2); + A *b = new A(2); + cout << b->geta() << endl; + cout << b->_b << endl; + cout << a._b << endl; + A::_b = 42; + cout << b->geta() << endl; + cout << b->_b << endl; + cout << a._b << endl; + return 0; +} + diff --git a/c,cc/class_member.cc b/c,cc/class_member.cc new file mode 100644 index 0000000..f78ca64 --- /dev/null +++ b/c,cc/class_member.cc @@ -0,0 +1,33 @@ +#include <iostream> + +using namespace std; + + +struct A +{ +}; + +struct B : public A +{ + +}; + +struct C : public A +{ + +}; + +struct D +{ + A* a; +}; + +int main(void) +{ + B b; + D d; + A a; + + d.a = &b; +} + diff --git a/c,cc/compression_test.cc b/c,cc/compression_test.cc new file mode 100644 index 0000000..21bfafc --- /dev/null +++ b/c,cc/compression_test.cc @@ -0,0 +1,45 @@ +#include <iostream> +#include <fstream> +#include <boost/iostreams/device/file.hpp> +#include <boost/iostreams/filter/zlib.hpp> +#include <boost/iostreams/filter/bzip2.hpp> +#include <boost/iostreams/filter/gzip.hpp> +#include <boost/iostreams/filtering_stream.hpp> + +using namespace boost::iostreams; +using namespace std; + + +int main(void) +{ + //ofstream raw("out-raw"); + filtering_ostream out_z; + out_z.push(gzip_compressor()); + //out_gz.push(gzip_compressor()); + out_z.push(file_sink("out-z", std::ios::binary)); + //out_gz.push(file_sink("out-gz", std::ios::binary)); + for (size_t i = 0; i < 10; i++) { + out_z << "line #" << i << endl; + //out_gz << "line #" << i << endl; + //out_bz << "line #" << i << endl; + //raw << "line #" << i << endl; + } + // flush(out); + close(out_z); + //close(out_gz); + //close(out_bz); + //raw.close(); + + for (size_t i = 0; i < 5; i++) { + ifstream file("out-z", ios_base::in | ios_base::binary); + filtering_istream in; + in.push(gzip_decompressor()); + in.push(file); + string s; + while (getline(in, s)) { + cout << s << endl; + } + file.close(); + } +} + diff --git a/c,cc/cond.h b/c,cc/cond.h new file mode 100644 index 0000000..0d9e572 --- /dev/null +++ b/c,cc/cond.h @@ -0,0 +1,9 @@ +#ifndef _COND_H_ +#define _COND_H_ + + +#define cond(expr, a, b) (expr) ? (a) : (b) + + +#endif /* _COND_H_ */ + diff --git a/c,cc/copy_struct.cc b/c,cc/copy_struct.cc new file mode 100644 index 0000000..cb23b09 --- /dev/null +++ b/c,cc/copy_struct.cc @@ -0,0 +1,36 @@ +#include <iostream> +#include <vector> + +using namespace std; + +struct C { + vector<int> i; +}; + +struct B { + C c; +}; + +struct A { + B b; +}; + +int +main(void) +{ + A a; + B b; + C c; + + c.i.push_back(23); + b.c = c; + a.b = b; + + cout << a.b.c.i[0] << endl; + + A x(a); + cout << x.b.c.i[0] << endl; + + return 0; +} + diff --git a/c,cc/ctype.c b/c,cc/ctype.c new file mode 100644 index 0000000..0331566 --- /dev/null +++ b/c,cc/ctype.c @@ -0,0 +1,43 @@ +#include <stdio.h> +#include <ctype.h> +#include <string.h> + + +int main(void) +{ + char *a="abc"; + char *x="xyz"; + char *b="abc"; + char *y="Xyz"; + + int r; + + if(isupper(y[0])) + { + printf("%c\n",y[0]); + } + + if(r=(!strcmp(a,b))) + { + printf("%d\n",r); + } + + printf("%c\n",*x); + + if(r=(!strcmp(a,x))) + { + printf("%d\n",r); + } + + char bla[4]="aAc"; + + for( unsigned int i = 0; i < 4; i += 1 ) + { + if(isupper(bla[i])) { + printf("%c\n",bla[i]); + } + } + + return 0; +} + diff --git a/c,cc/delegate_iterator.cc b/c,cc/delegate_iterator.cc new file mode 100644 index 0000000..c4e272e --- /dev/null +++ b/c,cc/delegate_iterator.cc @@ -0,0 +1,32 @@ +#include <iostream> +#include <vector> + +using namespace std; + +class A { + private: + typedef vector<int> Data; + Data data_; + public: + typedef Data::iterator iterator; + typedef const Data::iterator const_iterator; + iterator begin() { return data_.begin(); } + iterator end() { return data_.end(); } + + void add(int i) { + data_.push_back(i); + } +}; + +int +main(void) +{ + A a; + a.add(23); + for (A::iterator it = a.begin(); it != a.end(); it++) { + cout << *it << endl; + } + + return 0; +} + diff --git a/c,cc/derive_from_templated_class.cc b/c,cc/derive_from_templated_class.cc new file mode 100644 index 0000000..f252981 --- /dev/null +++ b/c,cc/derive_from_templated_class.cc @@ -0,0 +1,29 @@ +#include <vector> +#include <iostream> + +using namespace std; + + +template<class T> +class A { + T m_; +}; + +template<class T> +class B : public A<T> { + T n_; +}; + +template<class T> +void f(vector<B<T>*> v) +{ + for (typename vector<B<T>*>::iterator it = v.begin(); it != v.end(); it++) { + // do sth + } +} + +int main(void) +{ + vector<B<int>* > v; +} + diff --git a/c,cc/derived.cc b/c,cc/derived.cc new file mode 100644 index 0000000..809378a --- /dev/null +++ b/c,cc/derived.cc @@ -0,0 +1,28 @@ +#include <iostream> +#include <vector> + +using namespace std; + +struct Base { + virtual int + index() { return -1; }; +}; + +struct D1 : public Base { + int idx = 1; + + virtual int + index() { return idx; } +}; + +int +main(void) +{ + vector<Base*> v; + D1* d = new D1; + v.push_back(d); + cout << v.back()->index() << endl; + + return 0; +} + diff --git a/c,cc/double_pointer.c b/c,cc/double_pointer.c new file mode 100644 index 0000000..1c66b9a --- /dev/null +++ b/c,cc/double_pointer.c @@ -0,0 +1,35 @@ +#include <stdio.h> + + +int main(void) +{ + char word[] = "abcdef"; + + char *wp = word; + + printf("%s\n", wp); + + wp++; + + printf("%s\n",wp); + + wp++; + + printf("%s\n",wp); + + wp++; + + printf("%s\n",wp); + + wp++; + + printf("%s\n",wp); + + wp++; + + printf("%s\n",wp); + + + return 0; +} + diff --git a/c,cc/file_index b/c,cc/file_index new file mode 100644 index 0000000..b36d133 --- /dev/null +++ b/c,cc/file_index @@ -0,0 +1,2 @@ +1 2 3 +4 5 6 diff --git a/c,cc/file_index.cc b/c,cc/file_index.cc new file mode 100644 index 0000000..c6ec334 --- /dev/null +++ b/c,cc/file_index.cc @@ -0,0 +1,29 @@ +/* + * template.cpp + * + * Patrick Simianer <p@simianer.de> + * YYYY-MM-DD + */ + +#include <iostream> +#include <string> +#include <vector> +#include <sstream> + +using namespace std; + + +int main(int argc, char const* argv[]) +{ + string line; + while (getline(cin, line)) { + istringstream ss(line); + size_t e; + while (ss >> e) { + cout << e << endl; + } + } + + return 0; +} + diff --git a/c,cc/filelib.c b/c,cc/filelib.c new file mode 100644 index 0000000..57a27c3 --- /dev/null +++ b/c,cc/filelib.c @@ -0,0 +1,87 @@ +#include "filelib.h" + + +typedef struct +{ + hFile b_; + FILE* fp; +} hFile_; + +typedef struct +{ + hFile b_; + gzFile* fp; +} hgzFile; + +gzmode(const char* mode) +{ + // TODO + return true; +} + +hgzFile* +gzmakef(char* fname, const char* mode) +{ + hgzFile* hgz = malloc(sizeof(hgzFile)); + hgz->b_.name = fname; + hgz->b_.gz = true; + gzFile* fp = malloc(sizeof(gzFile)); + *fp = gzopen(fname, mode); + hgz->fp = fp; + return hgz; +} + +hFile* +makef(char* fname, const char* mode) +{ + if (endswith(fname, GZIP_EXT)) + return gzmakef(fname, mode); + hFile_* f = malloc(sizeof(hFile_)); + if (!f) return NULL; + f->b_.name = fname; + f->b_.gz = false; + if (!strcmp(fname, "-")) { + if (!strcmp(mode, "r") && !strcmp(mode, "r+")) f->fp = stdin; + else f->fp = stdout; + } else { + f->fp = fopen(fname, mode); + } + return f; +} + +bool +gzwriteln(const char* s, hFile* f) +{ + unsigned len = strlen(s); + gzwrite(*((hgzFile*)f)->fp, s, len); + gzwrite(*((hgzFile*)f)->fp, "\n", 1); + return gzflush(*((hgzFile*)f)->fp, Z_FINISH) == Z_OK; +} + +bool +writeln(const char* s, hFile* f) +{ + if (f->gz) + return gzwriteln(s, f); + return fputs(s, ((hFile_*)f)->fp)&&fputs("\n", ((hFile_*)f)->fp); +} + +bool +gzclosef(hFile* f) +{ + bool ret = gzclose(*((hgzFile*)f)->fp); + free(((hgzFile*)f)->fp); + free(f); + return ret; +} + +bool +closef(hFile* f) +{ + if (f->gz) + return gzclosef(f); + bool ret = fclose(((hFile_*)f)->fp); + free(f); + return ret; +} + diff --git a/c,cc/filelib.h b/c,cc/filelib.h new file mode 100644 index 0000000..587cddf --- /dev/null +++ b/c,cc/filelib.h @@ -0,0 +1,25 @@ +#ifndef FILELIB_H +#define FILELIB_H + +#include <stdlib.h> +#include <stdio.h> +#include <stdbool.h> +#include <zlib.h> +#include <assert.h> +#include "util.h" + +#define GZIP_EXT ".gz" + + +typedef struct +{ + char* name; // relative + bool gz; +} hFile; + +hFile* makef(char* fname, const char* mode); +bool writeln(const char* s, hFile* f); + + +#endif + diff --git a/c,cc/float_int.cc b/c,cc/float_int.cc new file mode 100644 index 0000000..9d0ece4 --- /dev/null +++ b/c,cc/float_int.cc @@ -0,0 +1,26 @@ +#include <iostream> + +using namespace std; + + +int main(void) +{ + int a = 1; + int b = 2; + double q = (double)a/b; + cout << q << endl; + + cout << 1 - (float)a/b << endl; + + float x = 0.1; + unsigned y = 105; + unsigned z = x*y; + cout << z << endl; + + float u = -1; + if (u) cout << "XXXXXX" << endl; + + float j = 0.0; + if (j) cout << "j" << endl; +} + diff --git a/c,cc/generator.cc b/c,cc/generator.cc new file mode 100644 index 0000000..c073e2e --- /dev/null +++ b/c,cc/generator.cc @@ -0,0 +1,15 @@ +#include <functional> +#include <iostream> + + +std::function<int()> generator = []{ + int i = 0; + return [=]() mutable { + return i < 10 ? i++ : -1; + }; +}(); + +int main(void) +{ +int ret = 0; while ((ret = generator()) != -1) std::cout << "generator: " << ret << std::endl; +} diff --git a/c,cc/gz.c b/c,cc/gz.c new file mode 100644 index 0000000..a0dcd90 --- /dev/null +++ b/c,cc/gz.c @@ -0,0 +1,16 @@ +#include <zlib.h> +#include <string.h> + +int +main(void) +{ + gzFile gz = gzopen("asdf.gz", "wb"); + int len = 0; + gzwrite(gz, "asdf\n", len); + char *s = "asdf\n"; + gzwrite(gz, "asdf\n", strlen(s)); + gzclose(gz); + + return 0; +} + diff --git a/c,cc/in_out.c b/c,cc/in_out.c new file mode 100644 index 0000000..448eaa5 --- /dev/null +++ b/c,cc/in_out.c @@ -0,0 +1,14 @@ +#include <stdio.h> + + +int main(void) +{ + int ch; + + while ((ch=fgetc(stdin))!=EOF) { + fputc(ch, stdout); + } + printf("\n"); + return 0; +} + diff --git a/c,cc/matrix.cc b/c,cc/matrix.cc new file mode 100644 index 0000000..9d1a1c0 --- /dev/null +++ b/c,cc/matrix.cc @@ -0,0 +1,210 @@ +#include <iostream> +#include <stdlib.h> + +using namespace std; + + +class MyMatrix +{ + public: + MyMatrix(int dimension); + MyMatrix(int rows, int columns); + ~MyMatrix(); + + void print(); + int get_rows(); + int get_columns(); + + double get_element(int row, int column); + void set_element(int row, int column, double value); + + MyMatrix operator + (MyMatrix A); + MyMatrix operator * (MyMatrix A); + void operator = (MyMatrix A); + + + private: + double* m; + int row_d; + int col_d; + int d; +}; + +MyMatrix::MyMatrix(int dimension) +{ + row_d = dimension; + col_d = dimension; + d = dimension*dimension; + m = new double[d]; + + unsigned int k=0; + for(unsigned int i=0; i<row_d; i++) { + for(unsigned int j=0; j<col_d; j++) { + if(i==j) { + m[k] = 1; + } else { + m[k] = 0; + } + k++; + } + } +} + +MyMatrix::MyMatrix(int rows, int columns) +{ + row_d = rows; + col_d = columns; + d = rows*columns; + m = new double[d]; + + + srand(time(NULL)); + for(unsigned int i=0; i<d; i++) { + m[i] = 1+(rand()%(100-1+1)); + } +} + +MyMatrix::~MyMatrix() +{ + delete[] m; +} + +void MyMatrix::print() +{ + unsigned int k=0; + for(unsigned int i=0; i<row_d; i++) { + for(unsigned int j=0; j<col_d; j++) { + cout << m[k] << " "; + k++; + } + cout << endl; + } +} + +int MyMatrix::get_rows() +{ + return row_d; +} + +int MyMatrix::get_columns() +{ + return col_d; +} + + +double MyMatrix::get_element(int row, int column) +{ + // first element is 1,1! + if((row*column)<=0) { + return -1; + } + return m[((row-1)*col_d)+(column-1)]; +} + +void +MyMatrix::set_element(int row, int column, double value) +{ + if((row*column)>0) { + m[((row-1)*col_d)+(column-1)] = value; + } +} + + +MyMatrix +MyMatrix::operator + (MyMatrix A) +{ + MyMatrix* res = new MyMatrix(row_d, col_d); + + unsigned int k=0; + for(unsigned int i=0; i<row_d; i++) { + for(unsigned int j=0; j<col_d; j++) { + res->m[k] = m[k]+A.get_element(i+1, j+1); + k++; + } + } + + return *res; +} + +MyMatrix MyMatrix::operator * (MyMatrix A) +{ + MyMatrix* res = new MyMatrix(row_d, col_d); + + unsigned int k=0; + for(unsigned int i=0; i<row_d; i++) { + for(unsigned int j=0; j<col_d; j++) { + res->m[k] = m[k]*A.get_element(i+1, j+1); + k++; + } + } + + return *res; +} + +void MyMatrix::operator = (MyMatrix A) +{ + unsigned int k=0; + for(unsigned int i=0; i<A.get_rows(); i++) { + for(unsigned int j=0; j<A.get_columns(); j++) { + m[k] = A.get_element(i+1, j+1); + k++; + } + } +} + + +int main (int argc, char* const argv[]) { + cout << "9x9 identity matrix:" << endl; + MyMatrix* m = new MyMatrix(9); + m->print(); + + cout << " rows: " << m->get_rows() << ", "; + cout << "columns: " << m->get_columns() << endl; + + delete m; + + + cout << endl << "2x5 matrix:" << endl; + MyMatrix n(2,5); + n.print(); + + cout << " rows: " << n.get_rows() << ", "; + cout << "columns: " << n.get_columns() << endl; + + cout << "(1,3) = " << n.get_element(1,3) << endl; + + cout << "last: (2,5) = " << n.get_element(2,5) << endl; + + cout << "second to last element = 100: " << endl; + n.set_element(2, 4, 100); + n.print(); + + cout << endl << "addition:" << endl; + cout << "matrix 1:" << endl; + MyMatrix* a1 = new MyMatrix(3,3); + a1->print(); + cout << "matrix 2:" << endl; + MyMatrix* a2 = new MyMatrix(3,3); + a2->print(); + cout << "result:" << endl; + MyMatrix* e1 = new MyMatrix(3,3); + *e1 = *a1+*a2; + e1->print(); + delete e1; + + cout << endl << "multiplication" << endl; + cout << "matrix 1:" << endl; + MyMatrix* m1 = new MyMatrix(3,3); + a1->print(); + cout << "matrix 2:" << endl; + MyMatrix* m2 = new MyMatrix(3,3); + m2->print(); + cout << "result:" << endl; + MyMatrix* e2 = new MyMatrix(3,3); + *e2 = *m1 * *m2; + e2->print(); + delete e2; + + return 0; +} + diff --git a/c,cc/ooc.c b/c,cc/ooc.c new file mode 100644 index 0000000..aa20488 --- /dev/null +++ b/c,cc/ooc.c @@ -0,0 +1,78 @@ +#include "ooc.h" +#include "stdio.h" +#include "string.h" + + +struct van { + struct vehicle base; + int cubic_size; +}; + +struct bus { + struct vehicle base; + int seats; +}; + +struct van* +make_van() +{ + struct van* v = malloc(sizeof(struct van)); + v->base.type = "van"; + v->cubic_size = 12; + return v; +} + +struct bus* +make_bus() +{ + struct bus* v = malloc(sizeof(struct bus)); + v->base.type = "bus"; + v->seats=112; + return v; +} + +struct vehicle* +make_vehicle(const char* type) +{ + if(!strcmp(type, "van")) return make_van(); + if(!strcmp(type, "bus")) return make_bus(); + return NULL; +} + +void +do_something_with_a_bus(struct vehicle* v) +{ + ((struct bus*)v)->seats = 13; +} + +void +do_something_with_a_van(struct vehicle* v) +{ + ((struct van*)v)->cubic_size = 11; +} + +void +do_something(struct vehicle* v) +{ + if(!strcmp(v->type, "van")) return do_something_with_a_van(v); + if(!strcmp(v->type, "bus")) return do_something_with_a_bus(v); +} + +int +main(void) { + struct van my_van; + struct vehicle *something = &my_van; + my_van.cubic_size = 100; + my_van.base.power = 99; + printf("%d\n", something->power); + printf("%d\n", my_van.base.power); + + struct bus* bus = make_vehicle("bus"); + printf("%s\n", bus->base.type); + printf("%d\n", bus->seats); + do_something(bus); + printf("%d\n", bus->seats); + + return 0; +} + diff --git a/c,cc/ooc.h b/c,cc/ooc.h new file mode 100644 index 0000000..0585d95 --- /dev/null +++ b/c,cc/ooc.h @@ -0,0 +1,6 @@ +struct vehicle { + int power; + int weight; + char* type; +}; + diff --git a/c,cc/padding.cc b/c,cc/padding.cc new file mode 100644 index 0000000..7ac9be7 --- /dev/null +++ b/c,cc/padding.cc @@ -0,0 +1,32 @@ +#include <iostream> +#include <string> + +using namespace std; + +struct foo { + int a; + string b; + string b1; + int c; + string b2; + string b3; +}; + +struct bar { + int a; + int c; + string b; + string b1; + string b2; + string b3; +}; + +int +main(void) +{ + cout << sizeof(foo) << endl; + cout << sizeof(bar) << endl; + + return 0; +} + diff --git a/c,cc/pairs_stringstream.cc b/c,cc/pairs_stringstream.cc new file mode 100644 index 0000000..300d55e --- /dev/null +++ b/c,cc/pairs_stringstream.cc @@ -0,0 +1,22 @@ +#include <iostream> +#include <vector> +#include <string> +#include <sstream> + +using namespace std; + +int +main(void) +{ + istringstream ss("ufllffrrf"); + ss.seekg(1); + while (ss.good()) { + char prev, cur; + ss >> prev; + ss >> cur; + cout << prev << " " << cur << endl; + } + + return 0; +} + diff --git a/c,cc/pointer_to_element.cc b/c,cc/pointer_to_element.cc new file mode 100644 index 0000000..5a11919 --- /dev/null +++ b/c,cc/pointer_to_element.cc @@ -0,0 +1,19 @@ +#include <vector> +#include <iostream> + +using namespace std; + +int +main(void) +{ + vector<char> bla; + bla.reserve(2); + bla.push_back('a'); + char* p = &bla.at(0); + cout << *p << endl; + bla.push_back('b'); + cout << *p << endl; + + return 0; +} + diff --git a/c,cc/pointers.c b/c,cc/pointers.c new file mode 100644 index 0000000..1efc25e --- /dev/null +++ b/c,cc/pointers.c @@ -0,0 +1,46 @@ +#include <stdio.h> + + +void +test1(void) +{ + char *sp; + char s[] = "str"; + sp = s; + printf("sp: %s\n", sp); + printf("s: %s\n", s); + + int a = 513; + int *ap = &a; + int **app = ≈ + printf("a: %d\n", a); + printf("ap: %d\n", *ap); + printf("app: %d\n", **app); +} + +void +test2() +{ + char *str = "abc"; + + printf("for:\n"); + for (unsigned int i = 0; i < 3; i += 1) + { + printf("%c\n", str[i]); + } + + printf("---\nwhile:\n"); + while(*str) { + printf("%c\n", *str++); + } + +} + +int main(void) +{ + test1(); + test2(); + + return 0; +} + diff --git a/c,cc/precedence.cc b/c,cc/precedence.cc new file mode 100644 index 0000000..08e29b6 --- /dev/null +++ b/c,cc/precedence.cc @@ -0,0 +1,16 @@ +#include <iostream> + +using namespace std; + + +int main(void) +{ + bool a = false; + bool b = true; + + cout << (!a && b) << endl; + + double c = 0; + if (c) cout << "xx" << endl; +} + diff --git a/c,cc/rand.cc b/c,cc/rand.cc new file mode 100644 index 0000000..6be97a6 --- /dev/null +++ b/c,cc/rand.cc @@ -0,0 +1,14 @@ +#include <iostream> +#include <random> + +int main() +{ + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution<> dis(2, 3); + + for (int n=0; n<10; ++n) + std::cout << dis(gen) << ' '; + std::cout << '\n'; +} + diff --git a/c,cc/reset_stringstream.cc b/c,cc/reset_stringstream.cc new file mode 100644 index 0000000..474f014 --- /dev/null +++ b/c,cc/reset_stringstream.cc @@ -0,0 +1,25 @@ +#include <string> +#include <iostream> +#include <sstream> + +using namespace std; + +int +main(void) +{ + string s("X,1"); + string t("1"); + int i; + istringstream ss(s); + if (ss >> i) { + cout << i << endl; + } else { + ss.clear(); + string buf; + while(ss.good() && getline(ss,buf,',')) + cout << buf << endl; + } + + return 0; +} + diff --git a/c,cc/reuse_stringstream.cc b/c,cc/reuse_stringstream.cc new file mode 100644 index 0000000..f214544 --- /dev/null +++ b/c,cc/reuse_stringstream.cc @@ -0,0 +1,23 @@ +#include <sstream> +#include <string> +#include <iostream> + +using namespace std; + +int +main(void) +{ + ostringstream ss; + for (int i = 0; i < 5; i++) { + ss.str(string()); + ss << "asdf"; + ss << i; + ss << i; + ss << i; + ss << i; + cout << ss.str() << endl; + } + + return 0; +} + diff --git a/c,cc/rmtag.cc b/c,cc/rmtag.cc new file mode 100644 index 0000000..bdcc8e6 --- /dev/null +++ b/c,cc/rmtag.cc @@ -0,0 +1,22 @@ +/* + * rmtag.cpp + * + * Patrick Simianer <p@simianer.de> + * 2015-06 + */ + +#include <iostream> +#include <string> + +using namespace std; + +int main(int argc, char const* argv[]) +{ + string s = "<a> b </a>"; + cout << s << endl; + cout << s.find_first_of(">") << endl; + cout << s.find_last_of("<") << endl; + cout << s.substr(s.find_first_of(">")+1, s.find_last_of("<")-3) << endl; + return 0; +} + diff --git a/c,cc/scanf.c b/c,cc/scanf.c new file mode 100644 index 0000000..21dda28 --- /dev/null +++ b/c,cc/scanf.c @@ -0,0 +1,17 @@ +#include <stdio.h> + + +int main (void) +{ + char str[80]; + int res; + + res = scanf(" %s", str); + printf("%d %s\n", res , str); + res = scanf("%s ", str); + printf("%d %s", res, str); + res = scanf(" %s", str); + printf("%d %s\n", res, str); + return 0; +} + diff --git a/c,cc/self_referencing_struct.cc b/c,cc/self_referencing_struct.cc new file mode 100644 index 0000000..c084164 --- /dev/null +++ b/c,cc/self_referencing_struct.cc @@ -0,0 +1,19 @@ +#include <iostream> +#include <vector> + +using namespace std; + +struct S { + int m; + vector<S> v; +}; + +int +main(void) +{ + S a; + S b; + a.v.push_back(b); + return 0; +} + diff --git a/c,cc/shared_ptr.cc b/c,cc/shared_ptr.cc new file mode 100644 index 0000000..4fb158e --- /dev/null +++ b/c,cc/shared_ptr.cc @@ -0,0 +1,31 @@ +#include <iostream> +#include <memory> + +using namespace std; + +struct B { + B(int i) : m(i) {} + int m; +}; + +struct A { + shared_ptr<B> b; +}; + +int +main(void) +{ + shared_ptr<B> x(new B(42)); + A* a = new A; + A* c = new A; + a->b = x; + c->b = x; + cout << x.use_count() << endl; + delete a; + cout << x.use_count() << endl; + delete c; + cout << x.use_count() << endl; + + return 0; +} + diff --git a/c,cc/slicing.cc b/c,cc/slicing.cc new file mode 100644 index 0000000..c89d99d --- /dev/null +++ b/c,cc/slicing.cc @@ -0,0 +1,43 @@ +#include <iostream> +#include <string> +#include <vector> + +using namespace std; + +struct A { + public: + virtual string s() { return "a"; } +}; + +class B : public A { + public: + string s() { return "b"; } +}; + +B +make_b() +{ + B b; + return b; +} + +A +make_a() +{ + return make_b(); +} + +int +main(void) +{ + vector<A> x; + B b = make_b(); + x.push_back(b); + cout << x[0].s() << endl; + B c; + x.push_back(c); + cout << x[1].s() << endl; + + return 0; +} + diff --git a/c,cc/snprintf.c b/c,cc/snprintf.c new file mode 100644 index 0000000..a2cf1c2 --- /dev/null +++ b/c,cc/snprintf.c @@ -0,0 +1,14 @@ +#include <stdio.h> + + +int main(void) +{ + int foo[4] = {192, 168, 1, 1}; + char bla[20]; + + snprintf(bla, 19, "%d.%d.%d.%d", foo[0], foo[1], foo[2], foo[3]); + + printf("%s\n", bla); + + return 0; +} diff --git a/c,cc/sort.cc b/c,cc/sort.cc new file mode 100644 index 0000000..165b086 --- /dev/null +++ b/c,cc/sort.cc @@ -0,0 +1,47 @@ +#include <iostream> +#include <algorithm> +#include <utility> +#include <vector> + +using namespace std; + + +struct X +{ + pair<string,int> p; +}; + +bool myf(X a, X b) +{ + return a.p.second > b.p.second; +} + +int main(void) +{ + X a; + a.p.first = "a"; + a.p.second = 1; + + X b; + b.p.first = "b"; + b.p.second = 2; + + X c; + c.p.first = "c"; + c.p.second = 3; + + vector<X> v; + v.push_back(a); + v.push_back(b); + v.push_back(c); + + for (unsigned i = 0; i < v.size(); i++) { + cout << v[i].p.first << endl; + } + sort(v.begin(), v.end(), myf); + cout << endl; + for (unsigned i = 0; i < v.size(); i++) { + cout << v[i].p.first << endl; + } +} + diff --git a/c,cc/sort2.cc b/c,cc/sort2.cc new file mode 100644 index 0000000..3d64952 --- /dev/null +++ b/c,cc/sort2.cc @@ -0,0 +1,62 @@ +#include <iostream> +#include <algorithm> +#include <utility> +#include <vector> + +using namespace std; + + +struct X +{ + string n; + int a; + int b; +}; + +bool cmph(X a, X b) +{ + return (a.a+a.b) > (b.a+b.b); +} + +bool cmpf(X a, X b) +{ + return (a.a-a.b) > (b.a-b.b); +} + +int main(void) +{ + X a; + a.n = "a"; + a.a = 1; + a.b = 2; + + X b; + b.n = "b"; + b.a = 2; + b.b = 0; + + X c; + c.n = "c"; + c.a = 4; + c.b = -1; + + vector<X> v; + v.push_back(a); + v.push_back(b); + v.push_back(c); + + for (unsigned i = 0; i < v.size(); i++) { + cout << v[i].n << " " << v[i].a << " " << v[i].b << endl; + } + sort(v.begin(), v.end(), cmph); + cout << endl; + for (unsigned i = 0; i < v.size(); i++) { + cout << v[i].n << " " << v[i].a << " " << v[i].b << endl; + } + sort(v.begin(), v.end(), cmpf); + cout << endl; + for (unsigned i = 0; i < v.size(); i++) { + cout << v[i].n << " " << v[i].a << " " << v[i].b << endl; + } +} + diff --git a/c,cc/split.cc b/c,cc/split.cc new file mode 100644 index 0000000..e01ad64 --- /dev/null +++ b/c,cc/split.cc @@ -0,0 +1,55 @@ +#include <iostream> +#include <string> +#include <sstream> +#include <vector> + +using namespace std; + + +int main(void) +{ + string s("a\tb\tc\td"); + string::iterator it = s.begin(); + char d = '\t'; + string tmp; + size_t parts = 4; + size_t c = 0; + while(true) { + if (parts > 0 && c == parts-1) { + while(it != s.end()) { + tmp += *it; + it++; + } + cout << tmp << endl; + break; + } + if (it == s.end()) { cout << tmp << endl; break; } + if (*it != d) tmp += *it; + else { + cout << tmp << endl; + tmp.clear(); + c++; + } + it++; + } + + cout << "---" << endl; + + stringstream ss(s); + string si; + parts = 0; + c = 0; + + while(true) + { + if (parts > 0 && c == parts-1) { + getline(ss, si); + cout << si << endl; + break; + } + if(!getline(ss, si, '\t')) break; + cout << si << endl; + c++; + } +} + diff --git a/c,cc/sscanf.c b/c,cc/sscanf.c new file mode 100644 index 0000000..221852e --- /dev/null +++ b/c,cc/sscanf.c @@ -0,0 +1,17 @@ +#include <stdio.h> + + +int main(void) +{ + char ip[15] = {"192.168.100.100"}; + int retval, val0, val1, val2, val3; + + if(4!=(retval=sscanf(ip, "%d.%d.%d.%d", &val0, &val1, &val2, &val3))) { + printf("Error: sscanf failed, retval was: %d\n", retval); + } + else { + printf("%d\n%d\n%d\n%d\n", val0, val1, val2, val3); + } + + return 0; +} diff --git a/c,cc/stringstream.cc b/c,cc/stringstream.cc new file mode 100644 index 0000000..3d68db3 --- /dev/null +++ b/c,cc/stringstream.cc @@ -0,0 +1,26 @@ +#include <iostream> +#include <string> +#include <sstream> +#include <vector> +#include <stdio.h> +#include <iomanip> + +using namespace std; + + +int main(void) { + string s = "1 2 3 4"; + int i; + vector<int> j; + stringstream ss(s); + while (ss >> i) { + j.push_back(i); + cout << i << endl; + } + + printf("%4d %4d %4d %4d\n", j[0], j[1], j[2], j[3]); + + cout.width(100); + cout << setw(100) << j.size() << endl; +} + diff --git a/c,cc/stringutil.c b/c,cc/stringutil.c new file mode 100644 index 0000000..aab4f03 --- /dev/null +++ b/c,cc/stringutil.c @@ -0,0 +1,22 @@ +#include "stringutil.h" + + +char* +strend(char* s) +{ + if (strlen(s) == 0) return '\0'; + while(*s != '\0') + ++s; + return --s; +} + +bool +endswith(char* s, char* suff) +{ + if (strlen(s) < strlen(suff)) return false; + char* a = strend(s)-(strlen(suff)-1); + if (!a) return false; + if (!strcmp(a, suff)) return true; + return false; +} + diff --git a/c,cc/stringutil.h b/c,cc/stringutil.h new file mode 100644 index 0000000..153068a --- /dev/null +++ b/c,cc/stringutil.h @@ -0,0 +1,13 @@ +#ifndef STRINGUTIL_H +#define STRINGUTIL_H + +#include <string.h> +#include <stdbool.h> + + +char* strend(char* s); +bool endswith(char* s, char* suff); + + +#endif + diff --git a/c,cc/stringutil_test.c b/c,cc/stringutil_test.c new file mode 100644 index 0000000..798bccd --- /dev/null +++ b/c,cc/stringutil_test.c @@ -0,0 +1,53 @@ +#include "filelib.h" +#include "stringutil.h" + + +int +main(void) +{ + /* + * strend + * + */ + char* s= ""; + char* end = strend(s); + if (end) + printf("should not output: %s\n", end); + + s = "asdf.gz"; + end = strend(s); + if (end) + printf("expect 'z': %s\n", end); + + /* + * endswith + * + */ + if (endswith(s, ".gz")) + printf("%s ends with %s\n", s, ".gz"); + + if (endswith(".gz", ".gz")) + printf(".gz ends with .gz\n"); + + if(!endswith("gz", ".gz")) + printf("gz does not end with .gz\n"); + + /* + * filelib + * + */ + hFile* f = makef("-", "a"); + if (!f) + printf("Error creating file '%s'\n", "-"); + writeln("this goes to stdout", f); + closef(f); + + hFile* g = makef("gzfile.gz", "a"); + if (!g) + printf("Error creating file '%s'\n", "-"); + writeln("this should be compressed", g); + closef(g); + + return 0; +} + diff --git a/c,cc/struct.c b/c,cc/struct.c new file mode 100644 index 0000000..4111526 --- /dev/null +++ b/c,cc/struct.c @@ -0,0 +1,20 @@ +#include <stdio.h> + + +struct bla { + int a, b; +}; + +int main(void) +{ + struct bla test, *ptest; + ptest = &test; + + ptest->a = 3; + ptest->b = 5; + + printf("%d-%d\n", ptest->a, ptest->b); + + return 0; +} + diff --git a/c,cc/struct_init.cc b/c,cc/struct_init.cc new file mode 100644 index 0000000..e54724a --- /dev/null +++ b/c,cc/struct_init.cc @@ -0,0 +1,33 @@ +#include <iostream> +#include <utility> +#include <vector> + +using namespace std; + +struct POD { + POD() { a = 2; } + POD(int x) { a = x; } + int a; +}; + +int +main(void) +{ + vector<pair<char,char> > a; + pair<char,char> p = make_pair('a','b'); + a.push_back(p); + vector<pair<char, char> > b(a); + cout << b[0].first << endl; + b[0].first = 'c'; + cout << b[0].first << endl; + + vector<POD> w; + POD x; x.a=3; + w.push_back(x); + POD y(x); + vector<POD> q(w); + cout << q[0].a << endl; + + return 0; +} + diff --git a/c,cc/substr.cc b/c,cc/substr.cc new file mode 100644 index 0000000..9a91adc --- /dev/null +++ b/c,cc/substr.cc @@ -0,0 +1,23 @@ +#include <iostream> +#include <string> + +using namespace std; + + +int main(void) +{ + // 0 12 34 56 + string s("aaax\tbx\tcxasd\tdx"); + unsigned f = 0; + for(unsigned i = 0; i < 3; i++) { + unsigned e = f; + f = s.find("\t", f+1); + cout << "e:" << e << " f:" << f << endl; + if (e !=0) cout << "'" << s.substr(e+1, f-e-1) << "'" << endl; + else cout << "'" << s.substr(0, f) << "'" << endl; + } + cout << "---" << endl; + s.erase(0, f+1); + cout << "'" << s << "'" <<endl; +} + diff --git a/c,cc/thread_test.c b/c,cc/thread_test.c new file mode 100644 index 0000000..e1ba9f3 --- /dev/null +++ b/c,cc/thread_test.c @@ -0,0 +1,43 @@ +#include <stdio.h> +#include <pthread.h> +#include <stdlib.h> + + +void saying_pthread_name(void* ptr); +int value=1; + +int +main(void) +{ + pthread_t wthread, bthread, rthread; + char* wname = "white"; + char* bname = "black"; + char* rname = "red"; + pthread_attr_t* pthread_attr_default = NULL; + + pthread_create(&wthread, pthread_attr_default, + (void*)&saying_pthread_name, (void*) wname); + + pthread_create(&bthread, pthread_attr_default, + (void*)&saying_pthread_name, (void*) bname); + + pthread_create(&rthread, pthread_attr_default, + (void*)&saying_pthread_name, (void*) rname); + + pthread_join(wthread,NULL); + //printf("%d\n", value); + pthread_join(rthread,NULL); + pthread_join(bthread,NULL); + + exit(0); +} + +void +saying_pthread_name(void* ptr) +{ + char* message; + message = (char*)ptr; + printf("I am a %s pthread and the value is %d \n", message, value); + value = value+1; +} + diff --git a/c,cc/to_hex.cc b/c,cc/to_hex.cc new file mode 100644 index 0000000..6e577dc --- /dev/null +++ b/c,cc/to_hex.cc @@ -0,0 +1,14 @@ +#include <iostream> + +using namespace std; + + +int main(void) +{ + int r = 116; + int g = 10; + int b = 10; + + cout << hex << r << " " << g << " " << b << endl; +} + diff --git a/c,cc/type_max.cc b/c,cc/type_max.cc new file mode 100644 index 0000000..5f59313 --- /dev/null +++ b/c,cc/type_max.cc @@ -0,0 +1,20 @@ +/* + * template.cpp + * + * Patrick Simianer <p@simianer.de> + * YYYY-MM-DD + */ + +#include <iostream> +#include <limits> + +using namespace std; + + +int main(int argc, char const* argv[]) +{ + cout << std::numeric_limits<int>::max() << endl; + + return 0; +} + diff --git a/c,cc/type_size.c b/c,cc/type_size.c new file mode 100644 index 0000000..18edb1f --- /dev/null +++ b/c,cc/type_size.c @@ -0,0 +1,12 @@ +#include <stdio.h> + + +int main(void) +{ + printf("char: %d\n", sizeof(char)); + printf("int: %d\n", sizeof(int)); + printf("float: %d\n", sizeof(float)); + printf("double: %d\n", sizeof(double)); + return 0; +} + diff --git a/c,cc/vector_addition.c b/c,cc/vector_addition.c new file mode 100644 index 0000000..decb3ff --- /dev/null +++ b/c,cc/vector_addition.c @@ -0,0 +1,29 @@ +#include "stdio.h" +#include "stdlib.h" + + +int main(void) { + int N = 10000, M=100000; + float **x = malloc(N*sizeof(float*)); + float *y = malloc(M*sizeof(float*)); + int i,j; + for (i = 0; i < N; i++) { + x[i] = malloc(M*sizeof(float)); + for (j = 0; j < M; j++) { + x[i][j] = (float) j; + } + } + + for (i = 0; i < N ; i++) { + for (j = 0; j < M; j++) { + y[i] += x[i][j]; + } + } + + printf("%f\n", y[100]); + + free(x); free(y); + + return 0; +} + diff --git a/c,cc/vector_addition.cu b/c,cc/vector_addition.cu new file mode 100644 index 0000000..4f16bc3 --- /dev/null +++ b/c,cc/vector_addition.cu @@ -0,0 +1,61 @@ +#include "stdio.h" + + +__global__ void add_arrays_gpu( float *in1, float *in2, float *out, int Ntot) +{ + int idx=blockIdx.x*blockDim.x+threadIdx.x; + if (idx < Ntot) + out[idx]=in1[idx]+in2[idx]; +} + + +int main(void) +{ + /* pointers to host memory */ + float *a, *b, *c; + /* pointers to device memory */ + float *a_d, *b_d, *c_d; + int N=100000000; + int i; + + /* Allocate arrays a, b and c on host*/ + a = (float*) malloc(N*sizeof(float)); + b = (float*) malloc(N*sizeof(float)); + c = (float*) malloc(N*sizeof(float)); + + /* Allocate arrays a_d, b_d and c_d on device*/ + cudaMalloc ((void **) &a_d, sizeof(float)*N); + cudaMalloc ((void **) &b_d, sizeof(float)*N); + cudaMalloc ((void **) &c_d, sizeof(float)*N); + + /* Initialize arrays a and b */ + for (i=0; i<N; i++) { + a[i]= (float) i; + b[i]=(float) -i; + } + + + /* Copy data from host memory to device memory */ + cudaMemcpy(a_d, a, sizeof(float)*N, cudaMemcpyHostToDevice); + cudaMemcpy(b_d, b, sizeof(float)*N, cudaMemcpyHostToDevice); + + /* Compute the execution configuration */ + int block_size=256; + dim3 dimBlock(block_size); + dim3 dimGrid ( (N/dimBlock.x) + (!(N%dimBlock.x)?0:1) ); + + /* Add arrays a and b, store result in c */ + add_arrays_gpu<<<dimGrid,dimBlock>>>(a_d, b_d, c_d, N); + + /* Copy data from deveice memory to host memory */ + //cudaMemcpy(c, c_d, sizeof(float)*N, cudaMemcpyDeviceToHost); + + /* Print c */ +/*for(i=0; i<N; i++) + printf(" c[%d]=%f\n",i,c[i]);*/ + + /* Free the memory */ + free(a); free(b); free(c); + cudaFree(a_d); cudaFree(b_d);cudaFree(c_d); +} + diff --git a/c,cc/vector_addition1.cu b/c,cc/vector_addition1.cu new file mode 100644 index 0000000..9490ff3 --- /dev/null +++ b/c,cc/vector_addition1.cu @@ -0,0 +1,48 @@ +#include "stdio.h" + + +__global__ void add_arrays_gpu( float *in1, float *in2, float *out, int Ntot) +{ + int idx=blockIdx.x*blockDim.x+threadIdx.x; + if (idx < Ntot) + out[idx]=in1[idx]+in2[idx]; +} + + +int main(void) +{ + int N=10000, M=100000; + float **x = (float**) malloc(N*sizeof(float)); + float **x_d = (float**) malloc(N*sizeof(float)); + int i,j; + + for (i = 0; i < N; i++) { + x[i] = (float*) malloc(M*sizeof(float)); + cudaMalloc ((void **) &x_d[i], sizeof(float)*M); + for (j = 0; j < M; j++) { + x[i][j] = (float) j; + } + cudaMemcpy(x_d[i], x[i], sizeof(float)*M, cudaMemcpyHostToDevice); + } + + + /* Compute the execution configuration */ +// int block_size=256; + // dim3 dimBlock(block_size); + //dim3 dimGrid ( (N/dimBlock.x) + (!(N%dimBlock.x)?0:1) ); + + /** Add arrays a and b, store result in c */ + // add_arrays_gpu<<<dimGrid,dimBlock>>>(a_d, b_d, c_d, N); + + /* Copy data from deveice memory to host memory */ + //cudaMemcpy(c, c_d, sizeof(float)*N, cudaMemcpyDeviceToHost); + + /* Print c */ +/*for(i=0; i<N; i++) + printf(" c[%d]=%f\n",i,c[i]);*/ + + /* Free the memory */ +// free(a); free(b); free(c); +// cudaFree(a_d); cudaFree(b_d);cudaFree(c_d); +} + diff --git a/c,cc/vector_back.cc b/c,cc/vector_back.cc new file mode 100644 index 0000000..cbc82f5 --- /dev/null +++ b/c,cc/vector_back.cc @@ -0,0 +1,24 @@ +#include <iostream> +#include <vector> + +using namespace std; + +struct Foo { + int i; + char c; +}; + +int +main(void) +{ + vector<Foo> foo; + foo.push_back(Foo()); + foo.back().i = 3; + foo.back().c = 'c'; + + for (vector<Foo>::iterator it = foo.begin(); it != foo.end(); it++) + cout << it->i << " " << it->c << endl; + + return 0; +} + diff --git a/c,cc/vector_move.cc b/c,cc/vector_move.cc new file mode 100644 index 0000000..1167270 --- /dev/null +++ b/c,cc/vector_move.cc @@ -0,0 +1,34 @@ +#include <iostream> +#include <vector> +#include <algorithm> +#include <iterator> + +using namespace std; + + +struct A { + int d; + + A(int i) : d(i) {} + + A(A&& a) : d(a.d) { a.d = 0; } +}; + +int +main(void) +{ + vector<A> a; + a.emplace_back(1); + a.emplace_back(1); + a.emplace_back(1); + vector<A> b; + b.emplace_back(2); + + cout << a.size() << endl; + move(b.begin(), b.end(), back_inserter(a)); + cout << a.size() << endl; + cout << b[0].d << endl; + + return 0; +} + diff --git a/c,cc/vector_pointer.cc b/c,cc/vector_pointer.cc new file mode 100644 index 0000000..70251e6 --- /dev/null +++ b/c,cc/vector_pointer.cc @@ -0,0 +1,19 @@ +#include <vector> +#include <iostream> + +using namespace std; + +int main(void) +{ + vector<char> x; + x.push_back('a'); + x.push_back('b'); + x.push_back('c'); + char* p = &(x[1]); + cout << *p << endl; + x[1] = 'x'; + cout << *p << endl; + + return 0; +} + |