Compiler keeps allocating the same identical memory address in loop iteration for NPP image

Hi Guys,

I need to create 10 of “ImageNPP_32f_C1”, store different images in them and keep their addresses in a structure for further access. My problem is that in each loop iteration, “myImage” always gets the same identical memory address! As a result all elements of “myArray” will have the same identical memory address.
Isn’t compiler supposed to allocate a new memory address for each iteration? Am I missing something?

std::deque<npp::ImageNPP_32f_C1 *> myArray ;
for(i=0 ; i<10 ; i++) {
npp::ImageNPP_32f_C1 myImage (640,480) ;

//.
//.
//processing and saving results in myImage
//.
//.

myArray.at(i) = &myImage ;
}

This has nothing to do with CUDA or NPP.

You’d get the same results if you took the address of any stack variable in a loop like that.

http://stackoverflow.com/questions/12446586/local-variables-defined-inside-for-loops-in-c

Even if the underlying class constructor had allocations going on, that would not impact the address of the stack variable (i.e. the address of the object instantiated on the stack).

If you use the c++ new operator to instantiate your object at each iteration, it should fix your issue. The pointer returned by new should be unique for each iteration.

Oh, right, thank you so much. Keeping the address of a local variable wouldn’t work.
I think for my purpose I need to dynamically allocate memory via a “new” in each iteration. Can you please let me know how one would do it?

npp::ImageNPP_32f_C1 *myImage = new ???

(what to put after “new”?)

Thanks.

Oh, right, thank you so much. Keeping the address of a local variable wouldn’t work.
I think for my purpose I need to dynamically allocate memory via a “new” in each iteration. Can you please let me know how one would do it?

npp::ImageNPP_32f_C1 *myImage = new ???

(what to put after “new”?)

Thanks.

This is just a C++ programming question now.

This seemed to work for me:

$ cat t1217.cu
#include <npp.h>
#include <ImagesNPP.h>
#include <iostream>
#include <deque>

const int ds = 10;

int main(){
  std::deque<npp::ImageNPP_32f_C1 *> myArray(ds) ;
  int i;
  for(i=0 ; i<ds ; i++) {
    npp::ImageNPP_32f_C1 *myImage = new npp::ImageNPP_32f_C1(640,480);
    std::cout << (unsigned long long int)myImage << std::endl;
    myArray.at(i) = myImage ;
    }
  return 0;
}
[user2@dc10 misc]$ nvcc -I/usr/local/cuda/samples/7_CUDALibraries/common/UtilNPP t1217.cu -lnppi -o t1217
[user2@dc10 misc]$ ./t1217
12887200
17484992
17485392
183274480
183274256
183274064
182384848
182387536
182390224
182392912
$

Awesome. Thank you very much.