Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: How is memory allocated (http://www.velocityreviews.com/forums/t313881-re-how-is-memory-allocated.html)

Micah Cowan 06-28-2003 03:02 AM

Re: How is memory allocated
 
samuel@avenir.net (Samuel Thomas) writes:

> Hello Everybody,
>
> Could you please go through the code I wrote and help me with my
> doubts?
>
>
> #include <stdio.h>
> #include <conio.h>


The above (conio) is not a standard C header, and off-topic here.

>
> void printnamefirst(char[]);
> void printnamesec(char[]);
>
> void main()
> {
> clrscr();


Great. You just obliterated the tail-end of an output run I still wanted.

> printnamefirst(name);
> }
>
> void printnamefirst(char nm[])
> {
> char nam[20] ="Samuej Thomas";
> printnamesec(nam);
> printf("%s \n",nam);
> }
>
> void printnamesec(char ns[])
> {
> printf("%s \n",ns);
> ns[5]='l';
>
> }
>
> 1.Is it safe to use the variables that are allocated in one function,
> in another function as I have done by printing a string in the
> printnamesec, but which has been declared in printnamefirst function?
> When does it become unsafe to use variables declared in one function
> else where?


It is safe to refer to that object until execution leaves the block in
which it was defined. It is unsafe any other time. Here are some
unsafe examples:

void printnamefirst(char nm[])
{
char *namptr;

{
char nam[20] = "Samuej Thomas";
namptr = nam;
} /* nam no longer exists once we reach here.

printnamesec(nam); /* Uh-oh... */
}

--------------------------------------------------

#include <stdio.h>

int main(void)
{
char *nam = get_name();

printf("%s\n",nam); /* Uh-oh... */
return 0;
}

char *get_name(void)
{
char nam[] = "Samuej Thomas";
return nam;
} /* nam doesn't exist once this exits. */

> 2.Is it possible to make 'pass by value' work with character strings
> so that they dont get changed?


In C, *all* parameters are passed by value; no exceptions.

To avoid a string being changed, declare the parameter to be of type
const char * rather than char *.

> Do they always get passed as reference
> values when passed across functions?


C doesn't support anything being passed by reference.

> Does the value of the nam
> variable declared in printnamefirst get modified because of the 'pass
> by reference' mechanism?


No: you didn't pass a string by reference (it is impossible to pass
strings at all in C); you passed a pointer-to-char. That
pointer-to-char was passed by value, but it still points to the same
place in memory. It's not possible to pass entire arrays by value in
C; the best way to handle this is to pass it through a parameter of
const char *:

void considerate_func(const char *foo);
/* considerate_func() promises that it won't alter any of the
characters pointed at by foo. */

void unscrupulous_func(char *foo);
/* unscrupulous_func() makes no such guarantees... */

HTH,
Micah



All times are GMT. The time now is 12:59 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.