Thread: char pointer length problem

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    6

    char pointer length problem

    Hi All,

    I am trying to get the length of an char pointer but the app is getting crashed by giving error - Access Violation. I am not getting why this is happening. The piece of code is given below -

    CODE

    Code:
    int length = 0;
    char** strArr = (char**)malloc(sizeof (char * )*(4));
    temp=(char*)malloc(sizeof(char*)*100);
    
    tempChar="HELLO";
    strArr[0] = (char*)malloc(sizeof(char*)*(strlen(tempChar)+1)); 
    strcpy(strArr[0],tempChar);
    
    tempChar="HELLO1";
    strArr[1] = (char*)malloc(sizeof(char*)*(strlen(tempChar)+1)); 
    strcpy(strArr[1],tempChar);
    
    tempChar="HELLO2";
    strArr[2] = (char*)malloc(sizeof(char*)*(strlen(tempChar)+1)); 
    strcpy(strArr[2],tempChar);
    
    tempChar="HELLO3";
    strArr[3] = (char*)malloc(sizeof(char*)*(strlen(tempChar)+1)); 
    strcpy(strArr[3],tempChar);
    
    length = get2DArrayLength(strArr);
    
    int get2DArrayLength(char** charArray)
    {
    int len=0;
    char **ptr;
    for(ptr=charArray;ptr!='\0';*ptr++)
    {
    print("0..%s",*ptr);
    if(*ptr==NULL)
    break;
    if(strlen(*ptr)!=0)
    {
    len+=strlen(*ptr);
    }
    print("1..%s",*ptr);
    }
    return len;
    }
    OUTPUT ::

    0..HELLO
    1..HELLO
    0..HELLO1
    1..HELLO1
    0..HELLO2
    1..HELLO2
    0..HELLO3
    1..HELLO3
    segmentation fault

    Please check the code and tell me what I am doing wrong in this..

    thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. Read the FAQ on casting malloc. In C, it isn't necessary.
    2. Learn the value of indented code. I don't know whether you see indented code in your IDE, but when you posted it, it was lost. This is a real turn off.

    If get2DArrayLength() is somehow trying to determine that there are 4 strings stored, then it can't.

    In the same way that a char array stores a \0 to mark the end of the array (for say strlen to work), you need to store a NULL pointer at the end of your char* array to mark the end of that array.

    So you would malloc 5 pointers to begin with, and set
    strArr[4] = NULL;
    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.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    Quote Originally Posted by Salem View Post
    1. Read the FAQ on casting malloc. In C, it isn't necessary.
    2. Learn the value of indented code. I don't know whether you see indented code in your IDE, but when you posted it, it was lost. This is a real turn off.

    If get2DArrayLength() is somehow trying to determine that there are 4 strings stored, then it can't.

    In the same way that a char array stores a \0 to mark the end of the array (for say strlen to work), you need to store a NULL pointer at the end of your char* array to mark the end of that array.

    So you would malloc 5 pointers to begin with, and set
    strArr[4] = NULL;
    Hi Salem,

    Thanx for the reply,

    Actually I am doing all this in BREW,
    I have to calculate the length dynamically as I am allocating the pointer runtime and deallocating also runtime as if I will use char** then I have to use one integer to keep track of size of the first diamention. so for each char** I have to use different integers. I don't want to do use such manual solution.

    I had also used this before -

    So you would malloc 5 pointers to begin with, and set
    strArr[4] = NULL;


    but still the problem was there.


    So please tell me is there any other way out for this

    Thanx

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well your function also needs to be fixed.

    int len = 0;
    while ( p[len] != NULL ) len++;
    return len;

    Is all you need, if p is your char** parameter in the function.
    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.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    Quote Originally Posted by Salem View Post
    Well your function also needs to be fixed.

    int len = 0;
    while ( p[len] != NULL ) len++;
    return len;

    Is all you need, if p is your char** parameter in the function.
    Hi,

    Just have a look at this -

    Code:
    #include <iostream>
    #include <stdio.h>
    
    using namespace std;
    
    void main()
    {
        char ** xyz;
        int len=0,bh=0,i=0;
        cin>>len>>bh;
        xyz = (char **)malloc(sizeof(char *)*len);
    	memset(xyz,NULL,(sizeof(char *)*len));
        for(i=0;i<len;i++)
        {
            xyz[i] = (char*)malloc(sizeof(char)*bh);
    //		memset(xyz[i],NULL,(sizeof(char)*bh));
        }
        for(i=0; i<len;i++)
        {
            cin>>xyz[i];
            cout<<strlen(xyz[i])<<endl;
        }
        char **ptr = xyz;
        cout<<"now data count is "<<endl;
        len = 0;
    	while(ptr[len]!=NULL) len++;
    
       /* for(ptr = xyz; ptr != '\0' ;*ptr++)
        {
            if(*ptr==NULL)
               break;
            if(strlen(*ptr) != 0)
            {
                    len += strlen(*ptr);
                    cout<<strlen(*ptr)<<endl;
            }
        }*/
        cout<<"Total length is "<<len<<endl;
    }
    it's working but it's not giving exact length of char**

    Thanx

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    found solution -

    have a look at this -

    Code:
    #include <iostream>
    #include <stdio.h>
    
    using namespace std;
    
    int get2DArrayLength(char** ptr);
    int main()
    {
        char** xyz;
        int len=0,bh=0,i=0;
        cin>>len>>bh;
        xyz = (char**)malloc(sizeof(char*)*(len+1));
    	memset(xyz,NULL,(sizeof(char*)*(len+1)));
        for(i=0;i<len;i++)
        {
            xyz[i] = (char*)malloc(sizeof(char)*bh);
    		memset(xyz[i],NULL,(sizeof(char)*bh));
        }
        for(i=0; i<len;i++)
        {
            cin>>xyz[i];
            cout<<strlen(xyz[i])<<endl;
        }
    	cout<<"Total length is "<<get2DArrayLength(xyz)<<endl;
    }
    
    int get2DArrayLength(char** ptr){
    	int len = 0;
    	while(ptr[len]!=NULL){
    		cout<<"STRING  :: "<<ptr[len]<<endl;
    		len++;
    	}
    	return len;
    }
    thanx

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should decide what language you are using
    currently - you have a horrible mix of C/C++
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    Quote Originally Posted by vart View Post
    you should decide what language you are using
    currently - you have a horrible mix of C/C++
    Hi Vart,

    sorry to say but does this thing matters here?
    the main goal of this post is getting length of char**
    rest of the thing is only for display purpose.

    So please check for the solution and leave the other part.

    Thanx

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, you're still casting malloc.
    In this case, the problem would appear to stem from your lack of including stdlib.h.

    Compiling C code with a C++ compiler is another source of difficultly.

    > rest of the thing is only for display purpose.
    So what?
    If it's wrong, we're going to point it out to you. Sooner or later, you'll make the same mistakes in your real code. So it's better to get a heads up on all the issues.
    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.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    6

    Smile

    Ok Salem,

    I'll take care of the same in the future!

    Thanx

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    getting length of char**
    You should define this term. Currently - it has no meaning for me.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM