How do arrays work with C pointers?

-1

I was recently given a code in C language to continue developing a new programming language and I found some instructions that caught my attention on how to declare an array and use it, but that I had seen them before. Attached an example of the situation in code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    int *lista = (int *)malloc(sizeof(int *));

    lista[0] = 1;
    lista[1] = 2;
    lista[2] = 3;
    printf("%d, %d, %d", lista[0], lista[1], lista[2]);

    return 0;
}

Nose if someone could explain to me how the arrays work in this way with pointers since I can not imagine it, because supposedly the list variable contains the memory address where an integer is stored, but because I can save a list there ?.

    
asked by FDarfe 20.01.2018 в 06:46
source

1 answer

1
int *lista = (int *)malloc(sizeof(int *));

This is a perfect example of something that should not be done in C.

That line reserves space for a single pointer of type int . the problem? That would have to reserve space for int ... but ... why? Basically because in 32 bit architectures, both int and int* occupy 4 bytes ... but in 64 bits int takes 4 bytes while int* will occupy 8 ... you are not reserving the space you need but another different ... if you reserve more memory waste, while if you reserve less or tread memory or the OS kills your application.

But if the reservation is already problematic, its use is no less harmful:

lista[0] = 1;
lista[1] = 2;
lista[2] = 3;

As we have seen before it was reserving space for one or two int ... although the idea was to reserve for only one element ... and yet we are treating the reservation as if it had space for 3 elements ... Do you see the problem? The reservation is too small and we have seen before what can happen:

  • Memory floors
  • The OS kills your application to protect the integrity of memory.

The reservation should have been like this:

int *lista = (int *)malloc(3*sizeof(int));

That is, we tell malloc to provide us with memory to store 3 int .

  

I do not know if someone could explain to me how the arrays work in this way

A program uses two different memories: stack and heap . There are really more regions but those are already dependent on hardware and software architecture and will not always be available, so I will not mention them here.

The stack is the stack. The resources stored here are destroyed automatically when they are no longer needed ... this memory zone is the one that you learn to use before:

int main ()
{
  int variableEnStack = 8;
}

This memory area has serious space restrictions, so it is not suitable for storing large collections of data because if the program is full, it will stop working.

The heap is the region of dynamic memory. The management of this memory falls on the programmer. This memory will be as big as the memory available on the computer. This memory is always accessed by pointers:

int main ()
{
  int* punteroEnHeap = (int*)malloc(100*sizeof (int));

  free(punteroEnHeap);
}
    
answered by 21.01.2018 / 19:00
source