(ポインタの応用 II: メモリ操作、動的メモリ)
http://www.sw.it.aoyama.ac.jp/2017/CP1/lecture10.html
© 2005-17 Martin J. Dürst 青山学院大学
void *
and NULL
pointers09A1 | 09A2 | 09A3 | 09B1 | 09C1 | 09C2 | |
100 points | 92 | 92 | 93 | 56 | 59 | 60 |
60 points | 6 | 6 | 5 | 42 | 37 | 36 |
errors | - | - | - | - | 2 | - |
not submitted | 1 | 1 | 1 | 1 | 1 | 3 |
char *month_names[] = { ...
char month_names[][10] = { ...
int
, ...&
(address operator) obtain the address of a
variable*
(indirection operator) obtains the data pointed to by a
pointer1
, a pointer moves by the size of one item of the
type it points to*&
v ≡ v&*
p ≡ p*(
p+
i)
≡
p[
i]
≡
i[
p]
[
p]
not recommended at
all)(*
sp).
m ≡
sp->
mSimplifying &array[i]
:
→ &(array[i])
→ &(*(array+i))
→ &*(array+i)
→ (array+i)
→ array+i
int countUpper (char string[])
{
int i;
int count = 0;
int length = strlen(string);
for (i=0; i<length; i++)
if (isupper(string[i]))
count++;
return count;
}
int countUpper (char *string)
{
int count = 0;
while (*string)
if (isupper(*string++))
count++;
return count;
}
Attention: If there are no specific instructions, programs can be submitted either using array notation or pointer notation (or a mixture; please care about readability)
static
variable (declared inside a function):The memory area where each variable is allocated differs for different kinds of variables.
Typical example:
FFFFFFFF | |
Stack: Local variables, function arguments, other data necessary for function call | |
↓↓↓↓↓↓↓↓ grows downwards, shrinks upwards after usage ↑↑↑↑↑↑↑↑ | |
Empty area |
|
↑ ↑ ↑ ↑ ↑ ↑ grows upwards, usually does not shrink ↑ ↑ ↑ ↑ ↑ ↑ | |
Heap: Memory needed at arbitrary points during program execution | |
Un-initialized global variables without | |
Initialized global variables | |
The actual program (functions,...) | |
00000000 |
malloc
,...)free
)#include <stdlib.h>
int masize = 10;
int *my_array;
if (!(my_array = (int *) malloc(sizeof(int)*masize))) printf("Not enough memory!\n"), exit(1);
my_array[5] = 500;
free (my_array);
if (!( // test whether there is enough memory
my_array = // assign address of new memory region to pointer
(int *) // cast pointer to desired type
malloc( // call allocation function
sizeof(int) // size of a single element of my_array
* asize) // multiplied by the number of elements
))
printf("Not enough memory!\n") // error message
, // comma (sequence) operator
exit(1); // force end of program execution
malloc
: Allocate memory regioncalloc
: Allocate memory region and initialize with 0esrealloc
: Reallocate memory regionnew_location =
realloc(old_location、new_size);
pNew = realloc(pOld, 10);
free
: Freeing (deallocation) of memory regionrealloc
, free
can only be used with pointers to
the start of a memory regionvoid *
malloc
,... is void *
void *
is a pointer not pointing to any specific data type
(just raw memory)malloc
,... return (void*)0
when there is not
enough memoryNULL
PointerNULL
is usedNULL
produces an error (segmentation
fault,...)NULL
(null pointer), 0
(number zero),
'\0'
(null character), and "false" are different concepts, but
represented in the same way in Cmalloc
、テストファイルあり)[]
は使用禁止)realloc
、挑戦は必須、完成は発展問題、月曜日締切)