TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Neil Heller
from: Jasen Betts
date: 2003-02-06 15:28:00
subject: How is memory handled?

Hi Neil.

04-Feb-03 16:21:00, Neil Heller wrote to All

 NH> If I were to create a variable, as such:

don't do that! (it's wrong - looks like a syntax error to me)

do this:

char *foo_pointer ="For whom the bell tolls" ;
  if you want a modifiable pointer to a string that should not be
  modified.

this,

char foo_array[] ="For whom the bell tolls";

  if you want an array of characters than can be modified but foo_array
  can't be pointed somewhere else

or this.

char * foo_array_of_pointers[] ={"For whom the bell tolls"};

  if you want an array of (1) pointers to constant strings (like the first
  example the pointer can be modified to point elsewhere but the intial
  string should not be modified)

  is say should not.  attempting to modify it can have differing effects
  depending on what environment it's running in, whatever happens it will
  probably not be what you want, and may vary dependoing on the compiler,
  OS, OS version etc...

 NH> how does the heap memory get cleared where that static string is
 NH> located?

no.

 foo_pointer is a (char*) pointer that points to a string in the program's
 constant area. on some platforma modifying that string

 foo_array is an array of chars it's sieve is big enough to hold the string
 given in the initialisation including the NUL ('\0') character that
 terminates it.

 foo_array_of_pointer has one eleent that's functionally equivalent to
 foo_pointer.

 NH> If I were to create the variable as:

 NH> static char * foo[] = "For whom the bell tolls";

 NH> would it be cleared any differently?

cleared?

If you declare it as static it's created in the same memory area that's
used by global variables.

static variables behave like global variables except that they are private
to the function that declares them.

if you declare it the normal way insode a function the memory for the variable
is allocated on
the stack. variuables of this type are called "auto" variables. For
variables auto is the opposite of static.

if you want it to be on the heap you need to do it like this.

 char *foo = strdup("For whom the bell tolls");

which allocates some space on the heap anc copies the characters in the
string to the allocated location anf points foo at them.... (or at NULL if
there's not enough memory)

The same effect can be had by using malloc (etc) and strcpy to copy the
string.

and you should  free()  the string after you've finished with it (like
before returning from the function etc)

if you want to use the heap you'll need

#include    //   for strdup and strcpy,  and
#include    //   for free and malloc.

I don't think you want to use the heap yet.

 -=> Bye <=-

---
* Origin: Don't pay your exorcist & get repossessed! (3:640/1042)
SEEN-BY: 633/267 270
@PATH: 640/1042 531 954 774/605 123/500 106/1 379/1 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.