Default arguments & arrays
Hello everyone, I''m new to both the forum and to game development.
My question is about default arguments. I have only recently began to use them extensively, but have run into a problem in one instance. What is the syntax for default arrays? Specifically, I have this written out:
void func(some_type array[]={ }, int num=0);
...
void func(some_type array[], int num)
{
...
}
My intent is to have the default be some sort of "empty" array, if there is such a thing, and "num" be zero, representing the size of the array.
Visual C++ 6 is giving me some error about unmatched left parenthesis in the default arguments. The actual implementation I''m using is the constructor of one of my classes, but I doubt that makes a difference.
Any help or advice is appreciated, even not directly related to my question.
-desamaru
Well, basically an array is series of the same variable type in a row. Given that, if you have an array
CHAR sString[20];
Is a slab of one 20 one byte parts. and sString by itself is the address to the first character of this string.
if you write sString[15] this is identical to *(sString + 15), so you can see from this that a array is the same as a pointer to a slab of memory.
CHAR sString[20]; is the same as
CHAR *sString;
sString = (CHAR *)malloc(20);
So now that we have established that arrays and pointers are the same thing here is what you do.
Basically you make a default pointer to whatever type you want, and the size of it.
void func(some_type *array = NULL, int num=0);
When you make the code for the function, you can treat it like an array if you really want to. I hope you understand all this and it helps you out. Just remember the golden rule. Everything is just memory and addresses to memory.
Sincerely,
Randy Trulson
www.GamePotato.com
www.NeuronGames.com
CHAR sString[20];
Is a slab of one 20 one byte parts. and sString by itself is the address to the first character of this string.
if you write sString[15] this is identical to *(sString + 15), so you can see from this that a array is the same as a pointer to a slab of memory.
CHAR sString[20]; is the same as
CHAR *sString;
sString = (CHAR *)malloc(20);
So now that we have established that arrays and pointers are the same thing here is what you do.
Basically you make a default pointer to whatever type you want, and the size of it.
void func(some_type *array = NULL, int num=0);
When you make the code for the function, you can treat it like an array if you really want to. I hope you understand all this and it helps you out. Just remember the golden rule. Everything is just memory and addresses to memory.
Sincerely,
Randy Trulson
www.GamePotato.com
www.NeuronGames.com
Thank you very much. This helped incredibly. That part about the memory address addition was particularly useful, as I have always been foggy as to how the subscript syntax of arrays relates to the memory addresses.
-desamaru
-desamaru
quote:Original post by Randy of Neuron
CHAR sString[20];
is the same as
CHAR *sString;
sString = (CHAR *)malloc(20);
Not quite. The difference that causes the most problems can be seen by comparing the value of
(sizeof sString / sizeof *sString)
in both cases. In the first case it should give you 20; in the second it should give you sizeof(char *). This is mostly a problem with beginners who learn the above as a method of finding the length of an array and then get confused when they try to use it on an array passed to a function.
char CharArray[20];char* DynamicCharArray = new char[20]; DynamicCharArray[0] = CharArray[0] = '\0'; char* CharPointer = 0;CharPointer = CharArray; delete[] DynamicCharArray;
Arrays and pointers are very closely linked. For example, you can offset into the memory pointed to by DynamicCharArray the same way as you would CharArray: DynamicCharArray[0] accesses the first element. In the same way, you can treat CharArray (with no array index) as a pointer, although some casting may be necessary. [edited by - psykr on June 5, 2004 3:14:29 PM]
Ok guys, I was doing fine until we started getting into sizeof. Passing arrays around I''m doing just fine, so long as I am also passed the size. But as Way Walker pointed out, things get sticky when trying to count the array. Here''s a chunk of code displaying my confusion...
//array passing type 1
template
int countarray1(itype *array)
{
return sizeof(array)/sizeof(itype);
}
//array passing type 2
template
int countarray2(itype array[])
{
return sizeof(array)/sizeof(itype);
}
int main()
{
char array[20];
char *dynamicarray=new char[20];
cout<cout<<''\n'';
cout<
cout<<"\n\n"< <
delete[] dynamicarray;
return 0;
}
If we were to run this, we see that all 4 combinations of array types and passing types return simply the size of the actual pointer, rather than entire array as desired. The only method that worked was counting the array within the actual function it is declared (main), and even then, the dynamic array does not get counted correctly, and instead returns the size of char*, as Way Walker also pointed out.
If there is yet a BETTER way of passing around ararys other than the two listed, I''m all ears. Maybe my method of counting is flawed... Again, any input in any respect is appreciated.
-desamaru
//array passing type 1
template
int countarray1(itype *array)
{
return sizeof(array)/sizeof(itype);
}
//array passing type 2
template
int countarray2(itype array[])
{
return sizeof(array)/sizeof(itype);
}
int main()
{
char array[20];
char *dynamicarray=new char[20];
cout<cout<<''\n'';
cout<
cout<<"\n\n"< <
delete[] dynamicarray;
return 0;
}
If we were to run this, we see that all 4 combinations of array types and passing types return simply the size of the actual pointer, rather than entire array as desired. The only method that worked was counting the array within the actual function it is declared (main), and even then, the dynamic array does not get counted correctly, and instead returns the size of char*, as Way Walker also pointed out.
If there is yet a BETTER way of passing around ararys other than the two listed, I''m all ears. Maybe my method of counting is flawed... Again, any input in any respect is appreciated.
-desamaru
Argh.... I''m new to the forum. My previous post got cut.. trying again, starting from the program:
//array passing type 1
template
int countarray1(itype *array)
{
return sizeof(array)/sizeof(itype);
}
//array passing type 2
template
int countarray2(itype array[])
{
return sizeof(array)/sizeof(itype);
}
int main()
{
char array[20];
char *dynamicarray=new char[20];
cout<cout<<''\n'';
cout<
cout<<"\n\n"< <
delete[] dynamicarray;
return 0;
}
If we were to run this, we see that all 4 combinations of array types and passing types return simply the size of the actual pointer, rather than entire array as desired. The only method that worked was counting the array within the actual function it is declared (main), and even then, the dynamic array does not get counted correctly, and instead returns the size of char*, as Way Walker also pointed out.
If there is yet a BETTER way of passing around ararys other than the two listed, I''m all ears. Maybe my method of counting is flawed... Again, any input in any respect is appreciated.
-desamaru
//array passing type 1
template
int countarray1(itype *array)
{
return sizeof(array)/sizeof(itype);
}
//array passing type 2
template
int countarray2(itype array[])
{
return sizeof(array)/sizeof(itype);
}
int main()
{
char array[20];
char *dynamicarray=new char[20];
cout<cout<<''\n'';
cout<
cout<<"\n\n"< <
delete[] dynamicarray;
return 0;
}
If we were to run this, we see that all 4 combinations of array types and passing types return simply the size of the actual pointer, rather than entire array as desired. The only method that worked was counting the array within the actual function it is declared (main), and even then, the dynamic array does not get counted correctly, and instead returns the size of char*, as Way Walker also pointed out.
If there is yet a BETTER way of passing around ararys other than the two listed, I''m all ears. Maybe my method of counting is flawed... Again, any input in any respect is appreciated.
-desamaru
Hmm... I see it''s interpreting angle brackets as HTML tags. Let me just post the most important info...
//array passing type 1
int countarray1(char *array)
{
return sizeof(array)/sizeof(char);
}
//array passing type 2
int countarray2(char array[])
{
return sizeof(array)/sizeof(char);
}
int main()
{
char array[20];
char *dynamicarray=new char[20];
countarray1(array);countarray2(array);
countarray1(dynamicarray);countarray2(dynamicarray);
sizeof(array)/sizeof(char);sizeof(dynamicarray)/sizeof(char);
delete[] dynamicarray;
return 0;
}
If we were to run this, we see that all 4 combinations of array types and passing types return simply the size of the actual pointer, rather than entire array as desired. The only method that worked was counting the array within the actual function it is declared (main), and even then, the dynamic array does not get counted correctly, and instead returns the size of char*, as Way Walker also pointed out.
If there is yet a BETTER way of passing around ararys other than the two listed, I''m all ears. Maybe my method of counting is flawed... Again, any input in any respect is appreciated.
-desamaru
//array passing type 1
int countarray1(char *array)
{
return sizeof(array)/sizeof(char);
}
//array passing type 2
int countarray2(char array[])
{
return sizeof(array)/sizeof(char);
}
int main()
{
char array[20];
char *dynamicarray=new char[20];
countarray1(array);countarray2(array);
countarray1(dynamicarray);countarray2(dynamicarray);
sizeof(array)/sizeof(char);sizeof(dynamicarray)/sizeof(char);
delete[] dynamicarray;
return 0;
}
If we were to run this, we see that all 4 combinations of array types and passing types return simply the size of the actual pointer, rather than entire array as desired. The only method that worked was counting the array within the actual function it is declared (main), and even then, the dynamic array does not get counted correctly, and instead returns the size of char*, as Way Walker also pointed out.
If there is yet a BETTER way of passing around ararys other than the two listed, I''m all ears. Maybe my method of counting is flawed... Again, any input in any respect is appreciated.
-desamaru
Hello everyone, I''m new to both the forum and to game development.
Welcome to both.
If there is yet a BETTER way of passing around ararys other than the two listed, I''m all ears. Maybe my method of counting is flawed... Again, any input in any respect is appreciated.
In C++, array is spelled std::vector.
And you can post source code by bracketing it with [source] and [/source].
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan
Welcome to both.
If there is yet a BETTER way of passing around ararys other than the two listed, I''m all ears. Maybe my method of counting is flawed... Again, any input in any respect is appreciated.
In C++, array is spelled std::vector.
And you can post source code by bracketing it with [source] and [/source].
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan
quote:Original post by Fruny
In C++, array is spelled std::vector.
I couldn''t agree more. desamaru, listen to Fruny.
quote:
My question is about default arguments. I have only recently began to use them extensively, but have run into a problem
This reminds me of something Petzold wrote in his Programming Windows book regarding multithreading. He said something like "just because it''s cool and it''s new and it''s there that doesn''t mean you should always use it." The point is...use default arguments when they make sense, not just because they''re pretty cool. In your case, it would make much more sense to just make the array (you should use a vector instead but that''s not the point) local to the function''s body.
And about arrays vs vectors...if you find yourself using an array when you''re not sure how many elements you''ll put in it, then you have to ask yourself whether an array is really what you want.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement