Link to home
Start Free TrialLog in
Avatar of arindam042099
arindam042099

asked on

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
Avatar of arindam042099
arindam042099

ASKER

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";
            }
    }
}
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.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
>> 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/
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
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.  
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.
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.
Are you serious?  That is a tremendous waste of space.
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.
thanks
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