Author Topic: Copying 2D array contents into another 2D array  (Read 4628 times)

0 Members and 1 Guest are viewing this topic.

Offline rill17Topic starter

  • Contributor
  • Posts: 15
  • Country: mt
Copying 2D array contents into another 2D array
« on: May 01, 2017, 04:46:22 pm »
Hi, i need to copy the contents of a 2D array into another 2D array of the same size on a microcontroller. i tried an iterative approach using for loops as shown in the code below but it doesn't work. Any ideas?
int a = 0;
for( a=0; a<7; a++);
{
          int b = 0;
          for (b=0; b<256; b++);
          {
                    pulse_b[a] = pulse[a];
           }
}
 

Offline Farley

  • Regular Contributor
  • *
  • Posts: 88
  • Country: us
Re: Copying 2D array contents into another 2D array
« Reply #1 on: May 01, 2017, 04:56:54 pm »
Quote
for( a=0; a<7; a++);
.
.
.
for (b=0; b<256; b++);

Remove the semicolons at the ends of the quoted lines.
Also, check the logic of what you're attempting. Research 2d arrays.
« Last Edit: May 01, 2017, 05:03:56 pm by Farley »
 

Offline RoGeorge

  • Super Contributor
  • ***
  • Posts: 6185
  • Country: ro
Re: Copying 2D array contents into another 2D array
« Reply #2 on: May 01, 2017, 05:06:00 pm »
For max speed I would use sizeoff and memcpy.
Carefull with memcpy, it is fast, but tricky.
 
The following users thanked this post: hamster_nz

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1799
  • Country: us
Re: Copying 2D array contents into another 2D array
« Reply #3 on: May 01, 2017, 05:09:00 pm »
Once you remove the semicolons as Farley notes, you will find that your code is copying each of the 7 elements (0 through 6) from the first array into the second array, 256 times.

(Inside the inner loop, you do the same assignment 256 times.)

For two-D arrays, you'll need the inner statement to look something like:

Code: [Select]
pulse_alternate[a][b] = pulse [a][b];
or

Code: [Select]
pulse_alternate[a*DIM + b] = pulse[a*DIM + b];
where DIM is the size of the array along the "b" dimension (in this case, 256).

Edit: Added code blocks to make clear what I was writing.
« Last Edit: May 01, 2017, 06:34:19 pm by sokoloff »
 

Offline ynfo

  • Contributor
  • Posts: 20
  • Country: gb
Re: Copying 2D array contents into another 2D array
« Reply #4 on: May 01, 2017, 05:42:21 pm »
There are no 2D arrays in your code!

2D arrays have 2 indices e.g. a[1][2]
 

Offline timothyaag

  • Contributor
  • Posts: 43
  • Country: us
Re: Copying 2D array contents into another 2D array
« Reply #5 on: May 01, 2017, 05:54:48 pm »
He's not using a code block, but if you quote him you can see he's actually doing this:
Code: [Select]
pulse_b[a][b] = pulse[a][b];
 

Online wraper

  • Supporter
  • ****
  • Posts: 16849
  • Country: lv
Re: Copying 2D array contents into another 2D array
« Reply #6 on: May 01, 2017, 05:58:18 pm »
Remove the semicolons at the ends of the quoted lines.
This
Quote
Also, check the logic of what you're attempting. Research 2d arrays.
What he posted was crippled by the forum:
He's not using a code block, but if you quote him you can see he's actually doing this:
Code: [Select]
pulse_b[a][b] = pulse[a][b];
+1
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4028
  • Country: nz
Re: Copying 2D array contents into another 2D array
« Reply #7 on: May 01, 2017, 06:05:19 pm »
Hi, i need to copy the contents of a 2D array into another 2D array of the same size on a microcontroller. i tried an iterative approach using for loops as shown in the code below but it doesn't work. Any ideas?

Code: [Select]
int a = 0;
for( a=0; a<7; a++);
{
          int b = 0;
          for (b=0; b<256; b++);
          {
                    pulse_b[a][b] = pulse[a][b];
           }
}

Don't do this:

Code: [Select]
int a = 0;
for( a=0; a<7; a++);

Do this:

Code: [Select]
for(int a=0; a<7; a++);

I suspect you started with that, and the compiler told you there was a problem that a (and b) were not defined in the
Code: [Select]
pulse_b[a][b] = pulse[a][b]; line.

But you didn't understand what the compiler told you and "fixed" it in entirely the wrong way.
 

Offline rill17Topic starter

  • Contributor
  • Posts: 15
  • Country: mt
Re: Copying 2D array contents into another 2D array
« Reply #8 on: May 01, 2017, 06:11:23 pm »
yeah the code i am using is without the for loop semicolons and was modified due to the post shortcuts, the actual code is as follows
Code: [Select]
int a = 0;
                                for( a=0; a<7; a++)
                                {
                                    int b = 0;
                                    for (b=0; b<256; b++)
                                    {
                                        pulse_b[a][b] = pulse[a][b];
                                    }
                                }
ill try the memcpy and your other suggestions and report back, thanks
 

Online wraper

  • Supporter
  • ****
  • Posts: 16849
  • Country: lv
Re: Copying 2D array contents into another 2D array
« Reply #9 on: May 01, 2017, 06:12:38 pm »
Do this:

Code: [Select]
for(int a=0; a<7; a++);
First of all
for(int a=0; a<7; a++); will not work with many compilers.
Secondly, don't do this:
int a;
for(a=0; a<7; a++);
{
...
}
Do this:
int a;
for(a=0; a<7; a++)
{
...
}
 

Offline rill17Topic starter

  • Contributor
  • Posts: 15
  • Country: mt
Re: Copying 2D array contents into another 2D array
« Reply #10 on: May 01, 2017, 06:40:01 pm »
for anyone following, the simplest and most efficient way i found which works is by ditching the for loops and using memcpy. the following code works:
Code: [Select]
int i;
  for(i = 0; i<7; i++)
memcpy(&pulse_b[i], &pulse[i], sizeof(pulse[0]));
}
still cant figure out why the for loops didn't work though, thanks for the help
 

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1799
  • Country: us
Re: Copying 2D array contents into another 2D array
« Reply #11 on: May 01, 2017, 06:54:27 pm »
Don't stop here. (IMO)

Now that you have a way that works, power through and figure out why the "other" way that should have worked didn't.

Post your new (but broken) code here in a code block and we'll help you figure it out.
 

Offline MK14

  • Super Contributor
  • ***
  • Posts: 4527
  • Country: gb
Re: Copying 2D array contents into another 2D array
« Reply #12 on: May 01, 2017, 08:06:48 pm »

still cant figure out why the for loops didn't work though, thanks for the help

(as others are trying to explain)
The for loop, continually repeats the NEXT statement block.

So if you go for (..............) ;

The statement block is empty, so it does NOTHING. i.e. the semi-colon ends the statement block.

for (i = 0; i < 1000000; i++) ;
x = x + 1;

would just increase x by ONE not a million.

for (i = 0; i < 1000000; i++)
x = x + 1;

or better still

for (i = 0; i < 1000000; i++)
{
  x++;
}

would increase x by a million.
« Last Edit: May 01, 2017, 08:09:07 pm by MK14 »
 

Offline Nusa

  • Super Contributor
  • ***
  • Posts: 2416
  • Country: us
Re: Copying 2D array contents into another 2D array
« Reply #13 on: May 01, 2017, 08:57:32 pm »
For two identically sized arrays, it's as simple as:
Code: [Select]
memcpy(pulse_b, pulse, sizeof(pulse));

 

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1799
  • Country: us
Re: Copying 2D array contents into another 2D array
« Reply #14 on: May 01, 2017, 09:06:37 pm »
Also, find the compiler setting that will emit a warning for "empty if body" and "empty loop body".

For bonus points, turn on both that and "treat warnings as errors" (assuming you can find the pragmas to disable specific warnings that you cautiously verify as safe.
 

Offline helius

  • Super Contributor
  • ***
  • Posts: 3639
  • Country: us
Re: Copying 2D array contents into another 2D array
« Reply #15 on: May 01, 2017, 09:16:03 pm »
for anyone following, the simplest and most efficient way i found which works is by ditching the for loops and using memcpy. the following code works:
Code: [Select]
int i;
  for(i = 0; i<7; i++)
memcpy(&pulse_b[i], &pulse[i], sizeof(pulse[0]));
}
still cant figure out why the for loops didn't work though, thanks for the help
If the arrays were declared in the normal way, like
Code: [Select]
int pulse[48][48];then the elements will all be contiguous. You don't need to call memcpy() once per row, but can just call it once for the array.
Code: [Select]
memcpy(pulse_b, pulse, sizeof(pulse));This won't work if the array was really declared as an array of pointers:
Code: [Select]
int *pulse[48]; because the memory need not be contiguous.
 

Offline TNorthover

  • Contributor
  • Posts: 42
  • Country: us
Re: Copying 2D array contents into another 2D array
« Reply #16 on: May 02, 2017, 12:54:25 am »
First of all
for(int a=0; a<7; a++); will not work with many compilers.

Really, really shitty compilers, to the extent that I'd not touch the chip with a barge pole if that was all that was available. The standard revision that added support is 18 years old already (more if you count C++), and if that's not supported yet I wouldn't trust them to get anything else right either.
« Last Edit: May 02, 2017, 12:57:09 am by TNorthover »
 
The following users thanked this post: newbrain

Offline helius

  • Super Contributor
  • ***
  • Posts: 3639
  • Country: us
Re: Copying 2D array contents into another 2D array
« Reply #17 on: May 02, 2017, 01:17:15 am »
Really, really shitty compilers, to the extent that I'd not touch the chip with a barge pole if that was all that was available. The standard revision that added support is 18 years old already (more if you count C++), and if that's not supported yet I wouldn't trust them to get anything else right either.
Code: [Select]
> gcc test.c
test.c: In function ‘main’:
test.c:11:3: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
   for (int i=0; i<12; ++i)
   ^
> cc -v
gcc version 4.9.2 (Debian 4.9.2-10)
« Last Edit: May 02, 2017, 02:30:42 am by helius »
 

Offline TNorthover

  • Contributor
  • Posts: 42
  • Country: us
Re: Copying 2D array contents into another 2D array
« Reply #18 on: May 02, 2017, 02:08:30 am »
Code: [Select]
> cc -v
gcc version 4.9.2 (Debian 4.9.2-10)

On modern systems the tool names dating from early UNIX (cc, cc1, ...) are often wrappers that tell the real compiler to use ancient standards so that old code still compiles. The underlying compiler is quite capable of supporting C99.

Newly written code shouldn't be using that (at least not without a -std=something_sane argument).
 

Offline helius

  • Super Contributor
  • ***
  • Posts: 3639
  • Country: us
Re: Copying 2D array contents into another 2D array
« Reply #19 on: May 02, 2017, 02:59:08 am »
On modern systems the tool names dating from early UNIX (cc, cc1, ...) are often wrappers that tell the real compiler to use ancient standards so that old code still compiles. The underlying compiler is quite capable of supporting C99.

On this system cc is not a wrapper. It is a symbolic link, as gcc is also.
Code: [Select]
lrwxrwxrwx 1 root root 20 May  1 16:32 /usr/bin/cc -> /etc/alternatives/cc
lrwxrwxrwx 1 root root     12 May  1 16:32 /etc/alternatives/cc -> /usr/bin/gcc
lrwxrwxrwx 1 root root  7 Feb 25  2015 /usr/bin/gcc -> gcc-4.9
It makes no difference which you type, because all these links act exactly the same.

Quote
Newly written code shouldn't be using that (at least not without a -std=something_sane argument).
-std=gnu90 is the default, which is C89. It stayed that way for a very long time, until GCC 5.1.0 changed it recently.
 

Offline TNorthover

  • Contributor
  • Posts: 42
  • Country: us
Re: Copying 2D array contents into another 2D array
« Reply #20 on: May 02, 2017, 03:21:20 am »
On this system cc is not a wrapper. It is a symbolic link, as gcc is also.

Same thing, or at least I decided not worth noting the difference unless you're trying to implement this kind of thing. You can either have a real wrapper of some code to look at argv[0] to decide how to behave.

(I do believe you that this particular behaviour doesn't change for you, especially as it doesn't for me on Linux. macOS isn't so lucky: cpp is a wrapper that implements a pre-ANSI preprocessor!)

Quote
It stayed that way for a very long time, until GCC 5.1.0 changed it recently.

Thank goodness. It should have happened a long time ago.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4028
  • Country: nz
Re: Copying 2D array contents into another 2D array
« Reply #21 on: May 02, 2017, 09:32:49 am »
On modern systems the tool names dating from early UNIX (cc, cc1, ...) are often wrappers that tell the real compiler to use ancient standards so that old code still compiles. The underlying compiler is quite capable of supporting C99.

On this system cc is not a wrapper. It is a symbolic link, as gcc is also.
Code: [Select]
lrwxrwxrwx 1 root root 20 May  1 16:32 /usr/bin/cc -> /etc/alternatives/cc
lrwxrwxrwx 1 root root     12 May  1 16:32 /etc/alternatives/cc -> /usr/bin/gcc
lrwxrwxrwx 1 root root  7 Feb 25  2015 /usr/bin/gcc -> gcc-4.9
It makes no difference which you type, because all these links act exactly the same.

Boy are you going to get a surprise when you learn how ccache works. Or busybox.

Or, to be more historical, ex and vi, which both link to the same executable, and always have, but behave differently.
 
The following users thanked this post: newbrain

Offline helius

  • Super Contributor
  • ***
  • Posts: 3639
  • Country: us
Re: Copying 2D array contents into another 2D array
« Reply #22 on: May 02, 2017, 01:47:46 pm »
Boy are you going to get a surprise when you learn how ccache works. Or busybox.
Or, to be more historical, ex and vi, which both link to the same executable, and always have, but behave differently.
Not at all. The sources for gcc aren't exactly a government secret: you can see for yourself that it does not care what argv[0] is when selecting a language standard (within the C family; it does behave differently for ADA et cetera).
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf