TristinColby
asked on
Why is malloc overwriting the contents of my variable?
Can anyone see why my *jobID is being overwritten???
char *jobID = "123456789";
DWORD size = GetCurrentDirectory(0,NULL ); /* size == size needed for array - 1 */
char *szPwd = (char *)malloc((sizeof(char)size )+1);
memset(szPwd,'\0',sizeof(c har)size+1 );
if(!szPwd) {
puts("Could not allocate buffer for pwd");
}
if(!GetCurrentDirectory(si ze+1,szPwd )) {
printf("Could not get size: Error:%d\n",GetLastError() );
return(EXIT_FAILURE);
}
char *szDir = (char *)malloc(sizeof(char)size+ 10); /* PROBLEM HERE */
/* The statement above is what overwrites the contents of *jobID with what looks like part of a path */
if(!szDir) {
puts("Could not allocate Memory for directory name");
}
memset(szDir,'\0',sizeof(c har)(size+ 10));
printf("JobID is %s\n",jobID);
sprintf(szDir,"%s\\%s",szP wd,jobID);
printf("%s",szDir);
char *jobID = "123456789";
DWORD size = GetCurrentDirectory(0,NULL
char *szPwd = (char *)malloc((sizeof(char)size
memset(szPwd,'\0',sizeof(c
if(!szPwd) {
puts("Could not allocate buffer for pwd");
}
if(!GetCurrentDirectory(si
printf("Could not get size: Error:%d\n",GetLastError()
return(EXIT_FAILURE);
}
char *szDir = (char *)malloc(sizeof(char)size+
/* The statement above is what overwrites the contents of *jobID with what looks like part of a path */
if(!szDir) {
puts("Could not allocate Memory for directory name");
}
memset(szDir,'\0',sizeof(c
printf("JobID is %s\n",jobID);
sprintf(szDir,"%s\\%s",szP
printf("%s",szDir);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
make that :
char *szDir = (char *)malloc(size+10); /* PROBLEM HERE */