473,413 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

help finding an efficient way to copy dynamic memory!!

I have developed my own copy function for coping my own dynamic memory
structure. It works, but I feel its not too efficient. There must be
a quicker way to copy the data. In some of the routines I have
developed the copy function gets called several hundreds of times, with
very large data structures. This can take some time....

If you can't tell by the code the data structure is a matrix for
storing complex numbers.

Attached is the code.

struct complexMatrix
{
float r;
float i;
};

typedef complexMatrix** ComplexMatrix;
typedef complexMatrix Complex;

ComplexMatrix Cmxcopy(ComplexMatrix mat, int rows, int cols)
{

int i;
int j;
ComplexMatrix temp;
temp=CmxAlloc(rows,cols);

for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
{
temp[i][j].r=mat[0][0].r;
temp[i][j].i=mat[0][0].i;
}

return temp;

}
All suggesting, comments, emails, faxes, post-its and memos welcome.

Aug 24 '05 #1
16 2112
laclac1, There is a potential for memory leakage after executing
CmxCopy unless you implement the correct destructor for ComplexMatrix.

Aug 24 '05 #2
la******@yahoo.com wrote:
I have developed my own copy function for coping my own dynamic memory
structure. It works, but I feel its not too efficient. There must be
a quicker way to copy the data. In some of the routines I have
developed the copy function gets called several hundreds of times, with
very large data structures. This can take some time....

If you can't tell by the code the data structure is a matrix for
storing complex numbers.

Attached is the code.

struct complexMatrix
{
float r;
float i;
};

typedef complexMatrix** ComplexMatrix;
typedef complexMatrix Complex;

ComplexMatrix Cmxcopy(ComplexMatrix mat, int rows, int cols)
{

int i;
int j;
ComplexMatrix temp;
temp=CmxAlloc(rows,cols);
This is very C-like. Why don't you start writing C++? Only declare
objects when they are needed, and avoid defining without initialising:

ComplexMatrix temp = CmxAlloc(rows, cols);

for(i=0;i<rows;i++)
for (int i=0; i<rows; i++)
for(j=0;j<cols;j++)
for (int j=0; j<cols; j++)
{
temp[i][j].r=mat[0][0].r;
temp[i][j].i=mat[0][0].i;
Why are you copying the top left element of the 'mat'? Shouldn't this be

temp[i][j].r=mat[i][j].r;
temp[i][j].i=mat[i][j].i;
???
}
Since your 'complexMatrix' struct is a POD you _may_ use memcpy to copy
objects of that type. That might improve things a bit:

memcpy(&temp[i][j], &mat[i][j], sizeof(temp[i][j]));

And it can be improved even further if you keep the pointer to the current
element around and increment it every inner loop, instead of indexing (for
some compilers it gives a bit of advantage).

Another way to actually improve it is to avoid dual allocation/filling
of the matrix and instead develop a 'allocate_and_copy' and use it in
place of 'CmxAlloc' and the loops.

return temp;

}
All suggesting, comments, emails, faxes, post-its and memos welcome.


V
Aug 24 '05 #3
Frank Chang wrote:
laclac1, There is a potential for memory leakage after executing
CmxCopy unless you implement the correct destructor for ComplexMatrix.


Frank, please pay attention. 'ComplexMatrix' is a pointer to a pointer
to 'complexMatrix' struct. There is no destructor.
Aug 24 '05 #4
Victor Bazarov, Yes, I know ComplexMatrix is a pointer to a pointer.
Suppose this scenario occurs,

int main()
{
int rows(1000);
int cols(1000);

ComplexMatrix s = CmxAlloc(rows, cols)
ComplexMatrix b = CmxCopy(s,rows,cols);
CmxFree(s); // deallocates memory for s

return 0;
}

Now ,if the user fails also to deallocate the memory for ComplexMatrix
b, which is a pointer to a pointer, there will be a memory leak. The
reason I used the term destructor is because 'ComplexMatrix' should
really be a C++ template class as I pointed out last week.

Aug 24 '05 #5
la******@yahoo.com wrote:
I have developed my own copy function for coping my own dynamic memory
structure. It works, but I feel its not too efficient. There must be
a quicker way to copy the data. In some of the routines I have
developed the copy function gets called several hundreds of times, with
very large data structures. This can take some time....


Having one contiguous block of memory representing one matrix would
help in creating copies faster. (This is my assumption. Hope that
is valid.)

How about something like this? In my example, I am using two blocks
of memory, one holding the actual data (or the complex numbers) and
the other holding the meta data. Any comments on this are welcome.

It is C code, run through C++ compiler. Converting this into a
suitable C++ class is left as an exercise.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct ComplexNumber
{
float r;
float i;
};

struct Matrix
{
int maxRow;
int maxCol;
size_t size;
ComplexNumber *first;
};

Matrix* matrix_alloc(int row, int col)
{
Matrix* matrix = (Matrix*) malloc( sizeof(Matrix) );
matrix->maxRow = row;
matrix->maxCol = col;
matrix->size = sizeof(ComplexNumber)*row*col;
matrix->first = (ComplexNumber*) malloc( matrix->size );
return matrix;
}

ComplexNumber* matrix_get(Matrix* matrix, int row, int col)
{
int idx = (row* matrix->maxCol)+col;
return matrix->first+idx;
}

void matrix_free(Matrix* x)
{
free(x->first);
free(x);
}
Matrix* matrix_clone(Matrix* orig)
{
Matrix* copy = matrix_alloc(orig->maxRow, orig->maxCol);
memcpy(copy->first, orig->first, orig->size);
return copy;
}

void matrix_print(Matrix *matrix)
{
printf("\n");
for(int i=0; i < matrix->maxRow; ++i)
{
for(int j=0; j< matrix->maxCol; ++j)
{
ComplexNumber* cn = matrix_get(matrix, i,j);
printf("(%f, %f) ", cn->r, cn->i);
}
printf("\n");
}
printf("\n");
}

int main()
{
Matrix *matrix = matrix_alloc(3, 3);

for(int i=0; i <3; i++)
{
for(int j=0; j<3; j++)
{
ComplexNumber* cn = matrix_get(matrix, i,j);
cn->r = i;
cn->i = j;
}
}
Matrix *copy = matrix_clone(matrix);
matrix_print(matrix);
matrix_print(copy);
matrix_free(matrix);
matrix_free(copy);
return 0;
}

Aug 25 '05 #6
How about only having one memcopy per matrix copy...?
I thought this would work but i guess it doesn't
memcpy(&temp, &m, rows*cols*2*sizeof(float));
Is there a way to make this work? I am looking in to the last
suggestion.

Aug 25 '05 #7
la******@yahoo.com wrote:
How about only having one memcopy per matrix copy...?


But the code I posted has only one memcpy per matrix copy.

Is it just me or does anyone else also think that, the operation
is more appropriately called clone rather than a copy?

Rgds,
anna

Aug 25 '05 #8
Yes your code does, but i would like to not have to redo all of my
existing code.... Is there anyway to optmize with my code to do this?
clone or copy works for me :)

Aug 25 '05 #9
an****************@gmail.com wrote:
[...]
Is it just me or does anyone else also think that, the operation
is more appropriately called clone rather than a copy?


How would you define the difference?
Aug 25 '05 #10
* Victor Bazarov:
an****************@gmail.com wrote:
[...]
Is it just me or does anyone else also think that, the operation
is more appropriately called clone rather than a copy?


How would you define the difference?


That a pure copy operation copies into existing (provided) storage?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 25 '05 #11
the diffrence? with what?

Aug 25 '05 #12
* la******@yahoo.com:
the diffrence? with what?


Please teach yourself common rules for quoting by checking typical postings
in this group.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 25 '05 #13
I am not sure i get whats going on.

Aug 25 '05 #14
la******@yahoo.com wrote:
Yes your code does, but i would like to not have to redo all of my
existing code.... Is there anyway to optmize with my code to do this?
clone or copy works for me :)


Your code is here

ComplexMatrix Cmxcopy(ComplexMatrix mat, int rows, int cols)
{

int i;
int j;
ComplexMatrix temp;
temp=CmxAlloc(rows,cols);

for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
{
temp[i][j].r=mat[0][0].r;
temp[i][j].i=mat[0][0].i;
}

return temp;

}

And if you are not willing to avoid those two for loops, I am
wondering what is left to optimize in this? Converting those
two float assignments into a memcpy is not going to make that
much of a difference. Since you will introduce one more
function call (or can it be inlined?) it can actually reduce
performance.

Rgds,
anna

Aug 26 '05 #15
Alf P. Steinbach wrote:
* Victor Bazarov:
an****************@gmail.com wrote:
[...]
Is it just me or does anyone else also think that, the operation
is more appropriately called clone rather than a copy?


How would you define the difference?


That a pure copy operation copies into existing (provided) storage?


Thats what I had in mind. The C standard functions like strcpy,
memcpy, is done this way only.

Rgds,
anna

Aug 26 '05 #16
I think that you just should copy the whole block of memory of the
matrix, like below:

struct complexMatrix
{
float r;
float i;
};

typedef complexMatrix** ComplexMatrix;
typedef complexMatrix Complex;

ComplexMatrix CmxCopy(ComplexMatrix mat, int rows, int cols)
{
ComplexMatrix temp;
temp=CmxAlloc(rows,cols);
::memcpy(temp, mat, rows * cols * sizeof(complexMatrix));
return temp;
}

and just like what Frank said, you should remember to realloc the
memory allocated in the function, after you don't use it.

Aug 27 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: AMT2K5 | last post by:
Hello folks. I am at the chapter in my textbook towards learning about dynamic memory and am practicing with a few examples and lessons from a variety of places. I am confused about one certain...
5
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
2
by: Richard | last post by:
Define a record structure to hold a set of test scores, number of tests, and the average score. The number of records are to be determined at runtime. Also the number of tests for each record is...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
12
by: whitehatmiracle | last post by:
Hi all Im not quite sure how to use the new and delete for a whole array dynamically. Actually i want that, a user inputs a char in a single char array. Everytime he inputs a char he creates a...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
19
by: smarty | last post by:
how can I find the memory allocated dynamically? is there any possibility of finding it?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.