CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Main CFD Forum

dynamically allocated memory in C++

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old   November 1, 2006, 19:53
Default dynamically allocated memory in C++
  #1
Junseok Kim
Guest
 
Posts: n/a
I want to convert C program into C++ frame work. Would you help me out the following problem.

I want to use new and delete for the following routinues. Thank you in advance.

If possible, I need a full sample program using the following routines.

double **dmatrix(long nrl, long nrh, long ncl, long nch) {

double **m;

long i, nrow=nrh-nrl+1+NR_END, ncol=nch-ncl+1+NR_END;

m=(double **) malloc((nrow)*sizeof(double*));

m+=NR_END;

m-=nrl;

m[nrl]=(double *) malloc((nrow*ncol)*sizeof(double));

m[nrl]+=NR_END;

m[nrl]-=ncl;

for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;

return m; } void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch) {

free(m[nrl]+ncl-NR_END);

free(m+nrl-NR_END);

return; }
  Reply With Quote

Old   November 3, 2006, 05:55
Default Re: dynamically allocated memory in C++
  #2
Márcio
Guest
 
Posts: n/a
the changes in your code would be in the allocation / dealocattion commands. The access to the elements in the matrix would be in the same way.

double **m; //unchanged

m = new double*[nrow];

instead of m=(double **) malloc((nrow)*sizeof(double*)), each row would be allocated simmilarly to this:

for(size_t i=0;i < nrow;++i) m[i] = new double[nrow*ncol];

and, to deallocate,

for(size_t i=0;i < nrow;++i) delete[] m[i];

delete[] m;

but, instead of doing this, you should consider using the STL containers, like std::vector or std::valarray, which is best suited for scientific computing.

tip: There's a lot of C++ material on the net, like the book "Thinking in C++", for example, or an online guide on the site www.informit.com, and tons of material in other sites. It's realy worth spending some time searching for it.

I Hope it helps

-Márcio

  Reply With Quote

Old   November 3, 2006, 06:24
Default Re: dynamically allocated memory in C++
  #3
chris
Guest
 
Posts: n/a
I bet you meant

for(size_t i=0;i < nrow;++i) m[i] = new double[ncol * sizeof(double)];

instead of

for(size_t i=0;i < nrow;++i) m[i] = new double[nrow*ncol];

;-).
  Reply With Quote

Old   November 3, 2006, 09:19
Default Re: dynamically allocated memory in C++
  #4
Márcio
Guest
 
Posts: n/a
oops! Thank you.
  Reply With Quote

Old   November 6, 2006, 03:15
Default Re: dynamically allocated memory in C++
  #5
O.
Guest
 
Posts: n/a
For the records: new double[100]; will allocate 100*sizeof(double) bytes in memory (800 bytes on most machines). new double[100*sizeof(double)]; will allocate 100*sizeof(double)*sizeof(double) bytes in memory (6400 bytes on most machines). !!!!

  Reply With Quote

Old   November 13, 2006, 14:22
Default Re: dynamically allocated memory in C++
  #6
Angen
Guest
 
Posts: n/a
Good point O.

Angen

  Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
INSUFFICIENT MEMORY ALLOCATED Virag Mishra CFX 2 May 14, 2007 10:02
INSUFFICIENT MEMORY ALLOCATED Error for FSI Cuong CFX 2 November 6, 2006 19:48
CFX CPU time & real time Nick Strantzias CFX 8 July 23, 2006 17:50
INSUFFICIENT MEMORY ALLOCATED Jan CFX 2 July 16, 2003 00:55
INSUFFICIENT MEMORY ALLOCATED xfshi CFX 1 June 19, 2003 18:54


All times are GMT -4. The time now is 04:08.