Skip to content

Commit

Permalink
vector
Browse files Browse the repository at this point in the history
fix const char issue
  • Loading branch information
tnut committed May 2, 2024
1 parent 88f21d2 commit 659f31a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/vector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ vector::vector(char * element) {
m_size = 1;
m_items[0] = strdup(element);
}
void vector::push_back(char * element) {
void vector::push_back(const char * element) {
if ( m_capacity == m_size) {
m_capacity = m_size * 2;
m_items = (char **)realloc( m_items, sizeof m_items * m_capacity );
Expand Down Expand Up @@ -46,6 +46,11 @@ vector::~vector(){
m_items[i] = nullptr;
}
}
if (m_items != nullptr) {
free(m_items);
m_items = nullptr;
}

}
} // end of cards namespace

Expand Down
4 changes: 2 additions & 2 deletions src/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class vector
~vector();

const char* value(const unsigned int index);
void push_back(char * element);
void push_back(const char* element);
const unsigned int size();
const unsigned int capacity();


private:
char **m_items;
char** m_items;
unsigned int m_size;
unsigned int m_capacity;
};
Expand Down
10 changes: 10 additions & 0 deletions tests/vector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,15 @@ int main() {

printf("First Value: %s\n",myVector.value(0));
printf("Last Value: %s\n",myVector.value(myVector.size()));

cards::vector Vector;
Vector.push_back("hello");
Vector.push_back("how ");
Vector.push_back("are");
Vector.push_back("you");
Vector.push_back("?");
printf("First Value: %s\n",Vector.value(0));
printf("Last Value: %s\n",Vector.value(Vector.size()-1));

return 0;
}

0 comments on commit 659f31a

Please sign in to comment.