Variable Length String Array with Dynamic Memory Allocation

Hello -

I've mananged to solve most of my issues through internet searches but have hit a wall. I am writing some software to interact with a camera setup we are using. The header that I am trying to write has a relative time stamp and image count which vary in length from when I turn on the camera counting up from '0' up to xxx...xxx. The length of the array will also need to vary based on the number of images that are desired.

I have looked up articles with chars and strings and vectors and ostringstreams (which I have generally left in but commented out). At this point nothing I have tried has worked, even if I try keeping the string at a set length of 120 (which would work on the order of a year before I would recieve a buffer overflow). Sorry for all the comments and mess in the partial code as I don't like to remove all my attempts until I have something working. The full code does work when I define the Header size (e.g Header [120][120]).

I am not a programmer so if you can offer any help it's greatly appreciated but please be as basic as possible in your explinations. Also, the code is not 100% finished but if you have any suggestions on anything else, I am all ears. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
VSIDRIVER_API	int CamSaveImageSetInC(const int numFrames, char fname[])
{
        //Memory Allocation: Image, Temperature, and Header
	short *m_buf = new short[camInfo.numPixels*numFrames] ;
	float temp;
	char Header[120][120];

        // My attempt at using Strings/vectors instead of chars
	//string* Headers = new string[numFrames];
	//vector<string> HeaderVec;
	//char ** NewHeader[120] = new char[numFrames];

        // Creates an initial header and opens up the files
	HRESULT hr = CamStartImageSet(numFrames, fname);
	
	if(hr!=0)
	{
		CamEndImageSet();
		return hr;
	}
        
        // Loops through X images and references the function CamGetImage below
	for (int n = 0; n < numFrames; n++)
	{
		HRESULT hr = CamGetImageSet((m_buf + n*camInfo.numPixels), Header[n], &temp);
		//HRESULT hr = CamGetImageSet((m_buf + n*camInfo.numPixels), Headers + n*strlen(Headers), &temp);

		if (hr!=0)
			n=n-1;
		
		camStatus.numCounts = n+1;
	}
	//Takes image and header sets and writes them to separate files
	if (hr == 0)
	{
		hr = fwrite(m_buf, 2, camInfo.numPixels*numFrames, fp);
		hr = fwrite(Header, 2, numFrames*120, Hd);
	};
	
	if(m_buf)
		delete m_buf;

        // Closes the files
	return CamEndImageSet();;
}

VSIDRIVER_API	int CamGetImageSet(short * m_buf, char Header[], float *tempK)
{
	if(!InitSharedMem())
		return IDS_INI_MEM_ERROR;

	if(!m_pVirtualCam)
		return FALSE;

	HRESULT	hr;

	BOOL bReturn = FALSE;
	BSTR bstrNextImageBuffer = CComBSTR (m_sSharedMem[IMG_SHARED_MEM].strSharedName).Copy();
	BSTR bstrNextInfoBuffer = CComBSTR (m_sSharedMem[INF_SHARED_MEM].strSharedName).Copy();
	//example	:	GetImage(Width, Heigth, Number of subsampling, image shared memory name, image offset, info shared memory name, info offset, image time out, function return)
	hr	=	m_pVirtualCam->GetImage(m_dwPoint, m_dwLine, MODULO, bstrNextImageBuffer, 0, bstrNextInfoBuffer, 0, IMG_TIME_OUT, (ULONG*)&bReturn);
	
	::SysFreeString(bstrNextImageBuffer);
	::SysFreeString(bstrNextInfoBuffer);

	if(FAILED(hr))
		return IDS_SERVER_NOT_RESPONDING;
	if(!bReturn)
		return IDS_NO_IMAGE;

	memcpy(m_buf, m_sSharedMem[IMG_SHARED_MEM].pMemBuffer, (m_dwPoint * m_dwLine) * sizeof(WORD));

        // The camera stores all the information in a structure which I then 
        // pull any of the information out I need	
	InfoImageStruct* pInfoImage	= ((InfoImageStruct*)m_sSharedMem[INF_SHARED_MEM].pMemBuffer);
	if(!pInfoImage)
		return FALSE;

	camInfo.DetectorTemp=pInfoImage->fDetectorTemp;

	camInfo.IntegrationTime=pInfoImage->fIntegrationTime;

	camInfo.FrameRate= pInfoImage->fFrameRate;

        //I have tried using memcpy as well to get the information
	//memcpy(Headerss, m_sSharedMem[INF_SHARED_MEM].pMemBuffer, 1000);
	
        // My attempt with using strings and ostringsteam
	//ostringstream oss;
	//oss << "Preset:" << pInfoImage->wMultiItNumber <<
	//	"ITime:" << pInfoImage->fIntegrationTime <<
	//	"ImageTime:" << pInfoImage->qwImageTime;
	//Headerss = oss.str();

        // Here is how I currently am able to write to Header
	sprintf_s(Header, 120, "Preset:%d; ITime:%7.3f; ImageTime:%I64u; Count:%I64u; T:%.1f; T1:%.1f; T2:%.1f; T3:%.1f; T4:%.1f\n",  
	  pInfoImage->wMultiItNumber, //preset
	  pInfoImage->fIntegrationTime,
	  pInfoImage->qwImageTime,
	  pInfoImage->qwImageCounter,
	  pInfoImage->fDetectorTemp,
	  pInfoImage->fSensorTemp[0],
	  pInfoImage->fSensorTemp[1],
	  pInfoImage->fSensorTemp[2],
	  pInfoImage->fSensorTemp[3]);

	*tempK = camInfo.DetectorTemp;
	
	BOOL pbReturn = FALSE;

	return 0;
}
Ok, so what exactly do you try to achieve and where exactly is your problem with that?
My problem is with the Header (line 6).

I want it first and foremost to be an array that is numFrames long, e.g. 1 header per image. Essentially the same thing I have for the image variable m_buf (line 4).

The length of the header is a function of time and I would like the header to either be resized for what I print to it or at least not output the junk in the excess memory I would be using.
Topic archived. No new replies allowed.