/* memsegments.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct mem
{
char text[100];
int *p;
} mem;
int cmp_by_address(const void *, const void *);
void print_struct_array(mem *, size_t);
int init_global_var = 10; /* Initialized global variable */
int global_var; /* Uninitialized global variable */
static int init_static_var = 20; /* Initialized static variable in global scope */
static int static_var; /* Uninitialized static variable in global scope */
int main(int argc, char **argv, char **envp)
static int init_static_local_var = 30; /* Initialized static local variable */
static int static_local_var; /* Uninitialized static local variable */
int init_local_var = 40; /* Initialized local variable */
int local_var; /* Uninitialized local variable */
int *dynamic_var = (int*)malloc(sizeof(int)); /* Dynamic variable */
mem structs[] =
{"Global variable (initialized)", &init_global_var},
{"Global variable (uninitialized)", &global_var},
{"Static variable (in global scope, initialized)", &init_static_var},
{"Static variable (in global scope, uninitialized)", &static_var},
{"Static variable (in local scope, initialized)", &init_static_local_var},
{"Static variable (in local scope, uninitialized)", &static_local_var },
{"Function (code)", (int*)&main },
{"Environment variable", (int*)&envp[0] },
{"Local variable (initialized)", &init_local_var },
{"Local variable (uninitialized)", &local_var },
{"Dynamic variable", dynamic_var },
};
size_t len = sizeof(structs) / sizeof(mem);
qsort(structs, len, sizeof(mem), cmp_by_address);
print_struct_array(structs, len);
free(dynamic_var);
return 0;
}
int cmp_by_address(const void *a, const void *b)
mem *ma = (mem *)a;
mem *mb = (mem *)b;
if ((unsigned)ma->p > (unsigned)mb->p)
return -1;
else if ((unsigned)ma->p < (unsigned)mb->p)
return 1;
else
/* Example struct array printing function */
void print_struct_array(mem *array, size_t len)
size_t i;
for(i=0; i<len; i++)
printf("%-50s:\t%p\n", array[i].text, array[i].p);
В результате получим:
Environment variable : 0xbff52ee0
Local variable (uninitialized) : 0xbff529ac
Local variable (initialized) : 0xbff529a8
Dynamic variable : 0x871c008
Global variable (uninitialized) : 0x804a044
Static variable (in local scope, uninitialized) : 0x804a040
Static variable (in global scope, uninitialized) : 0x804a03c
Static variable (in local scope, initialized) : 0x804a034
Static variable (in global scope, initialized) : 0x804a030
Global variable (initialized) : 0x804a02c
Function (code) : 0x80484ad
Утилита size показывает размер разделов и общий размер для объектных файлов или архивов. Так, для memsegments.o получим:
$ size memsegments.o
text data bss dec hex filename
745 12 8 765 2fd memsegments.o
Объяснение:
№1
#include <iostream>
using namespace std;
int main() {
setlocale(LC_ALL, "Rus");
char st[100];
char rm[10] = " РМ";
gets_s(st);
strcat_s(st, rm);
cout << st;
№2
string st = "получены результаты контрольной работы";
cout << sizeof(st) << endl;
cout << st.find("результаты") << endl;
st.insert(st.find("результаты") + 10, " итоговой");
/* memsegments.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct mem
{
char text[100];
int *p;
} mem;
int cmp_by_address(const void *, const void *);
void print_struct_array(mem *, size_t);
int init_global_var = 10; /* Initialized global variable */
int global_var; /* Uninitialized global variable */
static int init_static_var = 20; /* Initialized static variable in global scope */
static int static_var; /* Uninitialized static variable in global scope */
int main(int argc, char **argv, char **envp)
{
static int init_static_local_var = 30; /* Initialized static local variable */
static int static_local_var; /* Uninitialized static local variable */
int init_local_var = 40; /* Initialized local variable */
int local_var; /* Uninitialized local variable */
int *dynamic_var = (int*)malloc(sizeof(int)); /* Dynamic variable */
mem structs[] =
{
{"Global variable (initialized)", &init_global_var},
{"Global variable (uninitialized)", &global_var},
{"Static variable (in global scope, initialized)", &init_static_var},
{"Static variable (in global scope, uninitialized)", &static_var},
{"Static variable (in local scope, initialized)", &init_static_local_var},
{"Static variable (in local scope, uninitialized)", &static_local_var },
{"Function (code)", (int*)&main },
{"Environment variable", (int*)&envp[0] },
{"Local variable (initialized)", &init_local_var },
{"Local variable (uninitialized)", &local_var },
{"Dynamic variable", dynamic_var },
};
size_t len = sizeof(structs) / sizeof(mem);
qsort(structs, len, sizeof(mem), cmp_by_address);
print_struct_array(structs, len);
free(dynamic_var);
return 0;
}
int cmp_by_address(const void *a, const void *b)
{
mem *ma = (mem *)a;
mem *mb = (mem *)b;
if ((unsigned)ma->p > (unsigned)mb->p)
return -1;
else if ((unsigned)ma->p < (unsigned)mb->p)
return 1;
else
return 0;
}
/* Example struct array printing function */
void print_struct_array(mem *array, size_t len)
{
size_t i;
for(i=0; i<len; i++)
printf("%-50s:\t%p\n", array[i].text, array[i].p);
}
В результате получим:
Environment variable : 0xbff52ee0
Local variable (uninitialized) : 0xbff529ac
Local variable (initialized) : 0xbff529a8
Dynamic variable : 0x871c008
Global variable (uninitialized) : 0x804a044
Static variable (in local scope, uninitialized) : 0x804a040
Static variable (in global scope, uninitialized) : 0x804a03c
Static variable (in local scope, initialized) : 0x804a034
Static variable (in global scope, initialized) : 0x804a030
Global variable (initialized) : 0x804a02c
Function (code) : 0x80484ad
Утилита size показывает размер разделов и общий размер для объектных файлов или архивов. Так, для memsegments.o получим:
$ size memsegments.o
text data bss dec hex filename
745 12 8 765 2fd memsegments.o
Объяснение:
№1
#include <iostream>
#include <string.h>
using namespace std;
int main() {
setlocale(LC_ALL, "Rus");
char st[100];
char rm[10] = " РМ";
gets_s(st);
strcat_s(st, rm);
cout << st;
return 0;
}
№2
#include <iostream>
using namespace std;
int main() {
setlocale(LC_ALL, "Rus");
string st = "получены результаты контрольной работы";
cout << sizeof(st) << endl;
cout << st.find("результаты") << endl;
st.insert(st.find("результаты") + 10, " итоговой");
cout << st;
return 0;
}