-1

Having a problem moving a 2D array of doubles. In the code below, I have
double Weights[wt][2]
I can move the Weights[0][0] to Weights[0][1], but only by adding "*2" to the end of how many bytes to move. Why when I use "wt * sizeof(double)" it only moves about half the array.

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

    #define wt 169  

    int main(void)
    {
    double Weights[wt][2]  ;
    double randnum = 0 ;
    int randint = 0 ;
    int i = 0 ;

        srand(time(NULL)) ;

        randnum = (double) rand() ;
        randint = (int)randnum ;

        for (i=0; i<wt; i++)    // Initialize weights
        {
            while (randnum > 1)
                randnum /= 10 ;

            Weights[i][0] = randnum ;

            randnum = (double)rand() ;
        }

        for (i=0; i<wt; i++)
            printf("%lf, ", Weights[i][0]) ;
        printf("\n\n") ;

        memmove(&Weights[0][1], &Weights[0][0], wt * sizeof(double)*2) ;

        for (i=0; i<wt; i++)
            printf("%lf, ", Weights[i][1]) ;
        printf("\n\n") ;

    return 0 ;  
    }

Output with "wt * sizeof(double)*2" moves all the numbers

    0.195398, 0.127509, 0.124090, 0.541791, 0.165211, 0.966964, 0.937778, 0.147996, 0.145539, 0.174831, 0.492294, 0.182906, 0.317039, 0.194350, 0.674522, 0.162258, 0.133443, 0.546239, 0.397388, 0.159630, 0.343595, 0.110326, 0.186186, 0.133299, 0.150245, 0.904083, 0.166712, 0.567598, 0.147243, 0.243560, 0.202927, 0.127892, 0.151865, 0.587645, 0.182071, 0.102328, 0.972841, 0.611008, 0.355752, 0.280744, 0.211837, 0.848045, 0.210981, 0.528876, 0.644063, 0.636847, 0.397108, 0.197850, 0.691471, 0.401359, 0.142732, 0.103507, 0.150461, 0.114170, 0.220569, 0.859577, 0.204578, 0.188769, 0.142717, 0.137072, 0.213124, 0.130896, 0.502159, 0.150241, 0.131484, 0.175389, 0.378205, 0.140194, 0.786397, 0.733956, 0.420938, 0.998233, 0.158200, 0.383263, 0.152711, 0.785811, 0.102011, 0.153108, 0.205708, 0.171158, 0.193244, 0.133691, 0.599164, 0.128957, 0.331123, 0.819733, 0.166431, 0.229418, 0.559934, 0.142884, 0.160014, 0.543696, 0.590316, 0.210230, 0.204611, 0.190515, 0.130202, 0.276829, 0.204535, 0.916599, 0.101079, 0.318801, 0.191483, 0.445303, 0.702064, 0.129446, 0.523884, 0.172217, 0.678055, 0.433479, 0.128627, 0.463012, 0.177039, 0.188544, 0.175258, 0.210151, 0.557686, 0.175425, 0.183448, 0.111762, 0.103560, 0.178359, 0.166132, 0.162592, 0.173840, 0.155994, 0.138359, 0.186860, 0.183677, 0.128145, 0.637718, 0.700070, 0.160025, 0.405066, 0.114537, 0.154831, 0.169952, 0.166926, 0.187701, 0.230096, 0.210274, 0.101579, 0.693108, 0.172564, 0.753747, 0.298207, 0.167967, 0.131143, 0.205245, 0.186312, 0.281571, 0.940572, 0.149922, 0.194289, 0.419006, 0.109014, 0.135534, 0.180259, 0.811259, 0.104463, 0.936559, 0.144898, 0.174470, 0.389326, 0.185404, 0.742589, 0.544157, 0.140608, 0.264363, 
    0.195398, 0.127509, 0.124090, 0.541791, 0.165211, 0.966964, 0.937778, 0.147996, 0.145539, 0.174831, 0.492294, 0.182906, 0.317039, 0.194350, 0.674522, 0.162258, 0.133443, 0.546239, 0.397388, 0.159630, 0.343595, 0.110326, 0.186186, 0.133299, 0.150245, 0.904083, 0.166712, 0.567598, 0.147243, 0.243560, 0.202927, 0.127892, 0.151865, 0.587645, 0.182071, 0.102328, 0.972841, 0.611008, 0.355752, 0.280744, 0.211837, 0.848045, 0.210981, 0.528876, 0.644063, 0.636847, 0.397108, 0.197850, 0.691471, 0.401359, 0.142732, 0.103507, 0.150461, 0.114170, 0.220569, 0.859577, 0.204578, 0.188769, 0.142717, 0.137072, 0.213124, 0.130896, 0.502159, 0.150241, 0.131484, 0.175389, 0.378205, 0.140194, 0.786397, 0.733956, 0.420938, 0.998233, 0.158200, 0.383263, 0.152711, 0.785811, 0.102011, 0.153108, 0.205708, 0.171158, 0.193244, 0.133691, 0.599164, 0.128957, 0.331123, 0.819733, 0.166431, 0.229418, 0.559934, 0.142884, 0.160014, 0.543696, 0.590316, 0.210230, 0.204611, 0.190515, 0.130202, 0.276829, 0.204535, 0.916599, 0.101079, 0.318801, 0.191483, 0.445303, 0.702064, 0.129446, 0.523884, 0.172217, 0.678055, 0.433479, 0.128627, 0.463012, 0.177039, 0.188544, 0.175258, 0.210151, 0.557686, 0.175425, 0.183448, 0.111762, 0.103560, 0.178359, 0.166132, 0.162592, 0.173840, 0.155994, 0.138359, 0.186860, 0.183677, 0.128145, 0.637718, 0.700070, 0.160025, 0.405066, 0.114537, 0.154831, 0.169952, 0.166926, 0.187701, 0.230096, 0.210274, 0.101579, 0.693108, 0.172564, 0.753747, 0.298207, 0.167967, 0.131143, 0.205245, 0.186312, 0.281571, 0.940572, 0.149922, 0.194289, 0.419006, 0.109014, 0.135534, 0.180259, 0.811259, 0.104463, 0.936559, 0.144898, 0.174470, 0.389326, 0.185404, 0.742589, 0.544157, 0.140608, 0.264363, 

Output with "wt * sizeof(double)" only moves the first half of the numbers

    0.153249, 0.333985, 0.315349, 0.134140, 0.101261, 0.191920, 0.135506, 0.126694, 0.320255, 0.179945, 0.723870, 0.450292, 0.147805, 0.166899, 0.140249, 0.194037, 0.143623, 0.395438, 0.161701, 0.212640, 0.147223, 0.579871, 0.115994, 0.192346, 0.165274, 0.162130, 0.142833, 0.491645, 0.187743, 0.500989, 0.142792, 0.126244, 0.834974, 0.174327, 0.456355, 0.184758, 0.151499, 0.181142, 0.967037, 0.183524, 0.146338, 0.169091, 0.138052, 0.793942, 0.121242, 0.154054, 0.586830, 0.501165, 0.193598, 0.563521, 0.713805, 0.126073, 0.114339, 0.187375, 0.103670, 0.176708, 0.134757, 0.317550, 0.111237, 0.107752, 0.818539, 0.153916, 0.192476, 0.165351, 0.113494, 0.648831, 0.135361, 0.502446, 0.312766, 0.173164, 0.190206, 0.177615, 0.186407, 0.328257, 0.422603, 0.929002, 0.186880, 0.100943, 0.143017, 0.165729, 0.106579, 0.214397, 0.770535, 0.118013, 0.187023, 0.180724, 0.799717, 0.107032, 0.212479, 0.910953, 0.348812, 0.795843, 0.302626, 0.192825, 0.301873, 0.143757, 0.841656, 0.165548, 0.194001, 0.115442, 0.182865, 0.213022, 0.783083, 0.154524, 0.310994, 0.120569, 0.326754, 0.323083, 0.676368, 0.175692, 0.168960, 0.113342, 0.175341, 0.312651, 0.166064, 0.147616, 0.211989, 0.965781, 0.398994, 0.209719, 0.187673, 0.399342, 0.745553, 0.318769, 0.592167, 0.104743, 0.146945, 0.143382, 0.555426, 0.126198, 0.440760, 0.236591, 0.124471, 0.122384, 0.178183, 0.155571, 0.282046, 0.210858, 0.158802, 0.349683, 0.171802, 0.113013, 0.148311, 0.132394, 0.144278, 0.164917, 0.652620, 0.141519, 0.467467, 0.105161, 0.136490, 0.196718, 0.145096, 0.211045, 0.228595, 0.204312, 0.101039, 0.169804, 0.132946, 0.156582, 0.812532, 0.177022, 0.180241, 0.205725, 0.846581, 0.143675, 0.146547, 0.112863, 0.139785, 
    0.153249, 0.333985, 0.315349, 0.134140, 0.101261, 0.191920, 0.135506, 0.126694, 0.320255, 0.179945, 0.723870, 0.450292, 0.147805, 0.166899, 0.140249, 0.194037, 0.143623, 0.395438, 0.161701, 0.212640, 0.147223, 0.579871, 0.115994, 0.192346, 0.165274, 0.162130, 0.142833, 0.491645, 0.187743, 0.500989, 0.142792, 0.126244, 0.834974, 0.174327, 0.456355, 0.184758, 0.151499, 0.181142, 0.967037, 0.183524, 0.146338, 0.169091, 0.138052, 0.793942, 0.121242, 0.154054, 0.586830, 0.501165, 0.193598, 0.563521, 0.713805, 0.126073, 0.114339, 0.187375, 0.103670, 0.176708, 0.134757, 0.317550, 0.111237, 0.107752, 0.818539, 0.153916, 0.192476, 0.165351, 0.113494, 0.648831, 0.135361, 0.502446, 0.312766, 0.173164, 0.190206, 0.177615, 0.186407, 0.328257, 0.422603, 0.929002, 0.186880, 0.100943, 0.143017, 0.165729, 0.106579, 0.214397, 0.770535, 0.118013, 0.187023, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 

Why does it work with "*2", when the size should be 169 * 8 = 1352 bytes, not 2704 bytes

2 Answers2

0

The way you've declared Weights, the elements appear in memory in this order:

Weights[0][0], Weights[0][1], Weights[1][0], Weights[1][1], Weights[2][0], ...

You seem to expect that they are in this order:

Weights[0][0], Weights[1][0], Weights[2][0], ..., Weights[168][0], Weights[168][1], Weights[0][1], Weights[1][1], ...

Your memmove() is copying half of the initialized elements to the uninitialized element just before it. It's also copying half of the uninitialized elements to the initialized element just before it, trashing that value.

If you want all of the 169 weights for one set to be contiguous in memory with each other, you need to reverse the order of the subscripts in both the declaration and uses of Weights. (E.g. double Weights[2][wt];)

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • You're absolutely correct. I declared the array [0-168] [0] and [0-168[1] so I expected [0][0], [1][0], [2][0], [3][0],....
    – Brian Lafferry Apr 01 '18 at 03:18
0

I had no idea order of the declaration mattered, as long as it was consistant. Code below works as ecpected

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

    #define wt 169  

    int main(int)
    {
    double Weights[2][wt]  ;
    double randnum = 0 ;
    int randint = 0 ;
    int i = 0 ;

        srand(time(NULL)) ;

        randnum = (double) rand() ;
        randint = (int)randnum ;

        for (i=0; i<wt; i++)    // Initialize weights
        {
            while (randnum > 1)
                randnum /= 10 ;
            Weights[0][i] = randnum ;
            randnum = (double)rand() ;
        }

        for (i=0; i<wt; i++)
            printf("%lf, ", Weights[0][i]) ;
        printf("\n\n") ;

        memmove(&Weights[1][0], &Weights[0][0], wt * sizeof(double)) ;

        for (i=0; i<wt; i++)
            printf("%lf, ", Weights[1][i]) ;
        printf("\n\n") ;        

    return 0 ;  
    }

Thanks, I'll try switching declaration in the future :)