Thread: double pointer problem

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    12

    double pointer problem

    Hi, everyone.

    I tried to handle my character pointer in the function but it's not easy..

    At first, I allocate 3 lenths of character string and set as "EOF" out of function "strToHexa".
    And then tried to make expand space to put 6 lengths of other strings with same character pointer by freeing and allocating memory again in the function. So I used double pointer as a parameter of function.



    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <mem.h>
    
    void strToHexa(char **string)
    //convert characters to hexadecimal
    {
        char toHexa[3];
        int i;
        int sLen=strlen(*string);//set the length of the original string into "sLen"
        printf("in strToHexa[before] : %p\n", *string);//result: "0090B928"
        if(sLen<=3){//if the length of original string is less than 3 (3 lengths is my limitation.
            strcpy(toHexa, *string);//copy original strings to arrays
            free(*string);//free original 
            *string=(char *)malloc(sizeof(char)*sLen*2);//allocate memory with 2 times of the original string because one character needs two characters to express hexadecimal
            for(i=0;i<sLen;i++){
                sprintf((*string)+(i*2), "%X", toHexa[i]); 
            }
        }
        printf("in strToHexa[after] : %p\n", *string); //result: "0090B928"
    }
    
    
    int main(void)
    {
    	char *test=(char *)malloc(sizeof(char)*3);
    	strcpy(test, "EOF");
    	printf("[before] %p\n", test); //result: "0090B928"
    	printf("test: %s\n", test); // result: "EOF"
    
    	strToHexa(&test);
    
    	printf("[after] %p\n", test); //result: "FFFFFFFF"
    	printf("test: %s\n", test); // segmatation fault! I wanned to see "454F46" which is hexadecimal form of "EOF"
        return 0;
    }


    Here is my another trying. It's simple and goes in right way.

    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <mem.h>
    
    void setStr(char **string)
    {
    	printf("in[before] %p\n", *string);
    	free(*string);
    	*string=(char*)malloc(sizeof(char)*4);
    	strcpy(*string, "ABCD");
    	printf("in[after] %p\n", *string);
    }
    
    int main(void)
    {
    	char *test=(char *)malloc(sizeof(char)*2);
    	strcpy(test, "EO");
    	printf("[before] %p\n", test);
    	printf("test: %s\n", test);
    	setStr(&test);
    	printf("[after] %p\n", test);
    	printf("test: %s\n", test);
    
        return 0;
    }
    The result is as follow.

    [before] 009029B8
    test: EO
    in[before] 009029B8
    in[after] 009029B8
    [after] 009029B8
    test: ABCD

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I note that "EOF" consists of 4 characters: 'E', 'O', 'F' and '\0'. As such, it cannot fit into an array of 3 characters while remaining a string which can be used with string functions like strlen().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char *test=(char *)malloc(sizeof(char)*3);
    > strcpy(test, "EOF");
    It's already broken here, "EOF\0" is 4 characters, not 3.

    The \0 trashes someone else's memory.

    Sooner or later (anywhere between microseconds and years), your program will die with a segfault.

    Your 'working' program has the same mistake, but that just makes you lucky rather than good.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    12
    Thank you so much, laserlight..!!
    It's really helpful for me...

    But still qurious why the seconde code works well.. even it has no '\0' character, either..

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    12
    Quote Originally Posted by Salem View Post
    > char *test=(char *)malloc(sizeof(char)*3);
    > strcpy(test, "EOF");
    It's already broken here, "EOF\0" is 4 characters, not 3.

    The \0 trashes someone else's memory.

    Sooner or later (anywhere between microseconds and years), your program will die with a segfault.

    Your 'working' program has the same mistake, but that just makes you lucky rather than good.
    OK, I can see clearly now with Salem's replayment...
    I need to make my bad habit correct...

    Thanks..!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 12:42 PM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM