-4

だから私は機能を持っています

f(int D[],int A[],int len){ 
   for(int k = 0; k < len; k++)
      D[k] = A[k];

私が出力した場合D、数字はすべて間違っています。関数は関数のように初期化されてf呼び出され、関数で出力して問題ないように見えるので、すべて問題ありません。だから... 問題がどこにあるのか理解できません... 私も試しましたが、うまくいきません。Dint* D = new int[100000];mainAmemcpy(D+k,A+k,sizeof(int));

4

4 に答える 4

1

あなたのループは完全に機能します。問題はコードの別の場所にあるはずです。

forループ、memcpy、およびの 3 つの異なる方法でデータをコピーするプログラムの例を次に示しますstd::copy

#include <algorithm>
#include <cstring>
#include <iostream>
#include <iterator>

void copy1(int D[], int A[], int len) {
  for(int k = 0; k < len; k++)
    D[k] = A[k];
}

void copy2(int D[], int A[], int len) {
  std::memcpy(D, A, len*sizeof(int));
}

void copy3(int D[], int A[], int len) {
  std::copy(A, A+len, D);
}

int main () {
  int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  int *d = new int[10];

  std::ostream_iterator<int> out(std::cout, ",");

  // First, print the initial values
  std::copy(d, d+10, out);
  std::cout << "\n";

  // Next do the copies and print the values again
  copy1(d, a, 10);
  std::copy(d, d+10, out);
  std::cout << "\n";

  copy2(d, a, 10);
  std::copy(d, d+10, out);
  std::cout << "\n";

  copy3(d, a, 10);
  std::copy(d, d+10, out);
  std::cout << "\n";
}

私が得る出力は次のとおりです。

0,0,0,0,0,0,0,0,0,0,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
1,2,3,4,5,6,7,8,9,10,
于 2012-07-07T00:13:21.240 に答える
0

問題は、配列 int* D = new int[100000]; へのポインターを渡していることだと思います。

ただし、関数は 2 つの整数配列を取り、これは配列へのポインターと同じではありません。

これは、あなたが望むように動作するスニペットの SSCCE です。

#include <iostream>
using namespace std;

void f(int * d, int * a, int length){
    for(int k = 0; k < length; k++){
            d[k] = a[k];
    }
}

int main() {
    int* a = new int[9];
    for(int i = 0; i < 9; i++){a[i] = i;}
    int* d = new int[9];
    f(d, a, 9);

    for(int i = 0; i < 9; i++){
            cout << d[i] << " ";
    }
}
于 2012-07-07T00:14:00.043 に答える
0

Start with a minimal working example such as the following, then integrate your other code into it until something breaks. That’ll be the source of your error. Without more information, this is pretty much all I can tell you; when you encounter wrong values in a C++ program, you’re likely reading from an uninitialised variable. I suggest you try stepping through your program in a debugger such as GDB.

#include <algorithm>
#include <iostream>
#include <iterator>

void f(int D[], const int A[], int len){ 
   for(int k = 0; k < len; k++)
      D[k] = A[k];
}

int main(int argc, char** argv) {
  int A[] = { 1, 2, 3, 4, 5 };
  int D[5];
  f(D, A, 5);
  std::copy(A, A + 5, std::ostream_iterator<int>(std::cout, " "));
  std::cout << '\n';
  std::copy(D, D + 5, std::ostream_iterator<int>(std::cout, " "));
  return 0;
}

Unless you have a very good reason to do so, prefer std::vector and std::array over raw arrays. Learning how arrays and pointers work is a good reason to use them, though.

于 2012-07-07T00:09:41.053 に答える
0

デバッガーでコードを実行し、最初にガベージがないかどうかを確認しますA[](関数呼び出し後に問題が発生する可能性があります)。また、参照を渡すことをお勧めします (の変更を防ぐためにint & D[]、 andconst int & A[]のように)。constA

于 2012-07-07T00:04:53.283 に答える