Allocate Memory Problem

A

alan

Dear all,

I am a newbie in C++.

If I want to allocate memory to objects, I will use double *d = new
double[10] instead of ANSI-C malloc.

But if I want to resize, say enlarge num of objects to 20, how can I
do it?
In ANSI-C, I can use realloc to do this.

I also try that.
double *d = new double[20];

but my pervious 10 double value is cleared....I want to keep the
pervious value and enlarge the size.

Please help. If possible, pls give me some simple examples.

Thank you very much.
Alan
 
J

Jeff Schwab

alan said:
Dear all,

I am a newbie in C++.

If I want to allocate memory to objects, I will use double *d = new
double[10] instead of ANSI-C malloc.

But if I want to resize, say enlarge num of objects to 20, how can I
do it?
In ANSI-C, I can use realloc to do this.

I also try that.
double *d = new double[20];

but my pervious 10 double value is cleared....I want to keep the
pervious value and enlarge the size.

Please help. If possible, pls give me some simple examples.

Thank you very much.
Alan

Try using a std::vector; it will manage the memory for you.


#include <vector>

int main( )
{
std::vector< double > d( 10 );

d.resize( 20 );
}
 
R

Rolf Magnus

alan said:
Dear all,

I am a newbie in C++.

If I want to allocate memory to objects, I will use double *d = new
double[10] instead of ANSI-C malloc.

But if I want to resize, say enlarge num of objects to 20, how can I
do it?
In ANSI-C, I can use realloc to do this.

Either use std::vector instead, which does handle it for you, or do
yourself what realloc typically does: Allocate a new array and copy the
data to it, then deallocate the old one.
I also try that.
double *d = new double[20];

I guess you meant

d = new double[20];
but my pervious 10 double value is cleared....

No. Your previous 10 double values are still there, at the same place
where the were before. But you overwrote your pointer to it, so the
data has become unreachable. You have produced a memory leak.
I want to keep the
pervious value and enlarge the size.

Please help. If possible, pls give me some simple examples.

#include <algorithm>
#include <iostream>

int main()
{
double* d = new double[10];
for (int i = 0; i < 10; ++i)
d = i;

double* tmp = new double[20];
std::copy(d, d + 10, tmp);
delete d;
d = tmp;

for (int i = 0; i < 10; ++i)
std::cout << "d[" << i << "] = " << d << '\n';
}

Or let a vector handle it for you:

#include <iostream>
#include <vector>

int main()
{
std::vector<double> d(10);
for (int i = 0; i < 10; ++i)
d = i;

d.resize(20);

for (int i = 0; i < 10; ++i)
std::cout << "d[" << i << "] = " << d << '\n';
}
 
J

Jorge L Rivera

Small typo, which results in undefined behavior

Rolf said:
alan wrote:

Dear all,

I am a newbie in C++.

If I want to allocate memory to objects, I will use double *d = new
double[10] instead of ANSI-C malloc.

But if I want to resize, say enlarge num of objects to 20, how can I
do it?
In ANSI-C, I can use realloc to do this.


Either use std::vector instead, which does handle it for you, or do
yourself what realloc typically does: Allocate a new array and copy the
data to it, then deallocate the old one.

I also try that.
double *d = new double[20];


I guess you meant

d = new double[20];

but my pervious 10 double value is cleared....


No. Your previous 10 double values are still there, at the same place
where the were before. But you overwrote your pointer to it, so the
data has become unreachable. You have produced a memory leak.

I want to keep the
pervious value and enlarge the size.

Please help. If possible, pls give me some simple examples.


#include <algorithm>
#include <iostream>

int main()
{
double* d = new double[10];
for (int i = 0; i < 10; ++i)
d = i;

double* tmp = new double[20];
std::copy(d, d + 10, tmp);
delete d;


Here you probably meant,

delete[] d;

d = tmp;

for (int i = 0; i < 10; ++i)
std::cout << "d[" << i << "] = " << d << '\n';
}

Or let a vector handle it for you:

#include <iostream>
#include <vector>

int main()
{
std::vector<double> d(10);
for (int i = 0; i < 10; ++i)
d = i;

d.resize(20);

for (int i = 0; i < 10; ++i)
std::cout << "d[" << i << "] = " << d << '\n';
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top