Thread: Problem With Swapping

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    21

    Question Problem With Swapping

    Hello,
    I have written a code to swap two variables of generic types....
    I am able to swap numeric values correctly... but when i try to work with character arrays it is swapping but some characters are missed...
    Here is the code..
    Code:
    /***This is generic swap function implementation, which swaps two variables of any data types
        But it uses dynamically allocated buffer rather than stack buffer..   ***/
    
    #include<stdio.h>
    #include<stdlib.h>
    
    void swap(void *, void *, int);
    int main()
    {
       int a = 10, b = 20;
       char *s1 = strdup("this is new ........!");
       char *s2 = strdup("im gonna sleep..");
       printf("\n Before swap\n");
       printf(" a = %d\n b = %d\n",a,b);
    
       swap(&a, &b, sizeof(int));
    
       printf("\n After swap\n");
       printf(" a = %d\n b = %d\n",a,b);
    
       printf("\n Swapping strings\n");
       printf("Before Swap\n");
       printf(" s1 = %s\n", s1);
       printf(" s2 = %s\n", s2);
    
       swap(s1, s2, sizeof(char *));
       printf(" After Swap\n");
       printf(" s1 = %s\n", s1);
       printf(" s2 = %s\n", s2);
    }
    
    //accepts pointer to two variables and their size.
    void swap(void *vp1, void *vp2, int size)
    {
       //temp variable equivalent..a buffer
       char *buffer = (char *)malloc(size);
        //memcpy copies bits from one location to another, memcpy(source, destination, size)
       memcpy(buffer, vp1, size);
    
       memcpy(vp1, vp2, size);
       memcpy(vp2, buffer, size);
    
       //now we are done so we need to free dynamically allocated memory
       free(buffer);
    }
    And also when is declare strings without strdup.. while swapping it gives seg fault... as im moving addresses not manipulating contents of those address then why it is giving seg fault...??

    Thnx!!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
       swap(s1, s2, sizeof(char *));
    You should try printing that out to see exactly what sizeof(char*) is, and consider what the consequences of that are.

    How about:
    Code:
       int a = strlen(s1), b = strlen(s2);
       swap(s1, s2, (a > b) ? a + 1 : b + 1);
    If the inputs are true pointers (to const string literals, or strings created with malloc or strdup), then all you have to do is swap the pointers.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    21
    Quote Originally Posted by MK27 View Post
    Code:
       swap(s1, s2, sizeof(char *));
    You should try printing that out to see exactly what sizeof(char*) is, and consider what the consequences of that are.

    How about:
    Code:
       int a = strlen(s1), b = strlen(s2);
       swap(s1, s2, (a > b) ? a + 1 : b + 1);
    If the inputs are true pointers (to const string literals, or strings created with malloc or strdup), then all you have to do is swap the pointers.
    I got it.. i missed &.... it should be swap(&s1,&s2, sizeof(char *)) and it's working fine... as memcpy operates on data pointed to by address.. so for char string i must pass char **...
    And that was reason i was getting set fault witout strdup coz i was trying to change contents of constant string which is defined in string table and is read only i guess....

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by greendragons View Post
    I got it.. i missed &.... it should be swap(&s1,&s2, sizeof(char *)) and it's working fine... as memcpy operates on data pointed to by address..
    NO. YOU ARE WRONG.

    Sizeof(char*) is either 4 (32 bit system) or 8 (64 bit system). Of course, if every word you swap is less than that, you are okay. Otherwise this:

    Code:
    char *buffer = (char *)malloc(size);
    is not long enough. However, it might work sometimes anyway if nothing vital gets overwritten by the overrun. In this case such "luck" is unfortunate because you now believe code that appears to work == correctly written safe code, but that is not true.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    21
    Quote Originally Posted by MK27 View Post
    NO. YOU ARE WRONG.

    Sizeof(char*) is either 4 (32 bit system) or 8 (64 bit system). Of course, if every word you swap is less than that, you are okay. Otherwise this:

    Code:
    char *buffer = (char *)malloc(size);
    is not long enough. However, it might work sometimes anyway if nothing vital gets overwritten by the overrun. In this case such "luck" is unfortunate because you now believe code that appears to work == correctly written safe code, but that is not true.
    You are not getting wht i said... i know size of char * is dependent on architecture.... im here swapping addresses pointed by vp1 and vp2 in case of char string... so i need to pass char ** which is done by placing & in front of char arrays... swap(&s1,&s2, sizeof(char *)) so now swap is exchanging addresses stored at s1 and s2...im not swapping actual bits of data... it's just pointers getting swapped... but in case of int it's simply single pointer..... and code works perfectly fine....

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by greendragons View Post
    You are not getting wht i said... i know size of char * is dependent on architecture.... im here swapping addresses pointed by vp1 and vp2
    Okay, my bad -- that will work.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. swapping help
    By mouse666666 in forum C Programming
    Replies: 3
    Last Post: 03-24-2010, 10:38 AM
  2. swapping nibbles
    By rits in forum C Programming
    Replies: 2
    Last Post: 09-07-2009, 08:24 AM
  3. Recursive Swapping
    By AdINo in forum C Programming
    Replies: 1
    Last Post: 06-22-2007, 01:20 AM
  4. swapping
    By metallicaau in forum C Programming
    Replies: 2
    Last Post: 10-08-2002, 08:17 AM
  5. Swapping 2 variables in C
    By yogeshm in forum C Programming
    Replies: 10
    Last Post: 09-26-2002, 01:54 AM

Tags for this Thread