473,795 Members | 2,826 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3623
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*******@gmai l.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
18016
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 application using C++ and the STL for handling my data. Unfortunately, I must interact with a (vanilla) C API. I use vectors of strings (for simplicity and less memory hassle), but the function calls for this API require arrays of character...
4
3505
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 dynamically allocated member variables). Anyway, I have a program that segfaults without a copy constructor but if I add an empty one, the segfault is gone. The code is ugly indeed so I don't want to post it, but, in general terms, what sort of error...
4
4802
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 explain my situation detailed. double ** matrixa, **matrixb; int nrow = 10, mcol = 10; matrixa = initmatrix(nrow, mcol); // allocate memory a matrixb = initmatrix(nrow, mcol); // allocate memory b // copy a => b for (int i=0;i<nrow;i++)
3
16782
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 which uses the "..." as the last parameter how can I pass them to the orignal printf ? void myprintf(char *txt, ...) printf(txt, ???????); }
4
8828
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 a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
7
7417
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 example, the manner in which I update element 1 depends on several other (randomly numbered) elements in the array. So, I can't change an element until I've figured out how every element changes.
7
1731
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 != copy.Length; i++)
2
5145
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 std; class var_array { private: int *data; // The data
3
3338
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 elements which i want to keep and copy this array to the original one as follows This function to create two arrays it depends on the parameter x. void Econ::CreateFirms(int array,int x) { if(x==1) { FirmArray= new Firm*;
5
3186
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 use std::t1::array::data() and assign the returned value to the c-style pointer; if STL does not support this, is there any other library that has such a container (maybe BOOST)?
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10438
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10164
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7540
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.