int main(int argc, char* argv[]) & int main(int argc, char** a

int main(int argc, char* argv[]) & int main(int argc, char** a

Post by Vmlu » Sat, 06 Jan 2007 17:15:00


Hi,

Thanks for all responses. I access the arguments for both cases using
argv[1]...argv[n]

But what's up with the @# and where did it come from?

It will only show up for char* argv[] and not for char** argv

for char* argv[]: Another weird thing is that at times it will have this @#
and at other times it doesn't. Also if I execute the program on another
computer it never show up all.

Any ideas?
 
 
 

int main(int argc, char* argv[]) & int main(int argc, char** a

Post by Vmlu » Sat, 06 Jan 2007 17:16:00

Hi,

Thanks for all responses. I access the arguments for both cases using
argv[1]...argv[n]

But what's up with the @# and where did it come from?

It will only show up for char* argv[] and not for char** argv

for char* argv[]: Another weird thing is that at times it will have this @#
and at other times it doesn't. Also if I execute the program on another
computer it never show up all.

Any ideas?

 
 
 

int main(int argc, char* argv[]) & int main(int argc, char** a

Post by Bruno van » Sat, 06 Jan 2007 18:34:23


Hi,
That could be the problem. indexing in arrays is 0 based. meaning the first
element has index 0, and the last element has index n-1.
if n is the size of your array, indexing should be done
argv[0]...argv[n-1]

--

Kind regards,
Bruno van Dooren
XXXX@XXXXX.COM
Remove only "_nos_pam"
 
 
 

int main(int argc, char* argv[]) & int main(int argc, char** a

Post by Tamas Demj » Sun, 07 Jan 2007 03:31:12


If you address an item outside of the array's memory space, such things
are expected to happen. Nobody knows where it's coming from, that's just
the content of your memory -- garbage, or the content of other objects,
perhaps leftover by the operating system or the de *** . You have no
control over that. Once you address an invalid pointer, the consequences
are undefined and somewhat random.


As others have already stated it, there's no difference between those
two types when they are used in function arguments.

If you don't believe it, cut and paste these 2 definitions:
void f(char* argv[]) { }
void f(char** argv) { }

You'll get a compiler error, something like "body has already been
defined for 'f(char**)'". They violate the One Definition Rule, because
they're identical.


See, this just proves the random nature of this behavior. It doesn't
depend on whether the type is char*[] or char**, but on circumstances
beyond your control. Due to a programming error, you're exploiting
undefined behavior. I have no question in my mind that when you use that
array properly, it will return the correct value.

Tom
 
 
 

int main(int argc, char* argv[]) & int main(int argc, char** a

Post by Doug Harri » Sun, 07 Jan 2007 05:38:44

On Fri, 5 Jan 2007 00:15:00 -0800, Vin < XXXX@XXXXX.COM >



If you're using argv[n] for n > argc, the result is undefined, which means
anything is possible. You can compile with /FAs and compare the assembly
code generated by the compiler, but if the only difference is char** vs.
char*[], I'd expect it to be the same. How are you determining what the
strings contain? Show the code.

--
Doug Harrison
Visual C++ MVP
 
 
 

int main(int argc, char* argv[]) & int main(int argc, char** a

Post by QnJ1bm8gdm » Tue, 09 Jan 2007 17:32:00

> Thanks for all responses. I access the arguments for both cases using

If you go beyond the boundaries of the array, you get undefined values.
I.e. argv[n] returns something, but whatever it is is not subject to logical
behavior. You just get whatever value happens to be after argv[n-1]

That value is then interprested as a char pointer. That pointer can point
anywhere, since is is not a real pointer.
When you try to use that pointer to read a string, the string function will
start at that pointer, and search until it finds a 0 that indicates the end
of the string.

If you are lucky, it will find a 0 and use a bogus string.
if you are unlucky, it will continue searching until it hits an inaccessible
addres, at which time you will get an access violation.

since this is undefined behavior, you cannot compare what happens on 2
different computers, or 2 different runs of the application.
Your result depends on whatever happens to be in argv[n] when you read it.

--
Kind regards,
Bruno.
XXXX@XXXXX.COM
Remove only "_nos_pam"