A question about telling the size of an array to a function

Programming Topics (Computer Chess) and technical aspects as test techniques, book building, program tuning etc

Moderator: Andres Valverde

A question about telling the size of an array to a function

Postby Uri Blass » 20 Feb 2005, 09:24

1)I have some array info1 that is not a global variable
char info1[64]

I want to change the array inside the function that get the array.
My question is how to do it correctly.

The problem is that if I pass an array to a function, the actual array does not get passed.
The thing that does get passed is a pointer to the array. The size of
the pointer is 4 bytes (the address is 4 bytes for most compilers but
perhaps 8 bytes on some systems).



2)Dann Corbit told that me that if I need to tell the size of an array to a function, then I must
pass it in as a parameter or I can also wrap the array or the pointer to data in a struct and
pass that like this:

typedef struct tag_my_sizeable_thing {
char *data;
size_t data_length;
} my_sizeable_thing;



3)I still do not understand from this explanation how to tell function to change local array info1 correctly.

If you can give me an example how to do correctly only
memset(info1,'0', 64); inside a function that only get info1 and the size of it as parameters(info1 is used only in 2 files and I do not like it to be a global array) it can be productive

I guess that if the function gets only a pointer then memset(info1,'0',64) may change the content of other variables so of course it is not correct.

I can of course ask Dann Corbit about it but usually he is not available on sunday so I guess that I can get a faster reply here.

Uri
User avatar
Uri Blass
 
Posts: 727
Joined: 09 Oct 2004, 05:59
Location: Tel-Aviv

Re: A question about telling the size of an array to a funct

Postby Rémi Coulom » 20 Feb 2005, 09:38

Uri Blass wrote:If you can give me an example how to do correctly only
memset(info1,'0', 64); inside a function that only get info1 and the size of it as parameters(info1 is used only in 2 files and I do not like it to be a global array) it can be productive

I guess that if the function gets only a pointer then memset(info1,'0',64) may change the content of other variables so of course it is not correct.Uri


I think you guess wrong.

Code: Select all
void function(char *info1, int size)
{
 memset(info1,'0', size);
}

void otherfunction(void)
{
 char info1[64];
 function(info1, 64);
}


The code above should work (I have not tested it). Note that the memset function itself takes info1 and its size as a parameter, and works.

I had also noticed your remark about not understanding some code by Dann with pointers to pointers (**). You seem to have problems understanding how pointers work. Do you understand that a pointer is like another variable, and contains the address of the data in memory ? Once you understand this principle, everything should be easy for you to understand.

I am often amazed at the programming questions you ask, considering the level of your chess program. Note that it is not an accusation of anything. You have a very strange way of thinking.

R?mi
Rémi Coulom
 
Posts: 96
Joined: 12 Nov 2004, 13:47
Location: Lille, France

Re: A question about telling the size of an array to a funct

Postby Uri Blass » 20 Feb 2005, 09:48

I understand that a pointer is like another variable, and contains the address of the data in memory.

the problem that I think about is that if the address of info1 is 500
and the address of varaible x is 520 then memset(info1,'0',64) may change x.

The fact that sizeof(info1) is not 64 before
memset(info1,'0', size); suggest that the computer did not save 64 places in memory for info1.

Uri
User avatar
Uri Blass
 
Posts: 727
Joined: 09 Oct 2004, 05:59
Location: Tel-Aviv

Re: A question about telling the size of an array to a funct

Postby Fabien Letouzey » 20 Feb 2005, 09:55

Hi Uri,

Uri Blass wrote:The fact that sizeof(info1) is not 64 before
memset(info1,'0', size); suggest that the computer did not save 64 places in memory for info1.


I understand your confusion now, but be assured that Remi is right.

Inside of your function, "info1" has become a pointer (it was an array outside of this function) and sizeof() will not report the size you expected (but instead the size of a pointer).

In other words, C functions have no way of knowing the size of an array passed as an argument and that's exactly why you need to pass the size as well.

Try sizeof in a function that sees "info1" directly and the correct value will appear ...

Fabien.
Fabien Letouzey
 
Posts: 110
Joined: 03 Dec 2004, 10:17
Location: France

Re: A question about telling the size of an array to a funct

Postby Uri Blass » 20 Feb 2005, 10:06

Thanks


I still do not understand what it means that I need to pass the size
Remi suggested:

void function(char *info1, int size)
{
memset(info1,'0', size);
}

void otherfunction(void)
{
char info1[64];
function(info1, 64);
}


Is there a difference bwteen it and the following code when the function does not get the size as a parameter.

void function(char *info1)
{
memset(info1,'0', 64);
}

void otherfunction(void)
{
char info1[64];
function(info1);
}
User avatar
Uri Blass
 
Posts: 727
Joined: 09 Oct 2004, 05:59
Location: Tel-Aviv

Re: A question about telling the size of an array to a funct

Postby Rémi Coulom » 20 Feb 2005, 10:13

If the function always operates on an array of size 64, there is no need to provide the size as a parameter, of course.

You are really amazing.

R?mi
Rémi Coulom
 
Posts: 96
Joined: 12 Nov 2004, 13:47
Location: Lille, France

Re: A question about telling the size of an array to a funct

Postby Fabien Letouzey » 20 Feb 2005, 10:18

Uri Blass wrote:
I still do not understand what it means that I need to pass the size
Remi suggested:



Don't worry too much.

There are subtle differences between "arrays" and "pointers" in C. sizeof is one of them. The thing is that arrays cannot be passed as arguments in C, so the compiler converts them to pointers. In that case, sizeof will "not work".

Either pass a size or use 64 or whatever, but *don't use sizeof* (in this case).

Try printing sizeof at different places in your program (including after the array declaration) and you will see for yourself.

Fabien.
Fabien Letouzey
 
Posts: 110
Joined: 03 Dec 2004, 10:17
Location: France

Re: A question about telling the size of an array to a funct

Postby Uri Blass » 20 Feb 2005, 10:27

I guess that the confusion was because of the email of Dann Corbit when he wrote

"If you need to tell the size of an array to a function, then you must
pass it in as a parameter."

If I understand correctly in case that the address of info1 is 500
and in case that I defined char info1[64]; then addresses 500-563 will not be used for other variables even in function that does not know about info1.

original mistake of me was that I did
memset(info1,0,sizeof(info1)) but I thought that even memset(info1,0,64) is not correct for the reason that I explained.

Uri
User avatar
Uri Blass
 
Posts: 727
Joined: 09 Oct 2004, 05:59
Location: Tel-Aviv

Re: A question about telling the size of an array to a funct

Postby Uri Blass » 20 Feb 2005, 10:51

In second thought is is obvious because if address 500-563 will be used for another variable then info1 may be changed later by function that was
not supposed to change it and the problem is not only local variables in another function.

Uri
User avatar
Uri Blass
 
Posts: 727
Joined: 09 Oct 2004, 05:59
Location: Tel-Aviv

Re: A question about telling the size of an array to a funct

Postby Uri Blass » 20 Feb 2005, 11:34

I can add that the reason that I assumed that I have a bug is also because I got different number of nodes from the same position with a new function(I still do not use it because my default assumption is that I have a bug in the new function).

Now I understand that the problem is different and I simply have a different order of pieces inside the array.

In the original function I added squares to the board in the following order
56,57,58,...63,48,49,...55,...0,1,2,3,4,5,6,7

in the new function I add squares to the board in the order
0,1,2,....63

The result is that the order of pieces in my piece list is different.
It causes different order of moves and different number of nodes(not in the opening position but in different positions).

I still do not fully trust the new code to have no bugs at this moment but at least testing to show that the number of nodes is the same is a wrong test.

Uri
User avatar
Uri Blass
 
Posts: 727
Joined: 09 Oct 2004, 05:59
Location: Tel-Aviv

Re: A question about telling the size of an array to a funct

Postby Anonymous » 20 Feb 2005, 16:38

Uri, you got good advice already. I suggest you read chapter 6 of Steve Summit's excellent FAQ for the newsgroup comp.lang.c inside out.

Fabien mentioned that there are subtle differences between arrays and pointers in C. Hmmm - I would say there are subtle similarities. One can use the same synthax array[i] and pointer[i]. But they do mean different things actually:

extern int array[];
extern int *pointer;

[...]
pointer[0] = 1;
array[0] = 1;

will produce very different code for the two accesses. Perhaps it is better, to think of arrays and pointers as of very different things. What is confusing (and not really logical) is that the C-language has no means to pass an array to a function directly (it can be done by hiding the array inside a struc), instead it decays to a pointer. And in

void some_funct(int array_param[], int *pointer_param)
{
array_param[0] = 1;
pointer_param[0] = 1;
}

both accesses produce practically the same code. Both parameters are pointers to int, even when the first looks like an array.

I add the table of contents of chapter 6 of the C FAQ. The other chapters will be worth reading, too. Note that 6.21 is exactly your question.

Regards,
Dieter


Section 6. Arrays and Pointers

6.1: I had the definition char a[6] in one source file, and in
another I declared extern char *a. Why didn't it work?
6.2: But I heard that char a[] was identical to char *a.
6.3: So what is meant by the "equivalence of pointers and arrays" in
C?
6.4: Why are array and pointer declarations interchangeable as
function formal parameters?
6.7: How can an array be an lvalue, if you can't assign to it?
6.8: What is the real difference between arrays and pointers?
6.9: Someone explained to me that arrays were really just constant
pointers.
6.11: I came across some "joke" code containing the "expression"
5["abcdef"] . How can this be legal C?
6.12: What's the difference between array and &array?
6.13: How do I declare a pointer to an array?
6.14: How can I set an array's size at run time?
6.15: How can I declare local arrays of a size matching a passed-in
array?
6.16: How can I dynamically allocate a multidimensional array?
6.17: Can I simulate a non-0-based array with a pointer?
6.18: My compiler complained when I passed a two-dimensional array to
a function expecting a pointer to a pointer.
6.19: How do I write functions which accept two-dimensional arrays
when the width is not known at compile time?
6.20: How can I use statically- and dynamically-allocated
multidimensional arrays interchangeably when passing them to
functions?
6.21: Why doesn't sizeof properly report the size of an array which is
a parameter to a function?
Anonymous
 

Re: A question about telling the size of an array to a funct

Postby Tony van Roon-Werten » 21 Feb 2005, 07:41

What I found usefull is when I saw that

int *ptr
int MyArray[64]

ptr=MyArray and ptr=&MyArray[0] are equivelent.

So pointer to an array is the same as the adress of array element[0].

Tony
Tony van Roon-Werten
 
Posts: 99
Joined: 02 Oct 2004, 15:31
Location: 's Hertogenbosch, Netherlands


Return to Programming and Technical Discussions

Who is online

Users browsing this forum: No registered users and 31 guests