0

Actually when writting this question, another question a shorter one came to my mind so i will ask it first:

1st Question (Shorter):

I have a header with struct defined in this manner:

typedef struct _CAMERA_LIST
{
....
}CAMERA_LIST, *PCAMERA_LIST;

What is the meaning of this syntax? (note the difference in the struct name and the names after closing brace)

2nd Question (Longer):

I have a code example from hardware manufacturer showing how to properly initialize the hardware that Im using. In the code manufacturer uses raw pointers and in my application i would prefer to use boost scoped_ptr or shared_ptr. The problem is that memory block that i want to allocate is different size than that allocated by regular new ObjectType;

Here is the short version of what i got from the manufacturer's site:

int nNumCam = 3;
CAMERA_LIST* pucl;
pucl = (CAMERA_LIST*) new BYTE [sizeof (DWORD) + nNumCam * sizeof (CAMERA_INFO)];
pucl->dwCount = nNumCam;
printf("Camera %i Id: %d", iCamera, pucl->uci[1].dwCameraID);

And here is what i want to get:

int nNumCam = 3;
scoped_ptr<CAMERA_LIST> pucl;
pucl.reset( (CAMERA_LIST*) new BYTE [sizeof (DWORD) + nNumCam * sizeof (CAMERA_INFO)] );
pucl->dwCount = nNumCam;
printf("Camera %i Id: %d", iCamera, pucl->uci[1].dwCameraID);

Here is how the struct looks like exactly:

typedef struct _CAMERA_LIST
{
DWORD dwCount;
CAMERA_INFO uci[1];
}CAMERA_LIST, *PCAMERA_LIST;

The question is: How do I get this functionality to work with scoped_ptr/shared_ptr? Also note that in the code snippet the deletion of the memory block is done via delete[] not delete. Do i need to use scoped_array? If so how do access struct fields?

there is a complete example from manufacturers site in case anyone finds it helpful:

INT nNumCam;
if( is_GetNumberOfCameras( &nNumCam ) == IS_SUCCESS) {
if( nNumCam >= 1 ) {
  // Create new list with suitable size
  CAMERA_LIST* pucl;
  pucl = (CAMERA_LIST*) new BYTE [sizeof (DWORD) + nNumCam * sizeof (CAMERA_INFO)];
  pucl->dwCount = nNumCam;

  //Retrieve camera info
  if (is_GetCameraList(pucl) == IS_SUCCESS) {
    int iCamera;
    for (iCamera = 0; iCamera < (int)pucl->dwCount; iCamera++) {
      //Test output of camera info on the screen
      printf("Camera %i Id: %d", iCamera,
      pucl->uci[iCamera].dwCameraID);
    }
  }
}
delete [] pucl;
}
Marcin K.
  • 683
  • 1
  • 9
  • 20

1 Answers1

1

This is a job for a custom deleter.

struct delete_bytearr {
    void operator() ( void * ptr ) const
        { delete [] (BYTE *) ptr; }
};

boost::shared_ptr (and std::shared_ptr) accept a deleter object as a second constructor argument, but scoped_ptr does not. Its descendent std::unique_ptr does accept a custom deleter, and you should probably prefer the standard facilities moving forward.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • I thought I would need to use custom deleter just couldnt find a scoped_ptr constructor which accepts one as argument. Are there any differences between std::unique_ptr and boost::scoped_ptr that i should know about ? (other than the first one does accept a custom deleter as 2nd argument) – Marcin K. May 04 '14 at 12:15
  • @MarcinKorn Nope, they're only as simple as they appear on the surface. The default behavior of `unique_ptr` comes via a separate deleter class template, `std::default_delete`. – Potatoswatter May 04 '14 at 12:21
  • I cant get unique_ptr to compile in my environment - #if __cplusplus >= 201103L condition in not fullfilled so I will just go with shared_ptr. Is the following code fine? boost::shared_ptr camera_list_ptr; // as class member boost::shared_ptr tmp( (UEYE_CAMERA_LIST*) new BYTE [sizeof (DWORD) + number_of_cameras * sizeof (UEYE_CAMERA_INFO)], camera_list_deleter()); camera_list_ptr = tmp; – Marcin K. May 04 '14 at 13:59
  • @MarcinKorn What compiler are you using? Did you pass `-std=c++11`? Yes, that looks fine, if `camera_list_deleter` is along the lines of my example. – Potatoswatter May 04 '14 at 22:11
  • gcc 4.8. I thought my compiler doesnt have c++11 support since I couldnt make unique_ptr work, but I was wrong. It DOES support c++11, but i was adding compiler flags in my IDE in incorrect way. Could you add answer to question #1 also? Someone already answered it here but removed the answer. I will then mark your answer as best and problem as solved. – Marcin K. May 05 '14 at 18:06
  • @MarcinKorn See http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c ; it looks like the answers have some good links too. Also http://stackoverflow.com/a/1084204/153285 – Potatoswatter May 06 '14 at 02:14