Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I think your ~vector() destructor is doing the wrong thing:

  • It should free the memory which you allocated using calloc
  • It shouldn't memset over the objects contained in the array
  • My guess is that it should explicitly destroy the objects in the array, using the opposite of placement new ... perhaps by doing something like:
~vector
{
 for (iterator i = begin(); i != end(); ++i)
 i->~_Ty();
 free(__data);
}

Similarly the methods which create an object in the memory should use placement new, for example as described here.

The vector you have at the moment probably works for types which have a trivial/non-existent constructor and destructor (for example int), but not for types which have a non-default constructor and destructor (for example std::string).


Your operator== should be const (so that it can be used when *this is const).

I suspect it shouldn't include the typeid(_Value_type) != typeid(_Ty1) expression: instead it shouldn't define the _Ty1 template parameter, so that it can only be used to compare vectors which have an identical type.

This suggests you code it as a free operator (an inline functions which takes two parameters), instead of as a method.


Your find method should return end(), not NULL, if it fails to find.


I suspect it's not safe for your resize to call realloc. For example, I might have a type like this ...

class A
{
 A* self;
public:
 A() { self = this; }
 A(const A&) { ... to be supplied ... }
 const A& operator=(const A&) { ... to be supplied ... }
 A(A&&) { ... to be supplied ... }
 A& operator=(A&&) { ... to be supplied ... }
}

... which remembers where itself exists in memory. If you call realloc that will do a bitwise copy of existing elements, which will cause the moved instance of A to have invalid self pointers.

Instead you may need to create a new buffer, and explicitly move all existing instances from the old buffer into their new locations in the new buffer.


Your identifiers shouldn't begin with an _ underscore. Those identifiers are reserved for the compiler and/or the standard run-time library reserved for the compiler and/or the standard run-time library.


Your insert method doesn't make much sense: its signature doesn't match any of the three standard insert methods.

I think your ~vector() destructor is doing the wrong thing:

  • It should free the memory which you allocated using calloc
  • It shouldn't memset over the objects contained in the array
  • My guess is that it should explicitly destroy the objects in the array, using the opposite of placement new ... perhaps by doing something like:
~vector
{
 for (iterator i = begin(); i != end(); ++i)
 i->~_Ty();
 free(__data);
}

Similarly the methods which create an object in the memory should use placement new, for example as described here.

The vector you have at the moment probably works for types which have a trivial/non-existent constructor and destructor (for example int), but not for types which have a non-default constructor and destructor (for example std::string).


Your operator== should be const (so that it can be used when *this is const).

I suspect it shouldn't include the typeid(_Value_type) != typeid(_Ty1) expression: instead it shouldn't define the _Ty1 template parameter, so that it can only be used to compare vectors which have an identical type.

This suggests you code it as a free operator (an inline functions which takes two parameters), instead of as a method.


Your find method should return end(), not NULL, if it fails to find.


I suspect it's not safe for your resize to call realloc. For example, I might have a type like this ...

class A
{
 A* self;
public:
 A() { self = this; }
 A(const A&) { ... to be supplied ... }
 const A& operator=(const A&) { ... to be supplied ... }
 A(A&&) { ... to be supplied ... }
 A& operator=(A&&) { ... to be supplied ... }
}

... which remembers where itself exists in memory. If you call realloc that will do a bitwise copy of existing elements, which will cause the moved instance of A to have invalid self pointers.

Instead you may need to create a new buffer, and explicitly move all existing instances from the old buffer into their new locations in the new buffer.


Your identifiers shouldn't begin with an _ underscore. Those identifiers are reserved for the compiler and/or the standard run-time library.


Your insert method doesn't make much sense: its signature doesn't match any of the three standard insert methods.

I think your ~vector() destructor is doing the wrong thing:

  • It should free the memory which you allocated using calloc
  • It shouldn't memset over the objects contained in the array
  • My guess is that it should explicitly destroy the objects in the array, using the opposite of placement new ... perhaps by doing something like:
~vector
{
 for (iterator i = begin(); i != end(); ++i)
 i->~_Ty();
 free(__data);
}

Similarly the methods which create an object in the memory should use placement new, for example as described here.

The vector you have at the moment probably works for types which have a trivial/non-existent constructor and destructor (for example int), but not for types which have a non-default constructor and destructor (for example std::string).


Your operator== should be const (so that it can be used when *this is const).

I suspect it shouldn't include the typeid(_Value_type) != typeid(_Ty1) expression: instead it shouldn't define the _Ty1 template parameter, so that it can only be used to compare vectors which have an identical type.

This suggests you code it as a free operator (an inline functions which takes two parameters), instead of as a method.


Your find method should return end(), not NULL, if it fails to find.


I suspect it's not safe for your resize to call realloc. For example, I might have a type like this ...

class A
{
 A* self;
public:
 A() { self = this; }
 A(const A&) { ... to be supplied ... }
 const A& operator=(const A&) { ... to be supplied ... }
 A(A&&) { ... to be supplied ... }
 A& operator=(A&&) { ... to be supplied ... }
}

... which remembers where itself exists in memory. If you call realloc that will do a bitwise copy of existing elements, which will cause the moved instance of A to have invalid self pointers.

Instead you may need to create a new buffer, and explicitly move all existing instances from the old buffer into their new locations in the new buffer.


Your identifiers shouldn't begin with an _ underscore. Those identifiers are reserved for the compiler and/or the standard run-time library.


Your insert method doesn't make much sense: its signature doesn't match any of the three standard insert methods.

merge answers
Source Link
ChrisW
  • 13k
  • 1
  • 35
  • 76

I think your ~vector() destructor is doing the wrong thing:

  • It should free the memory which you allocated using calloc
  • It shouldn't memset over the objects contained in the array
  • My guess is that it should explicitly destroy the objects in the array, using the opposite of placement new ... perhaps by doing something like:
~vector
{
 for (iterator i = begin(); i != end(); ++i)
 i->~_Ty();
 free(__data);
}

Similarly the methods which create an object in the memory should use placement new, for example as described here.

The vector you have at the moment probably works for types which have a trivial/non-existent constructor and destructor (for example int), but not for types which have a non-default constructor and destructor (for example std::string).


Your operator== should be const (so that it can be used when *this is const).

I suspect it shouldn't include the typeid(_Value_type) != typeid(_Ty1) expression: instead it shouldn't define the _Ty1 template parameter, so that it can only be used to compare vectors which have an identical type.

This suggests you code it as a free operator (an inline functions which takes two parameters), instead of as a method.


Your find method should return end(), not NULL, if it fails to find.


I suspect it's not safe for your resize to call realloc. For example, I might have a type like this ...

class A
{
 A* self;
public:
 A() { self = this; }
 A(const A&) { ... to be supplied ... }
 const A& operator=(const A&) { ... to be supplied ... }
 A(A&&) { ... to be supplied ... }
 A& operator=(A&&) { ... to be supplied ... }
}

... which remembers where itself exists in memory. If you call realloc that will do a bitwise copy of existing elements, which will cause the moved instance of A to have invalid self pointers.

Instead you may need to create a new buffer, and explicitly move all existing instances from the old buffer into their new locations in the new buffer.


Your identifiers shouldn't begin with an _ underscore. Those identifiers are reserved for the compiler and/or the standard run-time library .


Your insert method doesn't make much sense: its signature doesn't match any of the three standard insert methods .

I think your ~vector() destructor is doing the wrong thing:

  • It should free the memory which you allocated using calloc
  • It shouldn't memset over the objects contained in the array
  • My guess is that it should explicitly destroy the objects in the array, using the opposite of placement new ... perhaps by doing something like:
~vector
{
 for (iterator i = begin(); i != end(); ++i)
 i->~_Ty();
 free(__data);
}

Similarly the methods which create an object in the memory should use placement new, for example as described here.

The vector you have at the moment probably works for types which have a trivial/non-existent constructor and destructor (for example int), but not for types which have a non-default constructor and destructor (for example std::string).

I think your ~vector() destructor is doing the wrong thing:

  • It should free the memory which you allocated using calloc
  • It shouldn't memset over the objects contained in the array
  • My guess is that it should explicitly destroy the objects in the array, using the opposite of placement new ... perhaps by doing something like:
~vector
{
 for (iterator i = begin(); i != end(); ++i)
 i->~_Ty();
 free(__data);
}

Similarly the methods which create an object in the memory should use placement new, for example as described here.

The vector you have at the moment probably works for types which have a trivial/non-existent constructor and destructor (for example int), but not for types which have a non-default constructor and destructor (for example std::string).


Your operator== should be const (so that it can be used when *this is const).

I suspect it shouldn't include the typeid(_Value_type) != typeid(_Ty1) expression: instead it shouldn't define the _Ty1 template parameter, so that it can only be used to compare vectors which have an identical type.

This suggests you code it as a free operator (an inline functions which takes two parameters), instead of as a method.


Your find method should return end(), not NULL, if it fails to find.


I suspect it's not safe for your resize to call realloc. For example, I might have a type like this ...

class A
{
 A* self;
public:
 A() { self = this; }
 A(const A&) { ... to be supplied ... }
 const A& operator=(const A&) { ... to be supplied ... }
 A(A&&) { ... to be supplied ... }
 A& operator=(A&&) { ... to be supplied ... }
}

... which remembers where itself exists in memory. If you call realloc that will do a bitwise copy of existing elements, which will cause the moved instance of A to have invalid self pointers.

Instead you may need to create a new buffer, and explicitly move all existing instances from the old buffer into their new locations in the new buffer.


Your identifiers shouldn't begin with an _ underscore. Those identifiers are reserved for the compiler and/or the standard run-time library .


Your insert method doesn't make much sense: its signature doesn't match any of the three standard insert methods .

added 405 characters in body
Source Link
ChrisW
  • 13k
  • 1
  • 35
  • 76

I think your ~vector() destructor is doing the wrong thing:

  • It should free the memory which you allocated using calloc
  • It shouldn't memset over the objects contained in the array
  • My guess is that it should explicitly destroy the objects in the array, using the opposite of placement new ... perhaps by doing something like:
~vector
{
 for (iterator i = begin(); i != end(); ++i)
 i->~_Ty();
 free(__data);
}

Similarly the methods which create an object in the memory should use placement new, for example as described here .

The vector you have at the moment probably works for types which have a trivial/non-existent constructor and destructor (for example int), but not for types which have a non-default constructor and destructor (for example std::string).

I think your ~vector() destructor is doing the wrong thing:

  • It should free the memory which you allocated using calloc
  • It shouldn't memset over the objects contained in the array
  • My guess is that it should explicitly destroy the objects in the array, using the opposite of placement new ... perhaps by doing something like:
~vector
{
 for (iterator i = begin(); i != end(); ++i)
 i->~_Ty();
 free(__data);
}

I think your ~vector() destructor is doing the wrong thing:

  • It should free the memory which you allocated using calloc
  • It shouldn't memset over the objects contained in the array
  • My guess is that it should explicitly destroy the objects in the array, using the opposite of placement new ... perhaps by doing something like:
~vector
{
 for (iterator i = begin(); i != end(); ++i)
 i->~_Ty();
 free(__data);
}

Similarly the methods which create an object in the memory should use placement new, for example as described here .

The vector you have at the moment probably works for types which have a trivial/non-existent constructor and destructor (for example int), but not for types which have a non-default constructor and destructor (for example std::string).

Source Link
ChrisW
  • 13k
  • 1
  • 35
  • 76
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /