-1

i have an array of object class, and i want to assign the last indexi to NULL i have the following code, but it gives an error

DNA is a class

allRightSequences is a vector

DNA* newDNA = new DNA[allRightSequences.size()];

newDNA [allRightSequences.size()] = NULL; << this line gives an error
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Abdelrahman
  • 67
  • 2
  • 10

4 Answers4

5

NULL is a macro that, in appropriate situations, will expand into something that can be treated as a null pointer constant. So you can use it to set a pointer value:

int *ip = NULL; // okay

However, newDNA does not contain pointers; it contains objects of type DNA. Forget the array for a moment. The problem is this:

DNA dna = NULL;

This won't work, unless DNA has a constructor that can be called with whatever NULL expands into.

If you really need to have a marker at the end of the array you need to create an array of pointers. But you really don't need this. Use std::vector<DNA>, which keeps track of the size for you.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
1

allRightSequences.size() is not the last index of an array that has allRightSequences.size() elements. The last index is allRightSequences.size() - 1. The behaviour of accessing the array out of bounds is undefined.

Another potential problem: There must exist an appropriate assignment operator in order to be assign a pointer to a DNA object. Unless you have defined such operator, the assignment is ill-formed.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    While this will help the OP get a valid element, If they try `newDNA [allRightSequences.size() - 1] = NULL;` they are still going to get an error. – NathanOliver Dec 14 '18 at 18:54
  • @NathanOliver not necessarily. It is possible to define an assignment operator. – eerorika Dec 14 '18 at 18:57
1

What you might want (though not sure from the question yet), is an array of pointers to DNA objects. What you are declaring is "just" an array of DNA objects; and you can't set an object to null, as Pete Becker's answer explains very well.

The following code would work:

// notice how we use +1 here to have place for the NULL element at the end
DNA* newDNA[] = new DNA*[allRightSequences.size()+1];  
newDNA [allRightSequences.size()] = NULL;

For each element of the array, you'd however also have to create a DNA object via new DNA... then...

Note that if you use a compiler supporting C++11, use nullptr instead of NULL. And if you want to avoid the hassle with pointers completely, you could use a construct like std::optional in case you use C++17 or boost::optional for earlier versions, as described in this answer to another question, as mentioned by Baum mit Augen above.

Also, the good question is what you really need that zero pointer at the end - if it's just for determining the last element when iterating over the array, then you might be better off using an std::vector<DNS> or a similar collection type instead...

codeling
  • 11,056
  • 4
  • 42
  • 71
  • 1
    Instead of using `newDNA = new DNA*[allRightSequences.size()]; newDNA[allRightSequences.size()-1] = NULL`, consider using `newDNA = new DNA*[allRightSequences.size()+1]; newDNA[allRightSequences.size()] = NULL` instead. This way, `newDNA` contains the same number of accessible elements as `allRightSequences` rather than losing 1 element. – Remy Lebeau Dec 14 '18 at 20:24
-1

Use the following code:

newDNA [allRightSequences.size()-1] = NULL;

Because an array index in C++ goes from 0 to n-1, where allRightSequences.size() in your case returns n, where n is the size of the array.

codeling
  • 11,056
  • 4
  • 42
  • 71
  • define operator=() method beacuse when use newDNA [allRightSequences.size()-1] = NULL; compiler find operator=() method which is not defined and throw a error. – Susheel Dwivedi Dec 14 '18 at 19:07