[En-Nut-Discussion] pointer don't work?
Ulrich Prinz
uprinz2 at netscape.net
Fri May 13 00:28:39 CEST 2011
Hi!
You have two options:
char buffer[128];
char *pointer;
pointer = &buffer[0];
or
pointer = buffer;
But
if you read
cnt = fread(...);
and cnt is now 128 then you read 128.
If you now get strlen(buffer) == 22 you have to keep in mind that strlen
gives you the length of the string. And this could mean that you have a
'\0' (0x00) in your buffer at buffer[22].
Could you please check if the results differs if you do this:
printf("buffer %u\n", strlen(buffer));
printf("pointer %u\n", strlen(pointer));
There might be another problem too:
if you do pointer = malloc(128) and you do not check if (pointer !=
NULL) you do not see if your malloc failed. On systems where there is
memory at position 0x0 this might not raise any error. But strlen then
checks for a string length in this memory and not in the memory you
might think.
Even if NULL is declared as -1 (0xffffffff) on small CPUs this must not
fail as they simply wrap around. On ARM and Cortex it will rise an
exception as these CPU detect an illegal access to non existent memory.
Ulrich
More information about the En-Nut-Discussion
mailing list