Why is malloc overwriting the contents of my variable?

Posted on 2007-03-26
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);
            if(!szPwd) {
                  puts("Could not allocate buffer for pwd");
            if(!GetCurrentDirectory(size+1,szPwd)) {
                  printf("Could not get size: Error:%d\n",GetLastError());
            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");
            printf("JobID is %s\n",jobID);
Question by:TristinColby
  • 2
LVL 53

Expert Comment

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

make that :

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

Accepted Solution

Infinity08 earned 500 total points
ID: 18796279
Same here :

            char *szPwd = (char *)malloc((sizeof(char)size)+1);

becomes :

            char *szPwd = (char *)malloc(size+1);

and here :


becomes :


Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to declare a 2-dimensional version of strlen 4 220
outside company writing a dll in c/c++, I want to call it from C# 2 74
sameEnds challenge 3 180
What is sub-make ? 2 71
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

828 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