How to use new and delete functions in Visual C ++ 6/ Please help me. Very Urgent!!!!

L

Liu Ju

Dear people,


I wrote a program in Visual C6 in which I used the functions
"malloc" and "free". The errors sometimes appears due to some mystery
causes.
A friend of mine suggested that I use new and delete functions instead
since malloc and free are not always good.

My aim is to set aside a free range of memory and then initialize
these values. My orginal source codes using malloc and free:
-----------------
int *accum;
float *tabSin;
float *tabCos;
accum = (int *) malloc( sizeof( int ) * numangle * numrho );
tabSin = (float *) malloc( sizeof( float ) * numangle );
tabCos = (float *) malloc( sizeof( float ) * numangle );
memset( accum, 0, sizeof( int ) * numangle * numrho );

blah blah blah
 
J

John Harrison

Liu Ju said:
Dear people,


I wrote a program in Visual C6 in which I used the functions
"malloc" and "free". The errors sometimes appears due to some mystery
causes.
A friend of mine suggested that I use new and delete functions instead
since malloc and free are not always good.

new and delete are *different*, they are not better or worse. If you don't
understand the difference how do you expect to improve your program by
substituting on for the other?
My aim is to set aside a free range of memory and then initialize
these values. My orginal source codes using malloc and free:
-----------------
int *accum;
float *tabSin;
float *tabCos;
accum = (int *) malloc( sizeof( int ) * numangle * numrho );
tabSin = (float *) malloc( sizeof( float ) * numangle );
tabCos = (float *) malloc( sizeof( float ) * numangle );
memset( accum, 0, sizeof( int ) * numangle * numrho );

blah blah blah
.
.
free(accum);
free(tabSin);
free(tabCos);

---------------------


Errors often appear.


Now I want to use new and delete instead. But what I need are the
range of memory is not fixed while programming. It would be pass to
the program as a parameter.

Can I write it as
------------------------
int* accum = new int[numangle * numrho];
float* tabSin = new float[numangle];
float* tabCos = new float[numangle];

for(int i=0;i<=numangle * numrho;i++)
accum=0;


Don't forget

delete[] accum;
delete[] tabSin;
delete[] tabCos;

Your code is perfectly correct, but whatever your problem was before this is
not going to fix it.

You need to find out what the real problem is, instead of guessing.
Thank you very much,

Yours truly,

LiuJu

john
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top