In: Computer Science
Modify the following code to use 'envp' instead of 'environ'. Be sure that you understand how the code works. Provide liberal comments to explain what your pointer arithmetic is computing. Also, answer the following questions and explain your answers:
i) WHERE in process memory is it most likely that each of the values exist at run time?
ii) WHERE in process memory is it most likely the actual strings containing environment variables are stored?
#include
#include
extern char **environ; // look into what “extern” means when
applied to
// a C global variable :)
int main(int argc, char **argv)
{ char **env_variable_ptr;
env_variable_ptr = environ;
while (*env_variable_ptr != NULL)
{ printf("%s\n", *env_variable_ptr);
env_variable_ptr++;
}
printf("\n");
}
1) The programming languages like C and C++, its static and dynamic memory allocation. All memory waas allocated during compile time.
C/C++ code to mashine output an executable file allocated in a compile file, memory always allocated in the RAM with the virtual memory.
The memory allocation at run/ compile-time resolves at the inside process of memory map
consider a global array variable.
int array [100];
Its compile-time the size of the array and the size of an int, its static storage global variable is allocated in static memory area of process memory.
Value initialized static storage.
int array[] = { 1,2,3,4,5 }
2) It consider memory segment data,heap,stack,and code, is allocated space by globle variable ,static variable get storage in memory.
1.global variable .. // data
2. static variables... // data
3. constant data type...// Its code and /or data, as string constant itself can be stored in the data sagment.
4. local variables... // stack
5. pointers... // ie *arr,int // It has heap data or stack,as declare a global or static pointer.
6. dynamically allocation ....// Its space using stack heap ( malloc,calloc,or realloc).