Tableau a 2 dimensions (win32)

Description

Juste une demo suite a discussion sur forum.
Le probleme etait sur l'allocation de memoire.
On proposait ceci, valide mais lent:
int **tab;
tab = (int**)malloc(nblignes*sizeof(int*));
for(int i=0; i!=nbcolonnes;i++)
tab[i] = (int*)malloc(nbcolonnes*sizeof(int));
Je propose donc:
1 seule alloc, 1 seule desalloc.
Prog complet avec mesures en ticks processeur.
Constatez vous memes les ecarts.

Source / Exemple :


#include <windows.h>
#include "resource.h"
#include "bnlib2c.h"
#include "bnPrecis.h"

#define NCOLS  100
#define NLINS  40

//  index de chaque elem du tableau a 2 dimensions
//  col     0       1        2
// lin
//  0:      0       1        2
//  1:      3       4        5
//  2:      6       7        8
//  3:      9       10      11

// index se trouve donc par: NCOLS*lin+col

HWND heda, hedb;
DWORD bdisp = 0;
char szappname[] = "TABLEAU";

void TesteUnicMalloc()
{
  unsigned __int64 ticks;
  DWORD *ptab, col, lin; // simuler ecriture tab[3][4]
  char szbuff[120], *c;
  DWORD val = 10;
  // 1 SEULE ALLOCATION !!!
  bnTicksStart();
  ptab = (DWORD*) malloc(NLINS * NCOLS * sizeof(int));
  ticks = bnTicksResult();
  //if(!ptab) return;
  // pour demo, on remplit afin d'obtenir
  // 10	11	12
  // 20	21	22
  // 30	31	32
  // 40	41	42
  for(lin = 0; lin < NLINS; lin++) {
    for(col = 0; col < NCOLS; col++) ptab[NCOLS*lin+col] = val + col;
    val += 10;
  }
  // ON RELIT petite partie du TABLEAU AVEC PASSAGE EN ASCII
  c = szbuff;
  for(lin = 0; lin < 4; lin++) {
    for(col = 0; col < 3; col++) {
      c = bnultoa(ptab[NCOLS*lin+col], c);

  • c++ = '\t';
}
  • (c-1) = 10;
}
  • c = 0;
// AFFICHAGE POUR VERIFICATION if(bdisp) MessageBox(heda, szbuff, szappname, 0); bnTicksStart(); free(ptab); // 1 SEULE LIBERATION, BENEF REBELOTE !!! ticks += bnTicksResult(); _ui64toa(ticks, szbuff, 10); if(bdisp) SetWindowText(heda, szbuff); } void TesteMultiMalloc() { unsigned __int64 ticks; DWORD **ptab, col, lin; char szbuff[120], *c; DWORD val = 10; bnTicksStart(); ptab = (DWORD**) malloc(NLINS * sizeof(int*)); for(col=0;col<NLINS;col++) ptab[col] = (DWORD*)malloc(NCOLS*sizeof(int)); ticks = bnTicksResult(); //if(!ptab) return; // pour demo, on remplit afin d'obtenir // 10 11 12 // 20 21 22 // 30 31 32 // 40 41 42 for(lin = 0; lin < NLINS; lin++) { for(col = 0; col < NCOLS; col++) ptab[lin][col] = val + col; val += 10; } // ON RELIT petite partie du TABLEAU AVEC PASSAGE EN ASCII c = szbuff; for(lin = 0; lin < 4; lin++) { for(col = 0; col < 3; col++) { c = bnultoa(ptab[lin][col], c);
  • c++ = '\t';
}
  • (c-1) = 10;
}
  • c = 0;
// AFFICHAGE POUR VERIFICATION if(bdisp) MessageBox(heda, szbuff, szappname, 0); bnTicksStart(); for(col=0;col<NLINS;col++) free(ptab[col]); free(ptab); ticks += bnTicksResult(); _ui64toa(ticks, szbuff, 10); if(bdisp) SetWindowText(hedb, szbuff); } BOOL CALLBACK AppDlgProc(HWND hdlg, UINT mssg, WPARAM wParam, LPARAM lParam) { switch(mssg) { case WM_INITDIALOG: SetClassLong(hdlg, GCL_HICON, (long)LoadIcon(0, IDI_APPLICATION)); heda = GetDlgItem(hdlg, IDED_A); hedb = GetDlgItem(hdlg, IDED_B); TesteUnicMalloc(); // CHARGE LES FONCTIONS EN MEMOIRE TesteMultiMalloc(); // AINSI MESURES TICKS SERONT SIGNIFICATIVES bdisp = 1; return 1; case WM_COMMAND: switch(wParam) { case IDBT_A: SetWindowText(heda, 0); TesteUnicMalloc(); PostMessage(hdlg, WM_NEXTDLGCTL, (long) heda, 1); return 0; case IDBT_B: SetWindowText(hedb, 0); TesteMultiMalloc(); PostMessage(hdlg, WM_NEXTDLGCTL, (long) hedb, 1); return 0; case IDCANCEL: EndDialog(hdlg, 0); } } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, int) { DialogBoxParam(hInstance, (LPCTSTR)IDD_APP, 0, AppDlgProc, 0); return 0; }

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.