开发者

How to determine 2D unsigned short pointers array length in c++

开发者 https://www.devze.com 2022-12-28 05:44 出处:网络
I am finding it difficult to determine the length of the columns in a 2D unsigned short pointer array. I have done memory allocation correctly as far as I know. and can print them correctly.

I am finding it difficult to determine the length of the columns in a 2D unsigned short pointer array. I have done memory allocation correctly as far as I know. and can print them correctly.

plz see the following code segment:

int number_of_array_index_required_for_pointer_abc=3;
char A[3][16];


strcpy(A[0],"Hello");
strcpy(A[1],"World");
strcpy(A[2],"Tumanicko");


cout<<number_of_array_index_required_for_pointer_abc*sizeof(unsigned short)<<endl;
unsigned short ** pqr=(unsigned short **)malloc(number_of_array_index_required_for_pointer_abc*sizeof(unsigned short));




    for开发者_开发问答(int i=0;i<number_of_array_index_required_for_pointer_abc;i++)
    {

        int ajira = strlen(A[i])*sizeof(unsigned short);
        cout<<i<<" = "<<ajira<<endl;
        pqr[i]=(unsigned short *)malloc(ajira);
        cout<<"alocated pqr[i]= "<<sizeof pqr<<endl;


        int j=0;
        for(j=0;j<strlen(A[i]);j++)
        {
            pqr[i][j]=(unsigned short)A[i][j];

        }
        pqr[i][j]='\0';


    }


    for(int i=0;i<number_of_array_index_required_for_pointer_abc;i++)
    {

        //ln= (sizeof pqr[i])/(sizeof pqr[0]);

        //cout<<"Size of pqr["<<i<<"]= "<<ln<<endl;


         // I want to know the size of the columns i.e. pqr[i]'s length instead of finding '\0'

         for(int k=0;(char)pqr[i][k]!='\0';k++)
        cout<<(char)pqr[i][k];
        cout<<endl;

    }


You're almost there. You have this loop:

for(int k=0;(char)pqr[i][k]!='\0';k++) ...

Once this loop is done, k will have the length of the row. So this will give you the length of pqr[i] (not including the null terminator):

int k;
for (k=0; pqr[i][k] != 0; k++)
    ;
cout<<"The length is "<< k <<endl;

Edit:

You now added that you want to know the size even if the null terminator is not there. There is no way to do that. You will need to either have some kind of terminator, or store the size somewhere. If you use vector<unsigned short>, it will store the size for you. Since it also handles allocation and deallocation, it's the recommended choice.

</Edit>


Note that you have two errors in your allocation:

  1. pqr is an array of pointers, but you're allocating a size of C*sizeof(unsigned short). that should be C*sizeof(unsigned short *) instead.

  2. You're not allocating memory for the null terminator at the end of each string: You should be allocating (strlen(A[i])+1) * sizeof(unsigned short) for each string.


You have a bug at this line:

pqr[i][j]='\0';

At this point j is equal to strlen(A[i]) - which is outside the bounds you setup for pqr:

int ajira = strlen(A[i])*sizeof(unsigned short);
pqr[i]=(unsigned short *)malloc(ajira);

pqr[i] goes from [0] to [strlen(A[i])-1] so writing to pqr[i][strlen(A[i])] overflows the array. The compiler won't pick up on this as you allocated the memory yourself.

The solution to that bug is to do malloc(ajira+sizeof(unsigned short))

Edited after comments

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号