LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

lifetime of string

I am concerning about the correctness of the following sample code snip and the liftime of  the string "My Text".
After assigning the pointer pText it will only be used for reading purpose. 
Is "My Text" stable over the liftetime of the program, is it compiler dependent ?

/***********  sample **************/
char  * pText;

MyTextFunction()
{
  ...
  pText ="My Text";
  ...
}

0 Kudos
Message 1 of 3
(3,148 Views)

Duration of variables is different depending on where they are define and which storage class is applied to them. Look at this example code:

*** File mySource.c

int      var1;
static int     var2;

void function1 (void) {
   int    var3;
   static int    var4;

......

   return;
}

*** End of mySource.c

Variables declared in this example have this characteristics:

  • Variables declared in a block (e.g. var3) have the same lifetime as the block (i.e. are released when exiting the block)
  • Variables declared in a block and assigned static storage class (e.g. var4) are allocated the first time the block is run and never released. They can be accessed only within the block they are declared into
  • Variables declared at module level (e.g. var1) are allocated at program start and never released. They can be accessed from any function within the module and from functions in another module if declared there with the extern keyword
  • Assigning them to the static storage class (e.g. var2) limits their scope to within the module only (i.e. they cannot be acessed from outside the module)

 

So, in your code pText is stable for all program life.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 3
(3,112 Views)
Thanks for the fast answer Roberto,
about the storage class of variables i am informed.
What about I am concerning is the content of  the adress, where pText is pointing to after the call to MyTextFunction().
Is the content  "My Text" stable outside that function? Is it compiler dependent ?
0 Kudos
Message 3 of 3
(3,064 Views)