Last active
January 12, 2020 23:05
-
-
Save jmlyn/d20c1e8c7f28077b04ca2482ee9796e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Main.cpp : This file contains the 'main' function. Program execution begins and ends there. | |
| // | |
| #include "pch.h" | |
| #include <iostream> | |
| #include <memory> | |
| #include <crtdbg.h> | |
| #include "DynamicArray.hpp" | |
| // Typically using a smart pointer with a c-style array is not the best idea. If you can use std::vector or std::array in normal production | |
| // code, that's usually the more effective choice. | |
| // However, sometimes we want to use a smart pointer on a raw array for really ad-hoc code (such as educational implementations of things | |
| // where STL is not permitted) and it becomes somewhat worthwhile to use a smart pointer for these arrays. I don't know. Maybe just calling delete[] on your | |
| // array at some point is less of a hassle than knowing the templated conversations of unique_ptr for whether or not something is an array on the stack, | |
| // an array allocated via a pointer type, or some other edge case. | |
| // These are just my notes on what's considered legal for these smart pointers. There's no guarantee on any of this being useful. | |
| int main() | |
| { | |
| _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); | |
| // int container[] = new int[3]; // Illegal because an array on the stack cannot point to a pointer of dynamically allocated data | |
| int container1[] = { 1, 3, 5 }; | |
| int *container2 = new int[3]{ 1, 3, 5 }; | |
| int *container3 = new int[3]; | |
| //std::cout << container2[2] << std::endl; // Prints '5' | |
| // std::unique_ptr<int[]> container4 = container3; // Illegal because cannot convert from int * to std::unique_ptr<int[3], std::default_delete<int [3]>> | |
| // std::unique_ptr<int[]> container5 = container1; // Illegal, cannot convert from int[3] to std::unique_ptr<int[3], std::default_delete<int [3]>> | |
| // std::unique_ptr<int[3]> container7 = container1; // Illegal, cannot convert from int[3] to std::unique_ptr<int[3], std::default_delete<int [3]>> | |
| // std::unique_ptr<int[]> container8 = std::unique_ptr<int[], std::default_delete<int[]>>(container1); // Throws an exception because array is on the stack and delete[] | |
| // cannot be called onto it. | |
| // std::unique_ptr<int[]> container6 = container2; // Illegal because you need the explicit conversion to the templated unique_ptr type that specifies the deletion function | |
| std::unique_ptr<int[]> container9 = std::unique_ptr<int[], std::default_delete<int[]>>(container2); // This works! | |
| std::unique_ptr<int[]> container9 = std::unique_ptr<int[], std::default_delete<int[]>>(container3); // This works! | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment