Код программы:
#include <iostream>
#include <locale>
#include <time.h>
using namespace std;
void rand_mas(int *mas, int n) {
srand(time(NULL));
int a = -30, b = 30;
for (int i = 0; i < n; i++)
mas[i] = a + rand() % (b - a);
}
int min_mas(int *mas, int n) {
int min = mas[0];
for (int i = 1; i < n; i++) {
if (min > mas[i])
min = mas[i];
return min;
int main()
{
setlocale(LC_ALL, "Russian");
int *A, N, min, *temp;
cout << "Введите количество элементов в массиве" << endl;
cin >> N;
A = new int[N];
rand_mas(A, N);
cout << "Исходный массив: " << endl;
for (int i = 0; i < N; i++)
cout << A[i] << ' ';
min = min_mas(A, N);
temp = A;
A = new int[N + 1];
A[0] = min;
for (int i = 1, j = 0; i < N; i++) {
if (temp[j] == min) {
++j;
A[i] = temp[j];
continue;
cout << "\nИзмененный массив: " << endl;
return 0;
/* 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);
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
Объяснение:
Код программы:
#include <iostream>
#include <locale>
#include <time.h>
using namespace std;
void rand_mas(int *mas, int n) {
srand(time(NULL));
int a = -30, b = 30;
for (int i = 0; i < n; i++)
mas[i] = a + rand() % (b - a);
}
int min_mas(int *mas, int n) {
int min = mas[0];
for (int i = 1; i < n; i++) {
if (min > mas[i])
min = mas[i];
}
return min;
}
int main()
{
setlocale(LC_ALL, "Russian");
int *A, N, min, *temp;
cout << "Введите количество элементов в массиве" << endl;
cin >> N;
A = new int[N];
rand_mas(A, N);
cout << "Исходный массив: " << endl;
for (int i = 0; i < N; i++)
cout << A[i] << ' ';
min = min_mas(A, N);
temp = A;
A = new int[N + 1];
A[0] = min;
for (int i = 1, j = 0; i < N; i++) {
if (temp[j] == min) {
++j;
A[i] = temp[j];
++j;
continue;
}
A[i] = temp[j];
++j;
}
cout << "\nИзмененный массив: " << endl;
for (int i = 0; i < N; i++)
cout << A[i] << ' ';
return 0;
}
/* 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
Объяснение: