[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

does the new oprator resize ?

I now know that if vector is used for resize for dynamic allocation ... I though we cannot resize using new
But can anyone tell me why the program I wrote does this

    int* temporary // global declaration of pointer
void main()
{
      int num = 3;
        temporary=new int[1];  
//      temporary=new int[num];
      for( int i=0;i<=6;i++)
      {
      temporary[i] = 0;
      }
     temporary[4] = 23907;
     temporary[15500] = 111;

       powerout<< temporary[0]<<"\t"<<temporary[1]<<
            "\t"<<temporary[3]<<"\t"<<temporary[4]<<
            "\t"<<temporary[15500]<<"\n";
}

If you see I have used new to allocate
1 memory space ... Now when I am using the for loop to initialize I am initializing temporary[0] ,temporary[1] ,temporary[2] ,temporary[3] ,temporary[4] ,temporary[5] ,temporary[6] .. Firstly how could I do that 6>num>1(space allocated)... Secondly how come it is storing 111 in that 15500 ... one more thing is if I increase 15500 to 16000 it crashes ... even if I change i to long int it still crashes ...

Right now what this seems to be doing is this
it is storing 1 memory space
then it is resize and initializing from 0 to input_size (here it is 6) and then it is storing 111 in 15500 even though no space was created at that location earlier ... These all indiates that it is resizing ... except it crashes if I try to asign 111 at 16000 and beyond ...
is part i do not understand
0
arindam042099
Asked:
arindam042099
  • 5
  • 3
  • 3
  • +1
1 Solution
 
arindam042099Author Commented:
same kind of thing happens for a 2 dimensional array


    float** Amatrix ;

void main()
{

// *********************************************************
 // Dynamic Allocation of the Ymatrix using the new operator
 // ********************************************************
     Amatrix= new float*[0]; //row
   
      for(int j=1; j<=3; j++)        
      {
       Amatrix[j] = new float[0]; //column
      }



      Amatrix[1][1]=2.0;
      Amatrix[1][2]=3.0;
      Amatrix[1][3]=5.0;

      Amatrix[2][1]=2.0;
      Amatrix[2][2]=4.0;
      Amatrix[2][3]=6.0;

      Amatrix[3][1]=7.0;
      Amatrix[3][2]=5.0;
      Amatrix[3][3]=9.0;

      for(int i=1; i<=3; i++)    
      {
          for(int j=1; j<=3; j++)        
            {
                  powerout<<i<<"\t"<<j<<"\t"<<Amatrix[i][j]<<"\t"<<"\n";
            }
    }
}
0
 
hustchCommented:
The new operator does not resize the memory.
When you allocate to few elements, you overwrite whatever is located after that memory, destroying the data, that was placed there.
That may or may not cause the program to crash, depending on what data gets destroyed.
When you change the program to overwrite more or different data, you will eventually cause the program to crash.
0
 
nietodCommented:
>> we cannot resize using new
That is true.

As hustch has said, your code is in error.  Unlike some languages, C++ does not have features to record the dimensions of a dynamically allocated array.  Because of this it is impossible for the language to detect errors where you attempt to access an array element beyond the end of the array.  So when you do

temporary=new int[1];  
temporary[10] = 0;

you are making a mistake.  But the program contains no features to detect the mistake directly.  The mistake can only be detected indirectly,b ut the effects that it causes (like other variables changing for apparently no reason.)

>> These all indiates that it is resizing ... except it
>> crashes if I try to asign 111 at 16000 and
>> beyond ..
Whenever you access an invalid element you are risking a crash.  It is possible that ther are items before 15000 that will cause a crash and items after 16000 that won't.  It is possible that some will cause a crash right away and some will cause a crash much latter.

If you have trouble using only the legitimate items in arrays, you might want to switch to using an array class, like the STL vector<> class.  These class do record the number of items in the array, so they can detect attempts to access invalid items and can notify you right away, so you can't get away with a mistake that causes problems in the program that may not show up until later.   Array classes have other advantages too, like the ability to resize.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
hustchCommented:
There are some things you should be aware of, when using the vector<> class:
1) [] is unchecked like normal arrays.
2) you can use at() for a checked access, but it will throw an exception if the index is out of range.
3) it can resize itself when needed, but be aware, that it may copy all the elements when it does that.

You still need to know the size of your array or vector<>. You can ask a vector<> about its size using the size() member, but you have to remember the size of an array yourself.

And as nietod said, you don't have to write to an invalid element to cause a crash, sometimes reading will be enough.
0
 
nietodCommented:
>> when using the vector<> class:
>> 1) [] is unchecked like normal arrays
The behavior is implimentation defined.  Often In a debug version of many implimentations it will report an error.

>> you don't have to write to an invalid
>> element to cause a crash, sometimes
>> reading will be enough
That's why I use the term "access", meaning read or write/
0
 
gashevCommented:
You defined temporary[num];
Example: num=3;
But 15500>num.
If you want correct write program, you can use functions for check indexs in array.

Example:
class CArray{
public:
     CArray(UINT size);
     ~CArray();
     bool SetElement(UINT Index,float Significance);
     void Resize(UINT size);
     ..........
private:
     UINT Size;
     float* temp;
}
CArray::CArray(UINT size)
{
   temp=new float[size];
   for(UINT i=0;i<size;i++)
        temp[i]=0;
   Size=size;
}
CArray::~CArray()
{
   delete[] temp;
}

bool CArray::SetElement(UINT Index,float Significance)
{
     if(Index>=Size) return false;
     temp[Index]=Significance;
     return true;
}

void CArray::Resize(UINT size)
{
   delete[] temp;
   temp=new float[size];
   for(UINT i=0;i<size;i++)
      temp[i]=0;
   Size=size;
}
In this example you unpossible use Index> size of array.

goleg@zahav.net.il
0
 
nietodCommented:
gashev, did you read the question history?

hustch already answered the question.  he should have answered, but for some reason did not.  You have just provided the same information that he and I did.  
0
 
gashevCommented:
Correct answer.
In constructor of CArray you can write:
const max=32767;
CArray::CArray(UINT size)
{
  temp=new float[max];
  for(UINT i=0;i<max;i++)
    temp[i]=0;
  Size=size;
}

and

CArray::Resize(UINT size)
{
   if (size<max)Size=size;
}

add function in class
  float CArray::GetElement(UINT Index).
If in your program

CArray arr(15600);
arr.SetElement(15500,111.0);
arr.Resize(20000);

After it's:
float t;
t=arr.GetElement(15500);

But t=111.0
After resize significance in point unchange.
0
 
hustchCommented:
nietod, I did only post a comment, because I have the impression, that since the change to EE, people preferred to have their questions kept unlocked, so that more experts will look at them.
Personally, I find it very anoying, when people post poor comments as answers to my qustions.

gashev, have your tested your solution? It seams, that resize() just discards any previous value, so it does not work as you describe.

arindam, if a resizing array is what you want, I would prefer vector<> over CArray, it has a lot of other advantages.
0
 
nietodCommented:
Are you serious?  That is a tremendous waste of space.
0
 
nietodCommented:
My last comment was directed at gashev's 2nd suggestion, not hutsh's comment.

>> since the change to EE, people
>> preferred to have their questions kept
>> unlocked, so that more experts will look
>> at them.
I don't think that is necessarily true.  If an answer is good, you only need one.  And not locking a question can cause disputes--like this.  So if you are confident that your answer is complete and correct, I recommend you make it an answer.  Otherwise someone else, someone less deserving, will.

>> I find it very anoying, when people post poor
>> comments as answers to my qustions
I don't think this is any defence against that.  Look at what happened here.  arindam still has an answer that is 1/2 the material you and I posted and !/2 a terrible mistake.

>> It seams, that resize() just discards any previous value  
That is one of a dozen mistakes.  The lack of a copy constructor and operator = seems most serious.  

>>  if a resizing array is what you want, I would
>> prefer vector<> over CArray
Certainly over this deadly CArray.  Although there are other array classes that may be a little easier to use than vector<>.  But in general, I would lean toward vector<> too.
0
 
arindam042099Author Commented:
thanks
0
 
arindam042099Author Commented:
My server were I work was down during the weekend and that is why I could not check the responses as well answer ... If this has caused some problems ... APOLOGIZE ... I have no intension to unlock and wait for more experts to comment

regards
arindam
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now