Initialize an object array in c++ with a special value - c++

I am an intermediate java programmer and I am used to relaying on the null value in java for cheking if objets are initialized with some reference to instanced object in memory. I want to do something similar in c++ but I do not have a clear idea about how I can achieve it. I want to initialize a user array - user is a class I have defined - so I can check if the actual position in the array does contain an object or it is free.
I have tried to use the null definition in c++ but found out that it is simply a "-1" int value and I could not use it properly. So basically I need something to distinguish between a free position in my array and an ocuppied one.
Additionally I might be interested in having an extra value to distinguish a position that contained a removed user, since I am planning to just mark the desired position with a special mark as a freed position when it comes to the method that remove a user from the array.
For the curious ones, I am implementing a simple hash set and the remove method in my class just mark the position of the element to remove instead of doing some restruction.

First of all null definition is not -1 but 0. Assuming you have something like
class User
{
};
User RemovedClass;
you can have something like this
User *arr[3];
arr[0] = new User();
arr[1] = 0;
arr[2] = &RemovedClass;
Here 0 is a new User, 1 is java null equivalent , 2 is marker for deleted class.
EDIT
Typically when you see User array in java you will have to map it to a User* array in C++.
After these operations.
User a;
User b;
a = b;
in java a and b will refer to the same User. In C++ such code will yield to a and b referring to two different User objects.

I think there are some concept you need to understand.
Unlike Java, when you are creating an N-element array of User, it is NOT an array of reference. It is really a piece of memory containing actual N user, which is already created and initialized (i.e. constructor already run). (Well, there are more advanced topics like placement-new but I think it is not really what you are looking for yet)
So it is somehow contradicting if you said you have an "array of User" and want to keep track if certain position is initialized.
Unless:
You are not creating array of User, but Array of "Pointer to User" (or other reference like auto_ptr). By such way, it is meaningful to say certain element is "null"; or
Your "initialize" do not mean creating an object instance, but an explicit initialization action (like executing an init() method of an User instance). By such way, it is meaningful to say certain element is "not initialized".
(First of all, as you are writing C++, I shall recommend you to use std::vector, not array. However I am using array in this answer, as to stick close to your question)
For case 1, it is strict forward, simply use NULL (avoid using 0, because, although NULL is defined as 0 in most system, using NULL actually makes the code more readable and more portable):
User* user[10] = {NULL}; // array of 10 pointer-to-User
user[0] = new User(...);
assert (user[0] != NULL);
assert (user[1] == NULL);
For case 2, you have many other choice, like keeping another array of boolean to keep the "initialized" flag, or have a wrapper over User for such extra flag, or simply add such flag into your User class, etc.
e.g.
User user[10]; // 10 element of User, which all of them is already created
//assume you have extra flag in User
user[0].init(...); // explicity do some init work on user[0]
//......
if (! user[5].isInitialized()) {
user[5].init(...);
}
(honestly I think case 2 is not really what you are looking for)

I use boost::optional:
boost::optional<int> a;
if (!a) { std::cout << "no a!"; }
a = 5;
if (a) { std::cout << "a = " << *a; }
a = boost::none;
if (!a) { std::cout << "no a!"; }

Related

Find End of Array Declared as Struct Type C++

I was recently learning to use struct datatype in c++. I know how the basics of struct datatype work and how to manipulate its variables. But I was wondering how would I determine the end of struct datatype array. For example consider the code below:
struct PersonDetails
{
string name, address;
int age, number;
}
Now in c++ program I create an array of struct type as follows:
PersonDetails Data[500];
Now consider that I have 30 records in data array and I have to display these records by looping through data array's index. So how would I determine that I have to loop through only first 30 indexes as the data is only stored in these indexes. As in char array we compare all indexes with '\0' to determine the end of array. Then what method will we use for Data[] array?
An edit that I have no idea about Vectors and the project i am working on requires me to use basics of c++(functions, control structures, loops, etc.).
It's not feasible.
For char[], back in times of C standardization, developers agreed to use \0 (integer value 0) as a special character marking end-of-string. Everything works as long as everyone is following this convention (i.e. both standard library functions and developers using those functions).
If you wanted to have such a convention for your type, you could just write down "Data object with both strings empty and both ints equal to 0 is array terminator", but you would have to follow this convention. You'd have to write functions that would stop processing array upon finding such an object. You'd have to make sure that in every array there is at least one such object.
Instead
You should use std::vector<Data> which can automatically accomodate for any number of Data objects and will now precisely how many of them are currently stored (using size() method)
or
use std::array<Data, 30>, which can store exactly 30 objects and you can assume all of them are valid objects.
IMHO the correct way to solve this is to not use a C-style array, but instead use a std::array or std::vector that knows it's .size().
Iterating a std::vector or std::array is trivial:
for (const auto& element : Data_array) {
// Do something with the array element
}
See also:
https://en.cppreference.com/w/cpp/container/array
https://en.cppreference.com/w/cpp/container/vector
https://en.cppreference.com/w/cpp/language/for
https://en.cppreference.com/w/cpp/language/range-for
The simplest solution is to just have a separate variable specifying how many array elements are filled in.
PersonDetails Data[500];
int numPersons = 0;
Data[0].name = ... ;
Data[0].address = ...;
Data[0].age = ...;
Data[0].number = ...;
numPersons = 1;
Data[1].name = ... ;
Data[1].address = ...;
Data[1].age = ...;
Data[1].number = ...;
numPersons = 2;
...
Then you use that variable when looping through the array.
for (int i = 0; i < numPersons; ++i)
{
// use Data[i] as needed...
}
I don't really agree using std::array makes any difference.
The problem you currently have doesn't occur in whether we have such an element in the container, but whether the element we are inspecting useful.
Consider the example you gave, for an array of chars, we simply check whether one of the elements is \0 to decide whether or not we should halt the iteration.
How does that work? The ramaining elements, of course, default initialized to be \0, they exist, but of no use.
Similarly, you can check, in this example, whether
name.empty()
Or, in order to avoid any possible exception, as mentioned in the comment section, do this:
add user-defined constructor to the class ( or struct, they are same actually.) which initialize age to -1 and then check if age == -1.
because it's impossible for a people not having any name, that means, you have not assign to any of the remaining elements. Thus, stop iteration.
As a supplement, using std::vector makes sense, but if that isn't a option for you for the time being, you don't need to consider it.

Check if vector is uninitialised at a certain position

This seems like a really basic thing to do, but anyway I couldn't manage to find a solution to it so far, because I always find only questions that are asking how to check if the vector is actually empty, which is not what I want to check for.
Consider this code example:
#include <iostream>
#include <vector>
using namespace std;
struct Atom {
int x,y;
int pol;
};
int main() {
vector<vector<Atom>> vec=vector<vector<Atom>>(5,vector<Atom>(5));
cout<<(vec[0][0]==nullptr); // this line doesn't compile, because the vector doesn't hold pointers.
return 0;
}
I'm trying to declare a vector of vectors of objects of a custom type. At the beginning of the Programm I will initialise the vector so that it has a specific size, but without assigning an actual object to it. Now I want to be able to check if I already assigned an object to a specific position of the vector. I would've liked to use something like vec==nullptr but this doesn't work, because the objects in the vector aren't pointers. Unfortunately I can't just change the structs standard constructor to put some indicator value that I can check for like Atom.pol==-2, because the class is created by ROS messages. Any other suggestions on how to check if I already assigned an object?
EDIT: pol will always be either -1 or 1 after I assigned an object. So is it safe to check Atom.pol==0? When I tried to do this on ideone.com it always worked, but I assume that it's not guaranteed to be 0, right?!
There is no way to check whether an object has been initialised. That said, elements of std::vector are always initialised, so there is never a need to check either.
It seems that you want to represent an "unassigned" object. The standard library has a template for you: std::optional. If you create a vector optional objects, those objects, when value-initialized, will be in "unassigned" state.
EDIT: pol will always be either -1 or 1 after I assigned an object. So is it safe to check Atom.pol==0?
Yes, that would be safe, since the constructor that you use initialises the elments using a value initialised argument.
If you can assume that some states of the object are "not valid", then you don't necessarily need to use std::optional. If the value initialised state is such invalid state, then you don't need to add a default constructor to the class either. Just like a value initialised pointer compares equal to nullptr, so too the integer members of the value initialised Atom compare equal to 0.
but I assume that it's not guaranteed to be 0, right?!
It is guaranteed to be 0.
The solution to use pol == 0 should be fine, provided that pol == 0 is in fact not a normal state for that object to be in and that you don't try it with an uninitialized instance.
The std::vector constructor you are using guaranties that the new elements are default inserted. If you are using the default allocator (which you are) then that performs value initialization of those new elements. Since Atom is a class type with a default constructor that is neither user-provided nor deleted, then your instance of Atom are zero initialized. That means each of Atom's members' value is initialized to zero.
Beware that this is something std::vector does. You need your Atoms to be initialized to zero for this approach to work. If you tried the following, it would be undefined behavior. The Atom members are not initialized, much less guaranteed to be zero :
int main()
{
Atom a;
std::cout << (a.pol == 0); // <- Not okay
}
You can force value initialization by adding {} though :
int main()
{
Atom a{};
std::cout << (a.pol == 0); // <- Okay now
}
Edit : Accidentally used the same code sample for both examples.
One way to do this is to change the signature of vec to,
vector<vector<Atom*>> vec=vector<vector<Atom*>>(5,vector<Atom*>(5));
Then you can do the null ptr check to see if a given element has been initialized. This does add some complexity though, as you have to handle the memory allocation yourself.
If you want to initialize the members of Atom to specific values and check if they are initialized, you can do this.
vector<vector<Atom>> vec=vector<vector<Atom>>(5,vector<Atom>(5, {1, 2, 3}));
This initializes x, y, pol to 1, 2 and 3 respectively.
Minimal Example:
int main() {
using std::cout;
using std::vector;
vector<vector<Atom>> vec=vector<vector<Atom>>(5,vector<Atom>(5, {1, 2, 3}));
cout<<((vec[0][0]).x == 1) << "\n";
cout<<((vec[0][0]).y == 2) << "\n";
cout<<((vec[0][0]).pol == 3) << "\n";
cout<<((vec[0][0]).x == -1) << "\n";
cout<<((vec[0][0]).y == -1) << "\n";
cout<<((vec[0][0]).pol == -1) << "\n";
return 0;
}
See Demo

C++ Vectors insert new objects

I'm a self-taught c++ programmer (still at novice level).
I think I got an idea of how c++ works, but I can't wrap my head around this:
I want to create and populate an std::vector with different elements of a defined-by-me class:
// other code
while (getline(cfgDataStream, cfgData)) //parsing cycle of the config file
{
std::stringstream ss(cfgData); //creating a stream in order to fill fields
ss >> string1 >> IP1 >> IP2 >> PORT2 >> INDEX;
//they are all strings save the last one, which is a int
if (ss.fail())
{
//bad things happen
}
//FIRST IDEA: Using insert()
CModbusServer MBtemp* = new CModbusServer(this, IP2.c_str(), PORT2, INDEX)
std::vector<CModbusServer*>::iterator iterator = this->m_pServerCollection.begin(); //I get the vector initial position
m_pServerCollection.insert(iterator + (INDEX), MBTemp); // I put the new object in the right index (I don't trust the order in the config file)
//SECOND IDEA: Using push_back()
m_pServerCollection.push_back( new CModbusServer(this, IP2.c_str(), PORT2, INDEX)); //I attach each new object to the end of vector (i trust the order in the config file)
}
basically I want to create an object of CModbusServer and insert its pointer in a vector, so that I have n different CModbusServer objects in each vector position.
And this is where I get lost, i tried two ways of insertion (as shown in the code ) without success.
CModbusServer has, among others, a const char* ipAddress field. If I try to access to that field (i.e. to use it in a .Format(_T("%S)) function) I get random data. Trying to see why I noticed that in the vector I don't have n different objects,but n copies of the last object created with new CModbusServer(this, IP2.c_str(), PORT2, INDEX). Probably this happens due to the fact that I have a vector of pointers, but those should be pointers to different objects...
I'm using Visual Studio 2015 with MFC in order to realize a dialog-based application. I have an AppEngine class that calls method from other classes and has a vector of CModbusServer elements.
CModbusServer.h is as follows:
class CModbusServer
{
public:
CModbusServer(void *parentEngine, const char* , unsigned short , int );
~CModbusServer();
const char* ipAddress;
unsigned short port;
int indNode;
modbus_t *MBserver;
bool isConnected;
}
So, my question are:
1) Why can't I access to the ipAddress field (instead of reading "192.0.2.1" I read random characters) while I theoretically should be able to read it usingtheApp.CModbusServerVector[properIndex]->ipAddress?
2) I'm making a mistake in populating the vector, but I can't see where and, most importantly, why it's wrong.
Thank you for your assistance, and excuse my english and any omission.
EDIT:
The constructor code of CModbusServer is this:
CModbusServer::CModbusServer(void *pE, const char* ip, unsigned short nport, int ind)
: parentEngine(pE), //used in order to keep track of the parent dialog
ipAddress(ip),
port(nport),
indNode(ind)
{
this->isConnected = false;
this->m_socket = INVALID_SOCKET;
memset(&m_socketstructhint, 0, sizeof m_socketstructhint);
m_socketstructhint.ai_family = AF_UNSPEC;
m_socketstructhint.ai_socktype = SOCK_STREAM;
m_socketstructhint.ai_protocol = IPPROTO_TCP;
MBserver = modbus_new_tcp(ipAddress, (int)nport);
}
Please tell me if I omitted any other useful information.
Initially I used CString for managing the strings, but then I incurred in more and more issues and finally got a compiling and patially working code with const char*. I managed to enstablish a connection and to read the desired modbus registers, but then I got stuck on the isAddress printing problem.
modbus_new_tc(ip,port) is a method found in libmodbus library, a freeware library written for C that I had to use.
EDIT 2: related to angew answer:
So, If I'm right, what's happening is that I create a temporary set of pointers, that are used by the constructor (I've now added the relevant code). But shouldn't be the constructed object unrelated with what I've passed as argument? Aren't those argument copied? Sorry if the question is stupid, but I'm still learning.
the indices are sequential, though in the config file thay could as well be 0-1-2-3 (1 per line) or 0-3-1-2, that's what I meant by "don't trust them".
Since the push_back method has the same issues, probably the problem is back in the constructor. What puzzles me is that by doing a step by step execution I can see that with each iteration of the while loop I get new and correct data, but instad of being put in the i-th position, is put in the first i positions (i.e.: origin data: a b c, 1st run vector = a; 2nd run vector = b b,3rd run vector = c c c)
I didn't know std::unique_ptr<>, i'll look it up.
I've tried to use std:string or even CString, but the problem lies beneath libmodbus libraries.
Calling c_str on a std::string returns a "live" pointer to the internal data stored in that std::string instance. The pointer returned points to a buffer which is only valid as long as the std::string on which it was called remains alive and unmodified.
The constructor of CExtracalModbusServer just stores the pointer passed in. That pointer becomes dangling as soon as IP2 is re-assigned in the next iteration of the input loop. (The address pointed to is still the same, but the buffer which was previously located at that address has either been overwritten, or freed, or something else. In other words, the pointer is just dangling).
As for inserting into the vector: the first way (with insert) could only work if the indices in the files are sequential and starting at 0. You need a valid iterator into the vector to insert into it, where valid here means either pointing to one of the elements already in the vector, or the past-the-end iterator (the one returned by end()). If INDEX is equal to or larger than the size of the vector, then m_pServerCollection.insert(iterator + (INDEX), MBTemp); will attempt to insert outside the vector (essentially a buffer overfow). Ubnefined behaviour ensues.
The push_back way of inserting data should work, and if you see it misbehaving, it's either an artifact of the earlier bug (the one with dangling pointers), or there is a separate problem in code which you haven't shown.
Unrelated to the question at hand, but the code contains pretty bad practice in the form of manually managed dynamic memory. Instead of storing a CModbusServer* in the vector and managing the memory manually with delete in all the right places, you should be using std::unique_ptr<CModbusServer> which will take care of proper deallocation for you, even in the presence of exceptions.
If CModbusServer is under your control, you should likewise change it to store a std::string instead of a const char*. Never use C-style strings in C++ unless you have to interact with C-style APIs, and even in such case, limit their use only to the interaction itself. It's the same principle all over again: don't manage memory manually.

Vector overwriting object pointers

Everytime I call this method, The information is overwritten. If I call this function with name = "greg" then it will cout greg, If I then input "carl" it will cout carlcarl. The constructor is empty, and group and _groups are declared in the header.
I've been stuck on this for about 6 hours and I'm at a loss. Can someone explain to me how to fix this? It's not shown, but "group" is an object pointer. I'm taking this as a college course and its pretty much self-taught, I have to follow strict instructions on what to use and what not to use. I can't use strings.
void GroupDB::addGroup(char* name)
{
group = new GroupInfo(name, _nextGid++);
_groups.push_back(group);
for(int i = 0; i < _size; i++)
{
cout << (*_groups.at(i)).getGroupName();
}
_size++;
}
It sound's like you're storing the name pointer itself, which points to a temporary array that's invalidated at some point after this function returns. Instead, you want to store a more persistent copy of the string data. Use std::string not char* to store strings. If you "can't use strings" as a condition of the exercise, then you'll need to allocate an array to store the string data in, just as std::string would.
In general, don't use pointers unless you really need to. I doubt you want to be messing around with new here, but should rather store GroupInfo objects directly in the vector (or whatever _groups is).

Object not added to array

I'm doing my homework (and learn how C++ works).
My task is:
Define some class with field...(never mind)
create an vector and array from these object and iterate it! (listing, average by field,etc).
Now it's correctly works with vector, but array doesnot work:
static Cipo* cipok; // object array
static int cep = 0; // endpoint index
static int ccap = 0; // array size
Default assignmet opearator for Cipo:
public: Cipo& operator=(const Cipo &c)
{
return ((Cipo&)c);
}
Initalization:
cipok = (Cipo*) malloc(sizeof(Cipo*)*100); // new Cipo[num] doesn't work..
ccap = 100;
Test code:
for (int i = 0; i < 5; i++)
{
Cipo c(43.5, "str", 12670, false, false);
std::cout << c.ar <<" ";
cipok[cep] = c;
std::cout << cipok[cep].ar << " ";
cep++;
}
And the result:
12670 0 12670 0 12670 0 12670 0 12670 0
But objects not "disappeared" if I use vector, push_back() the objects and read from the vector with direct indexing (or with iterators). Why do they exhibit this behaviour?
You immediate problem is likely caused by whacky implementation of operator = that does absolutely nothing. I'd recommend step through the code in debugger to see it. operator = (and copy constructor) should properly copy values into destination object.
There are many other issues with the code - your naming convention is ... interesting, you seem to try to cast whatever you have to whatever result is required for code to compile without reasoning what should actually be done. malloc in C++ code is very rarely needed...
I think the general problem is, I'm programming always in java (but now in university i must prog. in C/C++, naming conventions, like in java and in hungarian the Cipő is meaning Shoe). And in Java there is no pointers, and all object always acces by reference, but looks like (as i tested ) if i create a new object array the C++ will not allocate only 100 pointer which points to the object (where the object data starts), it allocated 100*sizeof(object) and for this place i can add data trougth assing operator.
it's my teory true?
So i tried to manage Object acces like in java.
Why copy the Object if itself alredy exist? (I don't like to "clone" objects).