Thread: Pointer references corrupted via DLL (Cygwin)

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    7

    Pointer references corrupted via DLL (Cygwin)

    All, Here is a working sample code of my problem area with my DLL application. I am so close to finish, except that I am required to provide a string buffer back to the calling function.

    However, the DLL seems to obtain a memcpy of this string and not the pointer address. Please let me know if I should be doing anything different in passing this pointer.

    Thanks,

    Sriram
    Code:
    Source code for DLL
    -----
    $ cat -n mock_DLL.c
         1  /* Mock DLL to modify a char array */
         2  #include <stdio.h>
         3
         4  int
         5  DLL_API( char *var1)
         6  {
         7
         8    printf ("Inside DLL: Address of var1 %u",&var1);
         9
        10    return 0;
        11  }
        12$ gcc -o mock_test mock_test.c -L./ -lmock_dll
    
    $ gcc -shared -o mock_dll.dll mock_dll.o
     
    Source code for test file
      -----
     $ cat -n mock_test.c
         1  #include <stdio.h>
         2  static char *var1 = "Foobar";
         3  int main()
         4  {
         5     printf ("Address of var1 : %u\n", &var1);
         6     return DLL_API (var1);
         7  }
    
    $ gcc -o mock_test mock_test.c -L./ -lmock_dll
    
    $ ./mock_test
    Address of var1 : 4206592
    Inside DLL: Address of var1 2280640
    $
    Last edited by rockpaandi; 03-16-2009 at 05:21 PM. Reason: Minor changes to code

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by rockpaandi View Post
    Code:
    printf ("Inside DLL: Address of var1 %u",&var1);
    printf ("Address of var1 : %u\n", &var1);
    The problems are in red.

    EDIT: Also, you should be using %p to print a pointer value, not %u
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    7
    Quote Originally Posted by brewbuck View Post
    The problems are in red.

    EDIT: Also, you should be using %p to print a pointer value, not %u
    Thanks Brew. However, Even with these changes below (in red): I am still getting different addresses!
    Code:
    Output
    ---------
    $ ./mock_test
    Address of var1 : 0x402000
    Inside DLL: Address of var1 0x22ccc0
    
    changes
    ----------
    
    printf ("Address of var1 : %p\n", &var1);
    
    printf ("Inside DLL: Address of var1 %p\n",&var1);

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    7

    Issue resolved.

    Quote Originally Posted by rockpaandi View Post
    Thanks Brew. However, Even with these changes below (in red): I am still getting different addresses!
    Code:
    Output
    ---------
    $ ./mock_test
    Address of var1 : 0x402000
    Inside DLL: Address of var1 0x22ccc0
    
    changes
    ----------
    
    printf ("Address of var1 : %p\n", &var1);
    
    printf ("Inside DLL: Address of var1 %p\n",&var1);
    Oh! Never mind.. I missed removing the '&' . I had imagined you wanted me to add these.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by rockpaandi View Post
    Oh! Never mind.. I missed removing the '&' . I had imagined you wanted me to add these.
    What you were doing is printing the address of the pointer, not the pointer itself. When you pass the pointer as a parameter, clearly the address of where this pointer exists is going to be different. The pointer itself (the address it points to) should be the same.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    7

    Changing value at pointer

    Quote Originally Posted by brewbuck View Post
    What you were doing is printing the address of the pointer, not the pointer itself. When you pass the pointer as a parameter, clearly the address of where this pointer exists is going to be different. The pointer itself (the address it points to) should be the same.
    Do free or malloc operations on this address affect the original pointer. For instance:

    Code:
    $ cat -n mock_DLL.c
         1  /* Mock DLL to modify a char array */
         2  #include <stdio.h>
         3
         4  const char *text="Hi! there";
         5
         6  int
         7  DLL_API( char *var1)
         8  {
         9    printf ("Inside DLL: Address of var1 %p\n",var1);
        10
        11    var1 = text;
        12    return 0;
        13  }
    
    $ cat -n mock_test.c
         1  #include <stdio.h>
         2  static char *var1 = "Foobar";
         3  int main()
         4  {
         5     int err = 0;
         6
         7     printf ("Address of var1 : %p\n", var1);
         8     err = DLL_API (var1);
         9
        10     printf ("Altered value of var1 : %s\n", var1);
        11
        12     return err;
        13  }
    If not, how can I change the value referenced by the pointer in my DLL.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I don't see any malloc() or free() calls in that code.

    At any rate, to alter the value the pointer points to, just alter it. In this specific example, the pointer points to a string literal which cannot legally be altered.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  5. Newbie Q - Pointer vs None pointer references.
    By Guardian in forum Windows Programming
    Replies: 6
    Last Post: 05-02-2002, 04:52 PM