vendredi 26 mai 2017

c++: pointer access issue(solves if text file is read)

I encountered a strange issue in accessing a pointer array content when it is being passed as a argument. Here is my function,

template <typename T>
  void compare_input(T *inImage,string FileName, int inputSize )
  {

      std::cout<<"\ncompare_input: first: "<<(T)inImage[0];
    //int inputSize = 227*227*3;
    T *pling = (T *)malloc(sizeof(T) * inputSize);
    std::cout<<"\ncompare_input: first: "<<inImage[0];
    if(io::readTextFile<T>(caffe_outFIleName,pling) == 0) //hex reading
    {
      std::cout<<"input_image reading failed\n";
      free(pling);
      exit(0);
    }
    std::cout<<"\ncompare_input: first: "<<inImage[0];
    int nonEqualActivations=0;
    for(int l=0;l<inputSize;l++)//this loop identifies how many floating values are different atleast by .1
    {
      if((fabs((T)pling[l] - (T)inImage[l])*10) > ((T)0.01*10))
          nonEqualActivations++;
    }
    std::cout<<"\ncompare_input: first: "<<inImage[0];
    free(pling);
    std::cout<<"\ncompare_input -> nonEqualInput: "<<nonEqualActivations;
    std::cout<<"\nand address is: "<<inImage;
  }

i wanted to compare inImage(a float *) and content in text file(FileName), whenever i tried printing the first value in inImage array(before text file reading) it gives me a garbage value, but once text file is being read it prints exact value. the output of it look like

compare_input: first: -3.15812e-28 compare_input: first: -3.1586e-28 compare_input: first: 90.061 compare_input: first: 90.061 compare_input -> nonEqualInput: 0

i am just wondering what might be the issue? used -std=c++11 with g++.

Aucun commentaire:

Enregistrer un commentaire