L
Lars Erdmann
Hallo,
I don't know all the subtleties of the C language, therefore if I have
something like the code below:
1.) is it assured for all C compilers that the "function" will always
work on the memory space occupied by array type variable "myArray" or is
it possible for some compilers that it might work on a stack copy of
"myArray" (and therefore "function" would not change the contents of
variable "myArray" but only the stack copy) ?
2.) equivalently, is it assured for all C compilers that the address of
"myArray" will be passed to "function" instead of the whole contents of
"myArray" be copied into the stack ?
Lars
#include <stdio.h>
typedef unsigned long TULONGArray10[10];
void function(TULONGArray10 a)
{
printf("Adress of array variable in function:%#p\n",(void *)a);
printf("In function call:\n");
{
unsigned long i;
for(i=0;i<10;i++)
{
a = i;
printf("a[%lu] = %lu\n",i,a);
}
}
return;
}
int main(int argc,char *argv[])
{
TULONGArray10 myArray={0};
printf("Adress of array variable in main:%#p\n",(void *)myArray);
printf("Before function call:\n");
{
unsigned long i;
for (i=0;i<10;i++)
{
printf("a[%lu] = %lu\n",i,myArray);
}
}
function(myArray);
printf("After function call:\n");
{
unsigned long i;
for (i=0;i<10;i++)
{
printf("a[%lu] = %lu\n",i,myArray);
}
}
return 0;
}
I don't know all the subtleties of the C language, therefore if I have
something like the code below:
1.) is it assured for all C compilers that the "function" will always
work on the memory space occupied by array type variable "myArray" or is
it possible for some compilers that it might work on a stack copy of
"myArray" (and therefore "function" would not change the contents of
variable "myArray" but only the stack copy) ?
2.) equivalently, is it assured for all C compilers that the address of
"myArray" will be passed to "function" instead of the whole contents of
"myArray" be copied into the stack ?
Lars
#include <stdio.h>
typedef unsigned long TULONGArray10[10];
void function(TULONGArray10 a)
{
printf("Adress of array variable in function:%#p\n",(void *)a);
printf("In function call:\n");
{
unsigned long i;
for(i=0;i<10;i++)
{
a = i;
printf("a[%lu] = %lu\n",i,a);
}
}
return;
}
int main(int argc,char *argv[])
{
TULONGArray10 myArray={0};
printf("Adress of array variable in main:%#p\n",(void *)myArray);
printf("Before function call:\n");
{
unsigned long i;
for (i=0;i<10;i++)
{
printf("a[%lu] = %lu\n",i,myArray);
}
}
function(myArray);
printf("After function call:\n");
{
unsigned long i;
for (i=0;i<10;i++)
{
printf("a[%lu] = %lu\n",i,myArray);
}
}
return 0;
}