简体   繁体   English

C ++中int数组的memcpy的正确大小

[英]Correct size for memcpy for int arrays in C++

I'm new to C++ and would appreciate help with this problem. 我是C ++的新手,希望能帮助您解决此问题。 I've tried googling for examples, but can't seem to find quite what I'm looking for. 我已经尝试使用Google搜索示例,但似乎找不到我要找的东西。 I have a main function that allocates some space for an int array and a helper function (getRandomNumberArray) that populates that space with a random number array. 我有一个主要函数 ,它为int数组分配了一些空间,还有一个辅助函数 (getRandomNumberArray),该函数用一个随机数数组填充了该空间。

I'm printing the array as it is created in the helper function, and then after it's transferred to the memory allocated in the main function. 我正在打印在辅助函数中创建的数组,然后将其转移到主函数中分配的内存中。 And I lose a bunch of values inexplicably. 我莫名其妙地失去了一堆价值观。

The code below outputs this: 下面的代码输出:

95 70 24 40 26 7 84 45 62 13 41 11 21 55 23 50 17 8 76 20 94 25 90 29 95 26 9 44 47 8 61 27 42 10 15 26 53 64 55 90 79 28 72 46 50 87 86 36 87 33 33 29 4 65 56 65 12 30 14 59 25 42 69 99 47 2 42 25 80 90 32 51 17 14 74 83 94 34 92 88 51 85 78 71 9 38 54 17 34 19 47 89 73 43 38 99 48 8 84 81 95 70 24 40 26 7 84 45 62 13 41 11 21 55 23 50 17 8 76 20 94 25 90 29 95 26 9 44 47 8 61 27 42 10 15 26 53 64 55 90 79 28 72 46 50 87 86 36 87 33 33 29 4 65 56 65 12 30 14 59 25 42 69 99 47 2 42 25 80 90 32 51 17 14 74 83 94 34 92 88 51 85 78 71 9 38 54 17 34 19 47 89 73 43 38 99 48 8 84 81

95 70 24 40 26 7 84 45 62 13 41 11 21 55 23 50 17 8 76 20 94 25 90 29 95 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 95 70 24 40 26 7 84 45 62 13 41 11 21 55 23 50 17 8 76 20 94 25 90 29 95 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

As you can see, up to a point, the values are set, but then it's all zeros. 如您所见,设置值到一定程度,但是全为零。 Any idea what's going on here and how I can fix it? 知道这里发生了什么以及如何解决吗?

Thanks! 谢谢!

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


void getRandomNumberArray(int *result, int size){
    srand(time(0));

   int randomNos[100] = {0};

   for(int i = 0; i < 100; i++){
      randomNos[i] = rand() % 100 + 1;
      cout << randomNos[i] << ' ';
   }

   memcpy(result, randomNos,size);

}

int main() {
    int size = 100;
    int *randomNumbers = new int[size];
    getRandomNumberArray(randomNumbers,size);
    cout << "" << endl;
    for(int i = 0; i < 100; i++){
          cout << randomNumbers[i] << ' ';
       }

    free(randomNumbers);

   return 0;
}
  1. Why not just put if straight into the array in the first place? 为什么不直接放入数组呢?
  2. If you insist use memcpy(result, randomNos,size * sizeof(int)); 如果您坚持使用memcpy(result, randomNos,size * sizeof(int));
  3. Using C++ you use delete[] not free 使用C ++时,使用delete[]并非free
  4. Could be better off using vector - See http://www.cplusplus.com/reference/vector/vector/ 使用vector可能会更好-请参见http://www.cplusplus.com/reference/vector/vector/

int have usually a size of 4 bytes.Use int通常具有4个字节的大小。

size * sizeof(*randomNos)

instead. 代替。 sizeof(*randomNos) will return the size of one element of the array, which is the size of an int sizeof(* randomNos)将返回数组一个元素的大小,即一个int的大小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM