Hi little question

hi im new to the forums and i have been programing in java for some time now and i just started learning C/C++

any ways i was trying to make this program and i wanted to count the strings in my char *strings[] array (new to pointers) so i wrote this bit of code but i keep getting different values
1
2
3
//(this parts in main())
char *stringz[] = {"bill", "chris", "jason", "randy", "trish"};
int size = sizeCheck(stringz);

1
2
3
4
5
6
7
8
9
10
int sizeCheck(char *str[])
{
    int size =0;
    for(int i = 0; str[i] != '\0'; i++)
    {
            size++;
    }
    cout << size;
    return size;
}


so i tried compiling in Vi and i get size to be 10 and in bloodshed i get size 20 but when i put this stuff in another program (one randomly selected as a test pig) i got the correct size yet when i try the code in its own cpp file i get the same error that i get in the original file


Last edited on
stringz is a pointer to an array of pointers which you're not ending with a zero (e.g. char *stringz[] = {"bill", "chris", "jason", "randy", "trish", 0};). Your loop in sizeCheck() is therefore depending on unknown memory contents at run time, because there's no way to know what's in memory after stringz[4].

There's no way to know the size of a stack array that was created in a different function without adding more data, such as an extra size parameter, or having the last element being a special value, and the only way to know the size of the array in the function that did create it is sizeof(array)/sizeof(*array).
The size of a global array can be known from anywhere, I think.
Last edited on
but arnt all char arrays ended with a null char in c++? so in essence im just looking where the end of the array is
char arrays are, but stringz is not a char array. It's a pointer to an array of pointers to char arrays. It's a bit complicated to understand at first, I know. Let's see if I can explain it clear enough.
stringz looks like this:
stringz -> memory_location_0
memory_location_0:{
stringz[0] -> memory_location_1,
stringz[1] -> memory_location_2,
stringz[2] -> memory_location_3,
stringz[3] -> memory_location_4,
stringz[4] -> memory_location_5,
stringz[5] -> ???,
stringz[6] -> ???,
stringz[7] -> ???,
(and so on...)
}
memory_location_1:{
'b',
'i',
'l',
'l',
0,
???,
???,
???,
(and so on...)
}
memory_location_2:{
'c',
'h',
'r',
'i',
's',
0,
???,
???,
???,
(and so on...)
}
(And so on)

("->" means "points to". Do not confuse with the C/++ operarator with the same shape and different meaning. ":" means "contains". The memory_locations are just placeholders. They don't need to be in any particular order nor do they need to be contiguous.)
Note that even though the arrays have been defined of a given size, nothing's stopping you from accessing elements past beyond the limits. This is not correct, of course, since it's impossible to predict what those locations contain. That's known as a buffer overflow. Sometimes the system will detect buffer overflows and crash the program, but if it doesn't that still doesn't make the code correct.
What you're doing in the code you posted is just that: a buffer overflow. The program just happens to find a zero somewhere, but it could have happened that it didn't and it just kept going and going for ever more. In some older systems, this was actually possible if not for the very unlikely event of never finding two or four consecutive zeroes.
Last edited on
oh ok i see thanks for the explanation though xD saw my mistake after the first line you wrote but thanks a lot ^ ^ i wont forget


Topic archived. No new replies allowed.