www.digitalmars.com         C & C++   DMDScript  

c++ - variable-length arrays

reply John <smith mail.com> writes:
int main(int argc, char *argv[argc])
{
	return sizeof argv;
}

Is this must return 4?
Feb 27 2008
parent reply Jan Knepper <jan smartsoft.us> writes:
You're asking to return the size of a pointer...
A pointer is 4 bytes on 32 bits Windows...



John wrote:
 int main(int argc, char *argv[argc])
 {
 	return sizeof argv;
 }
 
 Is this must return 4?
-- ManiaC++ Jan Knepper But as for me and my household, we shall use Mozilla... www.mozilla.org
Feb 27 2008
parent reply John <smith mail.com> writes:
int main(int argc, char *argv[argc])
{
    char *vec[] = {"foo", "bar", "google"};
    return sizeof vec;
}

But why this returns 12?
VLAs in both cases are the same.
Feb 27 2008
next sibling parent reply John <smith mail.com> writes:
Ouch...

int main(int argc, char *argv[argc])
{
	int n = 2;
	++n;
	char *vec[n] = {"foo", "bar", "google"};
	return sizeof vec;
}

scppn -A -v2 +all -6 test.c
Digital Mars C/C++ Compiler Version 8.50.4n
Copyright (C) Digital Mars 2000-2006.  All Rights Reserved.
Written by Walter Bright
www.digitalmars.com
 'test.c'
main
Internal error: init.c 1514

--- errorlevel 1
Feb 27 2008
parent John <smith mail.com> writes:
And what about bottom?

== Quote from John (smith mail.com)'s article
 Ouch...
 int main(int argc, char *argv[argc])
 {
 	int n = 2;
 	++n;
 	char *vec[n] = {"foo", "bar", "google"};
 	return sizeof vec;
 }
 scppn -A -v2 +all -6 test.c
 Digital Mars C/C++ Compiler Version 8.50.4n
 Copyright (C) Digital Mars 2000-2006.  All Rights Reserved.
 Written by Walter Bright
 www.digitalmars.com
  'test.c'
 main
 Internal error: init.c 1514
 --- errorlevel 1
Mar 02 2008
prev sibling parent Jan Knepper <jan smartsoft.us> writes:
vec is an array of 3 pointers of 4 bytes each...
3 * 4 = 12 last time I checked...



John wrote:
 int main(int argc, char *argv[argc])
 {
     char *vec[] = {"foo", "bar", "google"};
     return sizeof vec;
 }
 
 But why this returns 12?
 VLAs in both cases are the same.
-- ManiaC++ Jan Knepper www.janknepper.com But as for me and my household, we shall use Mozilla... www.mozilla.org
Mar 01 2008