Need help to understanding the code

This is a discussion on Need help to understanding the code within the C Programming forums, part of the General Programming Boards category; Hi Guys, I am working on this code, and it is working fine. However, I would like to how it ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    13

    Need help to understanding the code

    Hi Guys,

    I am working on this code, and it is working fine. However, I would like to how it actually works.

    Here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<String.h>
    
    /*
     * 
     */
    
    void swap(void *,void*,int);
    
    int main(int argc, char** argv) {
    
       
        char *husband = "Fred";
        char *wife = "Wilma";
    
        swap(&husband, &wife,sizeof(char *));
        
    
        printf("The husband is: %s\n", husband);
        printf("The wife is: %s\n", wife);
    
        return (EXIT_SUCCESS);
    }
    void swap(void *vp1, void *vp2, int size){
    
        char buf[size];
        
        //copy the content of vp1 to buff
        memcpy(buf,vp1,size);
        memcpy(vp1,vp2,size);
        memcpy(vp2,buf,size);
    
    }
    I understand that - The buf should have a pattern of whatever in vp1. But I can not confirmed it. I am assuming that - the buf should have a copy of the address of whatever in vp1. But how can I print the content of the buf out ?

    I have tried:

    printf("buf has: %p", buf) and I am getting: 0x22cce0
    printf("vp1 has: %p",vp1) and I am getting: 0x22cd34

    I thought: the buf and the vp1 should have the same bit pattern in them.

    I have tried:

    printf("buf has: %p", *buf); and I am getting: 0x20

    Would some one help ?

    Thanks,

    z

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're dealing with strings, print them out using %s in your printf format.

  3. #3
    Banned
    Join Date
    May 2009
    Posts
    37
    "yeah... yeah.. YEAHHH!!! i mean i'm just an IT that doesn't really know any of the real science and technical details and mostly only work on plugging this to this and i work in well with linux. so i should know that this guy's crackpot, right!! yeah!!"

  4. #4
    Banned
    Join Date
    May 2009
    Posts
    37
    "i mean it's not like he's writing code for matrix arithmetic, right??? yeah!! coz like, it's the hardest arithmetic code i can think of" <-(by that last sentence i really am speaking from their point of view... it is the hardest calculation that they know to do... i wonder how they'll fare in even reading my code)

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop posting here Renzokuken. You don't provide anything useful to any thread you are ever in. Go away.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by renzokuken01 View Post
    "i mean it's not like he's writing code for matrix arithmetic, right??? yeah!! coz like, it's the hardest arithmetic code i can think of" <-(by that last sentence i really am speaking from their point of view... it is the hardest calculation that they know to do... i wonder how they'll fare in even reading my code)
    Hey man... have you even seen the travel folders for reality?

    Really this is totally unhelpful behavior and it's not going to make you any friends.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by zbonzbon View Post
    Hi Guys,

    I am working on this code, and it is working fine. However, I would like to how it actually works.

    Here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<String.h>
    
    void swap(void *,void*,int);
    
    int main(int argc, char** argv) {
    
       
        char *husband = "Fred";
        char *wife = "Wilma";
    
        swap(&husband, &wife,sizeof(char *));
        
    
        printf("The husband is: %s\n", husband);
        printf("The wife is: %s\n", wife);
    
        return (EXIT_SUCCESS);
    }
    
    
    void swap(void *vp1, void *vp2, int size){
    
        char buf[size];
        
        //copy the content of vp1 to buff
        memcpy(buf,vp1,size);
        memcpy(vp1,vp2,size);
        memcpy(vp2,buf,size);
    
    }
    Right off you are dealing with buffer overflows... Fred and Wilma are different lengths. When you used them to intialize the string pointers husband and wife you got 5 and 6 bytes respectively... when you try to reverse them you can put Fred where Wilma came from, because it's shorter. You are not going to put Wilma where Fred came from without causing a buffer overflow because it's longer.

    Secondly when you are sending in the size of the buffers to memcpy, you are making the mistake of telling it to copy only 4 characters... sizeof (char*)... instead of the size of the actual strings... for which you need strlen(). At most it's going to swap the first 4 characters of of each string. (a char* pointer is 4 bytes on most systems)

    I understand that - The buf should have a pattern of whatever in vp1. But I can not confirmed it. I am assuming that - the buf should have a copy of the address of whatever in vp1. But how can I print the content of the buf out ?

    I have tried:

    printf("buf has: %p", buf) and I am getting: 0x22cce0
    printf("vp1 has: %p",vp1) and I am getting: 0x22cd34
    You need to print the CONTENT not the pointer.

    Code:
    printf("buf has: %s",buf);
    printf("vp1 has: %s",vp1);

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by CommonTater View Post
    Right off you are dealing with buffer overflows... Fred and Wilma are different lengths. When you used them to intialize the string pointers husband and wife you got 5 and 6 bytes respectively... when you try to reverse them you can put Fred where Wilma came from, because it's shorter. You are not going to put Wilma where Fred came from without causing a buffer overflow because it's longer.

    Secondly when you are sending in the size of the buffers to memcpy, you are making the mistake of telling it to copy only 4 characters... sizeof (char*)... instead of the size of the actual strings... for which you need strlen(). At most it's going to swap the first 4 characters of of each string. (a char* pointer is 4 bytes on most systems)



    You need to print the CONTENT not the pointer.

    Code:
    printf("buf has: %s",buf);
    printf("vp1 has: %s",vp1);
    I think the point here is to swap the addresses that the pointers contain, hence the sizeof(char *), and the & in the swap call.

    So if wife points to "wilma" and husband points to "fred"

    after the function call:

    wife points to "fred" and husband points to "wilma"
    Last edited by Subsonics; 02-13-2011 at 07:26 PM.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Subsonics View Post
    I think the point here is to swap the addresses that the pointers contain, hence the sizeof(char *), and the & in the swap call.

    So if wife points to "wilma" and husband points to "fred"

    after the action:

    wife points to "fred" and husband points to "wilma"
    He's using memcpy()... so I went from there.

    Yes, in this case swapping pointers makes more sense.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by CommonTater View Post
    He's using memcpy()... so I went from there.

    Yes, in this case swapping pointers makes more sense.
    Yes, that is probably since the swap function uses void pointers.

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    Subsonics are correct. What the program doing is that - It is actually swapping the address that contain in vp1 and vp2. And it uses the char buf[size] as a storage for copying the address of the vp1 into it first and then copy the vp2 to vp1 and then finally, it copied the buf back to vp2. Effectively, it is swapping the pointer that pointed to the string Wilma and Fred.

    That logic is very cooled. However, I would like to confirm it by:

    Try to print out the content of the buf to see if it is actually containing the address of Fred. But I could not.

    Regards,

    z

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 04-10-2010, 11:53 PM
  2. Understanding something in Quake 2 Source Code
    By bengreenwood in forum C Programming
    Replies: 5
    Last Post: 08-05-2009, 02:22 PM
  3. Understanding a Line of Quake 2 Source Code
    By bengreenwood in forum C++ Programming
    Replies: 6
    Last Post: 08-04-2009, 03:15 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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