5

i'm having a weird situation. i'm trying to implement a 10+ years old pci camera device SDK to my camera management software. Manifacturer no longer in business and i have no chance to get official help. So here I am, looking for some help to my ugly problem.

SDK comes with Visual Studio 6.0 samples. One of the include files has a structure ending with a one byte array like below;

typedef struct AVData {
    ...  
    BYTE audioVideoData[1];
}AVDATA, *PAVDATA;

But this single byte allocated byte array receives video frames and weird enough, it works fine with Visual Studio 6.0 version. If I try it with Visual Studio 2005/2008/2010, i start getting Memory Access Violation error messages which actully makes sense since it shouldn't be possible to allocate space to a fixed size array afterwards, no? But same code runs fine with VS 6.0?! It's probably caused by either compiler or c++ runtime differences but i'm not very experienced on this subject so it's hard to tell the certain reason for me.

I tried changing the size to an expected maximum number of bytes like below;

typedef struct AVData {
    ...  
    BYTE audioVideoData[20000];
}AVDATA, *PAVDATA;

This helped it get working but from time to time i get memory access violation problems when trying to destroy the decoder object of the library.

There is something definitely wrong with this. I don't have the source codes of the SDK, only the DLL, Lib and Header files. My questions are:

1) Is it really legal to allocate space to a fixed size array in Visual Studio 6.0 version?

2) Is there any possible way (a compiler option etc.) to make the same code work with newer VS versions / C++ runtimes?

3) Since my workaround of editing the header file works up to a point but still having problems, do you know any better way to get around of this problem?

4

3 に答える 3

3

IIRC は、サイズが可変の構造体を作成するための古いトリックです。

検討

struct {
  int len;
  char name[1];
} s;

適切な割り当てが行われ、メモリ内に順番に配置される場合、「名前」は可変長になる可能性があります。

char* foo = "abc";
int len = strlen(foo);

struct s* p = malloc( sizeof(int) + len + 1 );

p->len = len;
strcpy(p->name, foo );

上記は Visual Studio の新しいバージョンでも問題なく動作するはずだと思います。パッキングの問題かもしれません。バイト境界で構造体を取得するために #pragma pack(1) を実行しましたか? VS6にはデフォルトでそれがあったことを私は知っています。

于 2012-05-11T13:37:05.797 に答える