strlen and sizeof

Pages: 12
Hello,

why is this:

1
2
3
4
	unsigned char *temp = NULL;
	temp = new unsigned char[17];

	std::cout<< "size " << strlen((char*)temp);


giving a size 32 and not size 16?



and

sizeof(temp)

gives 4? shouldnt it be 17*sizeof(unsigned char)?

Thank you a lot for your help.
Last edited on
All strlen() does is this:
1
2
3
4
5
6
int strlen(char *str){
    int len=0;
    while (str[len]!=0)
        len++;
    return len;
}

In other words, it counts how many characters are there from the character pointed to to the closest '\0'. It doesn't measure the actual size of the array, because that's impossible.

sizeof(temp) is giving the size of a pointer, which is, in this case, 4 bytes. sizeof calculates sizes at compile time, and the array temp points to was created at run time, so it's impossible for the compiler to know how long it is.
och, i understand now, thasnk you very much.
and if i need to put into the temp something like this:

1234505555

how can I do that? it will return only 12345.
I said '\0', not '0'.
OP : \0 is escape sequence for null. 0 is the number zero.
Don't forget your single quotes. '\0'==0, so your statement above is ambiguous.
yes, but what i am trying to do, is:

temp is unsigned char, i need to put there some leters, and 2 numbers.

something like this:

2abdcd0efgh

if i do this:

temp[0] = 2;

and than
if(temp[0] != 2) return false;

and its true. its probably becasue the char of number 2 is 2.

if i do that for temp[xy] with 0, its treating like '\0'. so, i dont know how to put it then.

NO that doesn't work. You need single quotes around anything for it to be interpreted as a character. So this would always be false:
1
2
char x = '2';
x == 2; // FALSE 

You need this:
x == '2' // TRUE
And it should not be treating it like 0. Are you comparing to '0' or to 0?
i am comparing to 2 not to '2'. so i need it to be treated as a number, not like a character.

Last edited on
So ['2'==2] would always be false
Not guaranteed, but yes '2' is never 2.

See what this does, ripley:
1
2
3
4
5
6
7
8
9
10
char s[]="Hello, World!\n";
std::cout <<s;
s[0]='2';
std::cout <<s;
s[0]=2;
std::cout <<s;
s[0]=0;
std::cout <<s;
s[0]='\0';
std::cout <<s;


EDIT: In that case, if you need to represent the actual value 0, you can't use a simple char array. You'll also have to pass the size of the array around, otherwise your code will not be able to tell how long the array is.
Last edited on
what i am trying to do, is some pkcs padding.

it works like 02|padding string|0|message;

and the problem is, that the pointer pointes only to 2|padding string not to the whole message,

but the numbers in there are treated as numbers not like charakters.
When you have a string, it is an array of chars. You would need to read them as characters and convert them to numbers if you want to handle them that way.
Last edited on
When you have a c-style string. If you have a std::string then it's a different story.
So you want to parse the character string "02" to the value 2, is that correct?

(Maybe you should have mentioned this from the beginning, instead of letting us think that you didn't understand the difference between '2' and 2.)
tummychow wrote:
When you have a c-style string. If you have a std::string then it's a different story.

He is using strlen(), so...
I know.
@OP: The cctype library has several functions for manipulating chars and turning them into numbers, and the like. cstdlib may also have some things to look at. Check the reference if that's what you are trying to do.
Last edited on
i am sorry, i should.

no, i need to parse char string do the value 0 .

so, its, like this;

i have some message, i i want there put some padding. so i put

02|some string, which doesnt contain 0|0|message

than i encrypt it. when i encrypt it, then i am comparing

if first byte is 0, then its ok
if second string is 2 then its ok
if at least 8 bytes are non-zero it ok

now i now, the messege has pkcs padding

and than after first zero is the messege.
and isnt that posible to do it , like Helios sad? that i return a size of string as well? but i don know, how to do that. i try to look in cstdlib
Edit your posts instead of double posting. It's just cleaner.
And I also directed you to cctype. One of those two has the atoi function to take an ascii char and convert to integer. And please capitalize. We start sentences with capital letters in English.
Pages: 12