ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
consider strArray holds 5 string value.
while (strArray != NULL) not terminating after parsing 5 string values.
There is no reason it should. The value after the end of the array is not necessarily NULL.
Quote:
Is there any memory validation method available?
If there were, that wouldn't help either. The value after the end of the array is not necessarily an invalid memory address.
I'm only guessing at the context of your problem, but based on that guess, I think you should modify the original source of the array of pointers so that it always allocates one more than the number of actual pointers and always makes sure that extra one (at the end) is NULL.
If the array is static or global, the compiler/linker/loader will make sure the extra is NULL as long as you allocate one more than you initialize. But if the array is stack or heap, you need run time code to set the extra pointer to NULL.
Did you get a segmentation fault when you tried to run it? That is what it looks like you would get. Have you tried to print just one string in the same manner without the loop? I think that you will get a segmentation fault too. Give that a try!
function( int* array ) {
int ndx;
for ( ndx=0; ndx<array[-1]; ++ndx ) {
do something with array[ndx]
}
}
So except for the allocate/deallocate, everything else can treat it as a nearly ordinary array that happens to have its element count stored at array[-1].
Quote:
Originally Posted by wje_lq
Code:
while (strArray != NULL) /* wrong */
Very sloppy of me failing to notice that error in the original post. Usually I look more carefully.
It seems to me that you want to use an array with program arguments argv[].?
An array of arguments is defined like this:
Code:
char *arg[] = {"progname", "op1", (char)0 };
So the end will be detected with:
Code:
while( arg[i] != 0 )
...
So there is no need to search for NULL but for 0 !
And if not, 0 terminated strings are c-strings and will always be handled right by the system !
With NULL there is expressed the need of a char with value 0 and not the character '0' (with value 48).
What compiler are you using?
Could you post the code where you set the string values to the array?
Could you post a test code to print out just one of the arrays in the string?
Quote:
while (strArray != NULL)
{
printf ("%s\n", *strArray);
strArray++;
}
What happened when you tried to run that code? Have you read about pointer arithmetic? I do not think that is what you want to do. Try this piece of code to see what happens:
It seems to me that you want to use an array with program arguments argv[].?
An array of arguments is defined like this:
Code:
char *arg[] = {"progname", "op1", (char)0 };
So the end will be detected with:
Code:
while( arg[i] != 0 )
...
So there is no need to search for NULL but for 0 !
And if not, 0 terminated strings are c-strings and will always be handled right by the system !
With NULL there is expressed the need of a char with value 0 and not the character '0' (with value 48).
FYI - on intel machines, NULL is 0. On other machines, 0 might be a valid address, so using it as a 'special case' is a bad idea (notably, SV5R4 used 0 as a valid address, and linux can emulate this behavior). It is best practice to check explicitly for NULL, which the C library on your platform 'guarantees' to be correct.
FYI - on intel machines, NULL is 0. On other machines, 0 might be a valid address, so using it as a 'special case' is a bad idea (notably, SV5R4 used 0 as a valid address, and linux can emulate this behavior).
-Aaron
Sorry, but NULL is never 0x0000:0x0000, this is the address of the first instruction when you turn on or reset your computer and not only Intel. So you would call the first BIOS instruction with NULL - no, NULL is never 0x0000 nor (char) 0, NULL will always be a address different from 0x0000:0x0000 and With c++ you even can't be sure that NULL from a parent is the same NULL of a child. In that case you even can't search for NULL at all. In sure Linux will have an other NULL as you in your program!
And linux system always needs (char) 0 terminated strings like char *st="hallo" is one, and linux needs also the array to be 0 terminated.
NULL only means that it is a pointer to its self and if NULL is used for a call, instead of a function, some POPs and a ret will be added after. So every NULL you use instead of a different function type will be a different NULL !
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.