Bifalgorithm post · Computer science one · Arrays of Structures

How can an array of structure pointers make swapping structures more efficient?

How can an array of structure pointers make swapping structures more efficient? (Computer science one · Arrays of Structures) — "As an alternative, we could in…

Ideas in this flowchart

  • Utiliser un tableau de pointeurs vers des structures comme approche indirecte
  • Chaque élément du tableau contient un pointeur vers une structure
  • Le remplacement copie seulement les valeurs des pointeurs au lieu de chaque octet de la structure
  • La syntaxe pointeur-vers-pointeur est utilisée pour créer ce tableau
  • malloc réserve un tableau de dix pointeurs Student
  • Une structure Student est ensuite réservée pour chaque pointeur du tableau
  • L'opérateur flèche permet d'accéder aux membres d'une structure via son pointeur
  • Les membres id et gpa sont modifiés avec roster[0]->id et roster[0]->gpa
  • Les deux premiers pointeurs sont échangés à l'aide d'une variable temporaire
  • Les structures peuvent être stockées dans des emplacements mémoire non contigus
  • Dans cet exemple, l'échange porte sur 8 octets au lieu de 40, et l'écart augmente avec les grandes structures
  • Chaque approche possède des avantages et des inconvénients selon la situation
Feed/

How can an array of structure pointers make swapping structures more efficient?

No cover

Channel

Computer science one

1 page

"As an alternative, we could instead deal indirectly with structures by creating an array of pointers to structures. Swapping elements then involves only copying pointer values rather than every byte that makes up the structure. To do this, we use the familiar pointer-to-pointer syntax.\n\nStudent **roster = (Student **) malloc(sizeof(Student *) * 10);\n\nfor(i=0; i<10; i++) {\n\nroster[i] = (Student *) malloc(sizeof(Student) * 1);\n\n}\n\n//access each as pointers and use the arrow operator\n\nroster[0]->id = 87654321;\n\nroster[0]->gpa = 4.0;\n\n//swap the first two *pointers*:\n\nStudent *temp = roster[0];\n\nroster[0] = roster[1];\n\nroster[1] = temp;\n\nAs in the example above, if each element in the array is a pointer to a structure, then we use the arrow operator to access each member variable. The differences between these two approaches is illustrated in Figures 23.1 and 23.2. In contrast, using pointers to structures means that structures may be stored non-contiguously in different memory locations. Swapping two structures is done indirectly by swapping pointers, only 8 bytes. The difference in this particular example is not that great, 8 vs. 40 bytes, but with larger structures it can become an issue. Each approach has its own advantages and disadvantages and one may be more appropriate than the other in different situations. Figure 23.2 illustrates an array of structure pointers in which each record is a pointer that refers to a structure that may be stored non-contiguously in completely different memory locations."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.