-4

I'm looking for a solution that how can I find the one dimension array which does not contain any elements. I had done so far some following codes which pretty code, but it will be good if there is any other solution for it.

Code_01

#include <iostream> 
#include <list> 
using namespace std; 

int main() 
{ 
    list<int> mylist{}; 
    if (mylist.empty()) { 
        cout << "True"; 
    } 
    else { 
        cout << "False"; 
    } 
    return 0; 
}

Code_03

#include <iostream> 
#include <vector> 
using namespace std; 

int main() 
{ 
    vector<int> myvector{}; 
    if (myvector.empty()) { 
        cout << "True"; 
    } 
    else { 
        cout << "False"; 
    } 
    return 0; 
} 
  • 12
    Arrays are *never* empty. An array of `50` elements will *always* have `50` elements. Some of them might not be initialized, but the size is fixed at compilation. – Some programmer dude Oct 02 '18 at 06:41
  • 7
    Furthermore, `int mylist{5};` defines a *single* `int` variable an initializes it to the value `5`. It seems to me like you could use [a few good books to read](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Oct 02 '18 at 06:42
  • 1
    You'll likely need to have some sort of canary value, a marker that can't exist in the array, that you can test for to detect an initialized-but-empty array. This is the same trick used by nul-terminated strings. – user4581301 Oct 02 '18 at 06:43
  • `Code_01` compiles as provided here? I doubt that. – Werner Henze Oct 02 '18 at 07:00
  • 1
    You are showing solutions, not problems. What exactly do you want to achieve? What is the context? Are you talking about dynamically sized arrays? So not about C style arrays, not `std::array`, but `std::vector`? – Werner Henze Oct 02 '18 at 07:03
  • @WernerHenze Actually I want to store the complete array into an available array. And what kind of doubt you have in `Code_01`! –  Oct 02 '18 at 07:26
  • code_01 produces `error: member reference base type 'int' is not a structure or union` – Thomas Sablik Oct 02 '18 at 07:27
  • In code_02 `mypointer` is the address of the array `myarray` and also the address of the first element. That address will never be zero so the output of code_02 always is "Not empty" – Thomas Sablik Oct 02 '18 at 07:30
  • I don't get your question. If you want to check if a STL container is empty, use the `empty` method of that container. You can't check, if an array is empty because an array can't be empty. An array has fixed size. – Thomas Sablik Oct 02 '18 at 07:34
  • Ok, thank you so much! I have understood what should I have to do now! –  Oct 02 '18 at 07:49
  • Your usage of the name "array" is not clear enough. Do you mean C style static size arrays or the general array concept with dynamic size arrays? – Werner Henze Oct 02 '18 at 08:28
  • @WernerHenze Yes –  Oct 02 '18 at 08:29

2 Answers2

2

The reference for std::empty on cppreference answers your question:

Returns whether the given container is empty.

1) returns c.empty()

2) returns false

3) returns il.size() == 0

Parameters

c - a container with an empty method

array - an array of arbitrary type

il - an initializer list

That means, that an array will never be empty. An array has fixed size.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
0

Did you tried to compile those code snippets?

The first one will surely fail. You are referencing a built-in type (int) as if it's an object, which is not.

The second one has similar problem (calling a built-in type as an object), though you might be able to get away by calling std::empty(myarray). Please note that the lines where you define a pointer to int and then assign mypointer=myarray are useless: as an array name is a pointer to the first element, you are simply creating a copy of it that you aren't going to modify. So it's wasted memory. Also it is advisable to use nullptr or at least the macro NULL when dealing with null pointers, as they are easier to spot in a lot of code and less prone to casual modifications. Last but not least, the if check isn't going to fail ever, since the pointer will always contain an address. Back to possible solutions, I still think std::empty() it's going to fail during compilation since, skimming through his documentation, it doesn't seem to be able to take an array as an argument, which makes sense, since array are never ever empty, they can at most contain uninitialized values.

The third snippet will compile and work as intended, thanks to how vectors are builded.

I don't know if it's possible, but you may want to look into the array object documentation if you have fixed data size and want to avoid the (small) overhead of vector.

Andrea Bocco
  • 792
  • 5
  • 14