Pointers in function parameter

This is a discussion on Pointers in function parameter within the C Programming forums, part of the General Programming Boards category; Hello, I'm having a problem with my code where it crashes when trying to copy and append the parameter data ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    216

    Pointers in function parameter

    Hello,

    I'm having a problem with my code where it crashes when trying to copy and append the parameter data to the variables used by the program. This problem is unrelated to libCurl which is what I'm using for the uploading procedure. This code is part of a DLL file.

    Code:
    DLLIMPORT void UploadFile(char *filename, char *newfilename, char *urlDir, char *username, char *password);
    {
      char *UPLOAD_FILE_AS;
                                 
      CURL *curl;
      CURLcode res;
      FILE *hd_src;
     
      strcpy(UPLOAD_FILE_AS, filename); // CRASHES HERE
     
      strcat(urlDir, newfilename); // SAME PROBLEM HERE
    
      struct curl_slist *headerlist=NULL;
      char buf_1 [] = "RNFR "; 
      char buf_2 [] = "RNTO "; 
      
      strcat(buf_1, filename); // AND POSSIBLY THESE TWO AS WELL
      strcat(buf_2, newfilename);
    
      /* get a FILE * of the same file */ 
      hd_src = fopen(filename, "rb");
    
      /* In windows, this will init the winsock stuff */ 
      curl_global_init(CURL_GLOBAL_ALL);
    
      /* get a curl handle */ 
      curl = curl_easy_init();
      if(curl) {
        /* build a list of commands to pass to libcurl */ 
        headerlist = curl_slist_append(headerlist, buf_1);
        headerlist = curl_slist_append(headerlist, buf_2);
    
        /* we want to use our own read function */ 
        curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
     
        /* enable uploading */ 
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
     
        /* specify target */ 
        curl_easy_setopt(curl,CURLOPT_URL, urlDir);
     
        /* pass in that last of FTP commands to run after the transfer */ 
        curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
     
        /* now specify which file to upload */ 
        curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
        
        /* specify username and password for ftp */
        curl_easy_setopt(curl, CURLOPT_USERNAME, username);
        curl_easy_setopt(curl, CURLOPT_PASSWORD, password);
     
        /* Now run off and do what you've been told! */ 
        res = curl_easy_perform(curl);
        
        /* clean up the FTP commands list */ 
        curl_slist_free_all (headerlist);
     
        /* always cleanup */ 
        curl_easy_cleanup(curl);
      }
      fclose(hd_src); /* close the local file */ 
      curl_global_cleanup();
    }
    Thanks for the help in advance.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    It's not allocated. It's a dangling pointer pointing to anywhere in memory.

    strcpy does not allocate memory as it copies. You have to do that yourself. Since you don't use UPLOAD_FILE_AS at all during this function, I recommend you remove it and don't bother to copy anything to it.
    Last edited by Cynic; 04-25-2012 at 03:14 PM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    Code:
    strcpy(UPLOAD_FILE_AS, filename); // CRASHES HERE
      
    strcat(urlDir, newfilename); // SAME PROBLEM HERE
     
    struct curl_slist *headerlist=NULL;
    char buf_1 [] = "RNFR ";
    char buf_2 [] = "RNTO ";
    
    strcat(buf_1, filename); // AND POSSIBLY THESE TWO AS WELL
    strcat(buf_2, newfilename);
    You're right about all of these being a problem:

    • UPLOAD_FILE_AS - this is a pointer, but it doesn't point anywhere useful, just to random memory you don't own. Make it a sufficiently large char array.
    • urlDir - Can't say for sure without knowing how you call UploadFile, but I would guess something similar. It's either not pointing to valid memory, or it's pointing to an array that is too small.
    • buf_1 and buf_2 - These are char arrays, but their size is 6 chars (the five you initialize it with plus a null terminator). Strings do not automatically grow in C. Try making it a larger buffer, but only initializing the first few bytes (see below).


    Code:
    char buf_1[BUFSIZ] = "RNFR ";  // declare it to be BUFSIZ (a standard #define in stdio.h), which is probably 512 chars or bigger, but only initialize the first 6 bytes
    strcat(buf_1, filename);  // now there is space to concatenate filename onto the end

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Ok, I'm making these changes as I type.

    Here is the way I call UploadFile.

    Code:
    UploadFile("filename.txt", "newfilename.txt", "ftp://website.com/public_html/upload/", "usernameForServer", "passwordForServer");

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Quote Originally Posted by binks View Post
    Ok, I'm making these changes as I type.

    Here is the way I call UploadFile.

    Code:
    UploadFile("filename.txt", "newfilename.txt", "ftp://website.com/public_html/upload/", "usernameForServer", "passwordForServer");
    Not really helpful. Are you actually passing literals and and these are merely parameter representation only?

    If they are literals, the memory cannot be written to, thus newfilename.txt will generate a fault if you attempt to change it. If you're passing variables, it would be helpful to show that and what specifically the variables are (arrays or pointers to dynamically allocated memory).
    Last edited by Cynic; 04-25-2012 at 03:41 PM.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    libCurl gives the option to rename a file once it's uploaded. "newfilename.txt" is just that.

    I don't understand what you mean by literals? this is an example of how I would call the function in a program (this is a DLL).

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    A literal is a literal. It is a explicitly stated string.

    strcyp( str_ptr, "literal" );


    the word "literal" is a literal. str_ptr is a pointer to a block of allocated memory (though it might actually point to any point in an static or locally defined array).

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    I personal will use literals in the program that uses the function. Won't it work the same as a variable would?

    Are you suggesting I won't be able to append to char * urlDir since it's a parameter variable?

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Quote Originally Posted by binks View Post
    Are you suggesting I won't be able to append to char * urlDir since it's a parameter variable?
    No, I'm saying you won't be able to change it because it's a literal with no associated holding variable.

    Think on this statement:

    strcpy( "literal", str_ptr );

    This would or should generate a compiler error, but it's for demo purposes.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    ohh! but if I use this for example:

    char urlDir[BUFSIZ] = "ftp://website.com/public_html/upload/";
    UploadFile("filename.txt", "newfilename.txt", urlDir, "usernameForServer", "passwordForServer");

    It should work then, shouldn't it?

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Yes, provided you don't overwrite the bounds of the array.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Can a DLL write to a variable local to the executable, or must it be global or something similar?

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    Generally, yes. Of course, it can't magically know the name of a variable in some other code. The functions in the DLL must be able to "see" the variable in the executable. Declaring them in the executable and passing them in to the DLL functions, as you did in post #10, is a good way to achieve this.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    It's working great, thanks. (forgot to link at first )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function parameter/ function argument
    By codecaine_21 in forum C Programming
    Replies: 2
    Last Post: 09-24-2010, 08:09 PM
  2. parameter passing and pointers
    By pokiepo in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 12:13 AM
  3. how to add a parameter to the function
    By jayfriend in forum C Programming
    Replies: 5
    Last Post: 01-16-2007, 08:31 PM
  4. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  5. Parameter in a function
    By cpluspluser in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2003, 07:48 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21