02-12-2019 09:53 AM
I'm in need of some knowledge-based information I'm trying to recall from my programming days how many characters can a pointer hold? Below is where I'm getting an error with an attempt to write beyond end of string on the pointer variable product.
Any suggests would be great thanks!
int GetProductName (char devName[DAQ_DEVICE_NAME_LEN+1], char *product)
{
int error = 0;
char cInfo[4100] = {"\0"};
memset( cInfo, '\0', 4100 * sizeof(char));
if ((error = DAQmxGetDeviceAttribute (devName, DAQmx_Dev_ProductType, cInfo, 4100)) != 0) goto Error;
if (strlen(cInfo) > DAQ_PRODUCT_NAME_LEN) cInfo[DAQ_NAME_LEN-1] = '\0';
Fmt(product, "%s<%s", cInfo);
return error;
Error:
ShowDAQmxError(0, error, "GetProductNames", 0);
return error;
}
02-13-2019 06:15 AM
In your case: no one knows. But if product is not NULL it points to a memory space with at least one char length.
You have to search all calls to this GetProductName() to know the smallest.
Changing it to char product[DAQ_DEVICE_NAME_LEN+1] just gives false sense of safety because this works without issues:
void f( char str[5] ) { DebugPrintf("str = %s\n", str); } int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { if (InitCVIRTE (hInstance, 0, 0) == 0) return -1; /* out of memory */ f("I am longer than 5 chars!"); return 0; }
IMHO better, if feasible:
Add a third parameter like "size_t maxlength" and use StringCopyMax instead of Fmt.
02-13-2019 07:16 AM
Well, I tried your suggestion and I'm still getting the string error. I've looked throughout most of the code to see if it was a global variable. It's not, 100% local to the function nothing else. Yes, it gets used again in another function but it's a local variable again.
02-13-2019 06:47 PM
Hi ,
Did you try to use a shipping example from DAQmx on CVI ?
Did you check this link: https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000019S8tSAE&l=en-US ?
02-13-2019 06:50 PM
Also, this wiki could be helpful: http://wiki.freepascal.org/NI-DAQmx_and_NI-DAQmx_Base_examples it has an example about "get the product info".