Link to home
Start Free TrialLog in
Avatar of naseeam
naseeamFlag for United States of America

asked on

How to make sure compiler doesn't optimize out pointer variable assigned with main 'c' function?

Consider Hello World program modified to illustrate embedded system Virtual Memory concepts:

#include <stdio.h>

   int bss_var;       /* uninitialized global variable */
   int data_var = 1;  /* initialized global variable */
   
   int main( int argc, char ** argv)
   {
      void *stack_var;    /* Local variable on the stack */
	  
	  stack_var = (void *)main;  /* Don't let the compiler optimize it out */
	  
	  printf("Hello, World! Main is executing at %p\n", stack_var);
	  printf("This address (%p) is in our stack frame\n", &stack_var);
	  
	  /* bss section contains uninitialized data */
	  printf("This address (%p) is in our bss section\n", %bss_var);
	  
	  /* data section contains initialized data */
	  printf("This address (%p) is in our data section\n", &data_var);
	  
	  return 0;
	  
   }

Open in new window


This program is created in BeagleBoard-xM running some Linux distribution.  This Linux distribution has vi editor and gcc compiler in it.
The line in the above program, "Don't let the compiler optimize it out", how do I make sure compiler doesn't optimize this line of code.  Do I need to compile
with some compiler option?
ASKER CERTIFIED SOLUTION
Avatar of Dr. Klahn
Dr. Klahn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of naseeam

ASKER

very fast and great solutions.  Thank you!