473,387 Members | 1,497 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,387 software developers and data experts.

Copy memory (array)

Hi all,

I have a 2-D array with the size M x N. Now I need to segment this
array into two parts with sizes P x N and (M-P) x N.

P | M-P
|--------|------------------------|
| | |
| | |
| | | N
| | |
| | |
| | |
| | |
|--------|------------------------|
|
|<-- cutting line

If using the loop and copying point to point, I think it is not a fast
way especially when M and N are large. Is there any way in C to copy
the memory blocks so that it would be faster?

Thank you
Jan 28 '08 #1
5 3592
VijaKhara wrote:
Hi all,

I have a 2-D array with the size M x N. Now I need to segment this
array into two parts with sizes P x N and (M-P) x N.

P | M-P
|--------|------------------------|
| | |
| | |
| | | N
| | |
| | |
| | |
| | |
|--------|------------------------|
|
|<-- cutting line

If using the loop and copying point to point, I think it is not a fast
way especially when M and N are large. Is there any way in C to copy
the memory blocks so that it would be faster?
First, re-examine why you need to perform this copying to
begin with. Is it possible to rearrange your program's data
structures so the copy becomes unnecessary? The fastest possible
copy is the one you don't perform.

If you must copy, I'd suggest using a loop over each of the
N rows, using memcpy() twice to copy the first P and the final
M-P elements of each row:

for (i = 0; i < N; ++i) {
memcpy (lhs[i], orig[i], P * sizeof orig[i][0]);
memcpy (rhs[i], orig[i]+P, (M-P) * sizeof orig[i][0]);
}

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jan 28 '08 #2
VijaKhara <Vi*******@gmail.comwrites:
Hi all,

I have a 2-D array with the size M x N. Now I need to segment this
array into two parts with sizes P x N and (M-P) x N.

P | M-P
|--------|------------------------|
| | |
| | |
| | | N
| | |
| | |
| | |
| | |
|--------|------------------------|
|
|<-- cutting line

If using the loop and copying point to point, I think it is not a fast
way especially when M and N are large. Is there any way in C to copy
the memory blocks so that it would be faster?
You might find memcpy is faster than element copying, but then you
might also find it slower. The only way to know is to measure.

for (row = 0; row < N; row++) {
memcpy(left[row], big[row], P * sizeof big[0][0]);
memcpy(right[row], &big[row][P], (M - P) * sizeof big[0][0]);
}

[untested, un-compiled... unwise.]

--
Ben.
Jan 28 '08 #3
Thank you, guys. It works very well. I need to segment it to 2 parts
because each part will be a parameter for a different module.

Thanks again.
Jan 28 '08 #4
# If using the loop and copying point to point, I think it is not a fast
# way especially when M and N are large. Is there any way in C to copy
# the memory blocks so that it would be faster?

memcpy or memmove are likely as fast as anything else
possibly even faster, optimised for the machine and
special cased by the compiler. I'm not sure if you
have overlapping source and destination; if so use
memmove; otherwise you can use memcpy.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
If your job was as meaningless as theirs, wouldn't you go crazy too?
Jan 29 '08 #5
VijaKhara wrote:
) Thank you, guys. It works very well. I need to segment it to 2 parts
) because each part will be a parameter for a different module.

What's the module interface ?

If you write the module interface so that it takes an extra parameter
stating how many values to skip for each row. Then you don't need to
copy anything.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Jan 29 '08 #6

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

Similar topics

1
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an...
4
by: William Payne | last post by:
Hello, I was under the impression that if I made a class Foo and if I didn't specify a copy constructor I would get one anyway that simply assigns the member variables (and that won't work for...
4
by: Yudan Yi | last post by:
I have a problem to copy (assign) a matrix to another matrix. Curreny, I know copy the number using loops, while it will take some time, I wonder if there have faster method. The following code...
3
by: Douwe | last post by:
I try to build my own version of printf which just passes all arguments to the original printf. As long as I keep it with the single argument version everything is fine. But their is also a version...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
7
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For...
7
by: Richard Forester | last post by:
Hello. I need some help understanding what goes on when an array is copied. I create 2 arrays and copy one to the other: int pins = {9, 3, 7, 2}; int copy = new int; for (int i = 0; i !=...
2
by: pragtideep | last post by:
Kindly help me explain the behaviour of defult copy constructor . Why the destructor is freeing the SAME memory twice , though it was allocated just once . #include<iostream> using namespace...
3
by: aeo3 | last post by:
Hi All, Now, I am trying to build a project, I need to expand an array of pointer to classes. Moreover, this array includes some elements I want to delete them. So, I create another array, copy the...
5
by: zr | last post by:
Hi, Is there a way to initialize a std::tr1::array with a pre-allocated built-in array in a copy-less assignment, such that both will point to the same memory? Vice-versa is easy to do, simply...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.