gpt4 book ai didi

c - memmove 只移动一半的数字

转载 作者:行者123 更新时间:2023-11-30 19:31:04 27 4
gpt4 key购买 nike

移动 2D double 组时遇到问题。在下面的代码中,我有
双权重[wt][2]
我可以将 Weights[0][0] 移动到 Weights[0][1],但只能在要移动的字节数末尾添加“*2”。为什么当我使用“wt * sizeof(double)”时它只移动大约一半的数组。

    #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 ;
}

输出“wt * sizeof(double)*2”移动所有数字

    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,

带有“wt * sizeof(double)”的输出仅移动数字的前半部分

    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,

为什么它可以使用“*2”,而大小应该是 169 * 8 = 1352 字节,而不是 2704 字节

最佳答案

按照您声明权重的方式,元素按以下顺序出现在内存中:

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

您似乎期望它们按以下顺序排列:

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

你的memmove()正在将一半的已初始化元素复制到其之前的未初始化元素中。它还将一半未初始化的元素复制到其之前的已初始化元素,从而丢弃该值。

如果您希望一组的所有 169 个权重在内存中彼此连续,则需要反转 Weights 的声明和使用中下标的顺序。 (例如double Weights[2][wt];)

关于c - memmove 只移动一半的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49593876/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com