• Jump To … +
    array_of_routines.c array_utils.h print.c arrays_and_pointers.c arrays_basics.c command_line.c fibonacci.c hello_world.c hello_world_correct.c linked_list.c malloc_stuff.c nans_and_other_oddities.c rationals_with_structs.c routine_pointers.c experiments.c naive_sequence_of_longs.c naive_sequence_of_longs.h sequence_of_longs.c sequence_of_longs.h tests.c sizeof_and_arrays.c array_of_doubles.c array_of_doubles.h perform_experiments.c sorting_algorithms.c sorting_algorithms.h string_io.c
  • ¶

    malloc_stuff.c – Memória dinâmica

  • ¶
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	int array_size = 3;
    	double *grades = malloc(array_size * sizeof(double));
    	*grades = 11.5;
    	*(grades + 1) = 33.5;
    
  • ¶

    x[y] => (x + y) (grades + 2) = -1;

    	grades[2] = -1;
    
    	printf("%p\n", grades);
    
    	for (int i = 0; i != array_size; i++)
    		printf(" %g", grades[i]);
    	putchar('\n');
    
  • ¶

    printf("%g\n", grades); printf("%g\n", (grades + 1)); printf("%g\n", grades[2]);

    	grades = realloc(grades, ++array_size * sizeof(double));
    
    	grades[3] = 101.1;
    
    	for (int i = 0; i != array_size; i++)
    		printf(" %g", grades[i]);
    	putchar('\n');
    
    	double *new_grades = malloc((array_size + 1) * sizeof(double));
    
    	for (int i = 0; i != array_size; i++)
    		new_grades[i] = grades[i];
    
    	array_size++;
    
    	free(grades);
    
    	grades = new_grades;
    
    	for (int i = 0; i != array_size; i++)
    		printf(" %g", grades[i]);
    	putchar('\n');
    
    	free(grades);
    
    	return EXIT_SUCCESS;
    }