From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

strcpy malloc problems??

#define DB_NAME ";DB="
#define DSN_CHAR "DSN"
#define DSN_PRELUDE "DSN="
#define PROMPT_USR_PWD ";DLG="

char *ConDB_Name,
*TablesName,
*DB_Path_Name,
*DB_string,
DB_Tables_Name[51];
DB_Source_Name[81];

ConDB_Name = malloc(strlen(DSN_PRELUDE) + strlen (DB_Source_Name) + strlen(DB_NAME) + strlen(dirname) + strlen(PROMPT_USR_PWD) + 20);

strcpy(ConDB_Name, DSN_PRELUDE);
strcat(ConDB_Name, DB_Source_Name);
strcat(ConDB_Name, DB_NAME);
strcat(ConDB_Name, dirname);
strcat(ConDB_Name, PROMPT_USR_PWD);

after the strcpy(ConDB_Name, DSN_PRELUDE); command ConDB_Name should be "DSN=" but I am getting "24" instead... probably garbage.

I've tried this code on my laptop and works fine but
does not on my desktop.

TIA-
0 Kudos
Message 1 of 4
(3,243 Views)
The chunk of code you posted isn't complete. Somewhere else you must declare and initialize dirname, and initialize DB_Source_Name. If you have only declared but not initialized a string, you don't want to use it with strcat().
The code you posted also has a mistake which I assume is a typo (otherwise the compiler would have caught it): the line DB_Tables_Name[51] should end in a comma (not a semi-colon) to continue the char declaration to DB_Source_Name.
If your variables DB_Source_Name and/or dirname change between malloc() and strcat(), you should use sizeof() in malloc(), not strlen(). sizeof(DB_Source_Name) will always return 81. strlen(DB_Source_Name) may return a lower number (including 0) if DB_Source_Name doesn't use up the full 81 bytes.
If code works on y
our laptop but not on your desktop, something else (other than malloc) must be different. Are you running the debug configuration on one and the release configuration on the other?
0 Kudos
Message 2 of 4
(3,243 Views)
Sorry for the typos... you are correct!

I went ahead and changed in malloc(); to sizeof(DB_Source_Name) and sizeof(dirname); but I still get error at strcpy(ConDB_Name, DSN_PRELUDE); Before I step in/over DSN_PRELUDE = "DSN=" and ConDB_Name = a long string 4 repeating strange characters. After the strcpy, ConDB_Name = "24" instead of "DSN="?
I think I can get it to work without using malloc(); and declaring from *ConDB_Name to ConDB_Name[256];

Thanks-
0 Kudos
Message 3 of 4
(3,243 Views)
OK, I think I have lost my mind!! This is what I did:
in seperate .h file:
#define DSN_PRELUDE "DSN="
in .c file:
int CVICALLBACK InsertCallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
static char *cstart = DSN_PRELUDE;
switch (event)

when I check the value of cstart it equals "24"!!
when I go back and change the #define's 'N' character in "DNS=" to any other character, I get cstart = DSN_PRELUDE!!!!!!! I verified on my laptop and this does NOT happen!
0 Kudos
Message 4 of 4
(3,243 Views)