Solved

Why is malloc overwriting the contents of my variable?

Posted on 2007-03-26
2
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

752 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