Solved

Why is malloc overwriting the contents of my variable?

Posted on 2007-03-26
2
390 Views
Last Modified: 2010-05-18
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(char)size+1);
            if(!szPwd) {
                  puts("Could not allocate buffer for pwd");
            }
            if(!GetCurrentDirectory(size+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(char)(size+10));
            
            printf("JobID is %s\n",jobID);
            sprintf(szDir,"%s\\%s",szPwd,jobID);
            printf("%s",szDir);
0
Comment
Question by:TristinColby
  • 2
2 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 18796271
     char *szDir = (char *)malloc(sizeof(char)size+10);  /* PROBLEM HERE */

make that :

      char *szDir = (char *)malloc(size+10);  /* PROBLEM HERE */
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 18796279
Same here :

            char *szPwd = (char *)malloc((sizeof(char)size)+1);
            memset(szPwd,'\0',sizeof(char)size+1);

becomes :

            char *szPwd = (char *)malloc(size+1);
            memset(szPwd,'\0',size+1);

and here :

        memset(szDir,'\0',sizeof(char)(size+10));

becomes :

        memset(szDir,'\0',(size+10));
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Converting a float to a string function in c languange 10 204
Which checksum is this? 7 139
Using unmanaged DLL from managed application 14 290
Need example 5 107
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now