Java Pointers and Structures Overview
Learn about methods dissociation from data, global method definition, structs, pointers, and more in Java with pointers pointing at memory addresses. Explore bit structures, unions, and in-depth examples.
Java Pointers and Structures Overview
E N D
Presentation Transcript
C para Javaneses Novembro 2003 Elmar Melcher UFCG elmar@dsc.ufcg.edu.br
C = f(Java) C = Java – OO + ponteiros Resumido: • Métodos são dissociados dos dados. • Todos os métodos são definidos globalmente. • Struct é uma classe somente com atributos públicos (sem métodos, sem privacidade). • Um ponteiro é o endereço de um objeto na memória. • Um ponteiro “sabe” o tamanho do objeto.
Estruturas e ponteiros • Veja agora as transaprências de Jacobi. • Depois volte para ‘ca.
Uma struct pode conter outra struct coisa_str { int fofa; int dura; } coisa; struct alguma_str { int mole; struct coisa_str coisa; } alguma; void test() { alguma.coisa.fofa = 55; }
Uma struct pode conter um vetor de outras struct coisa_str { int fofa; int dura; } coisas[10]; struct alguma_str { int mole; struct coisa_str *ncoisa; } alguma; void test() { alguma.ncoisas = coisas; alguma.ncoisas[5].fofa = 55; }
Um apontador para uma função typedef int (*func_ptr)(); int rotina(int a) { return(a+5); } void test() { func_ptr exec = &rotina; x = (*exec)(50); // agora x comtém 55 }
Estruturas de bits struct selector { pl:2; // 2 bits menos signficativos ldt:1; // 1 bits index:13; // 13 bits mais signficativos } sel; unsigned short s; // 16 bits void test() { sel.pl = 3; sel.ldt = 0; sel.index = 0x200; s = * (unsigned short *) &sel; } Ponteiro sobre struct selector Convertido em Ponteiro sobre unsigned short dereferenciado
Union • Armazena objetos diferentes no mesmo lugar union coisa_uni { int fofa; int dura; } coisa; void test() { coisas.fofa = 55; // agora coisa.dura é 55 também ! }
Union – outro exemplo struct gate_str { unsigned int offset; unsigned short selector; unsigned short params; }; struct segment_str { unsigned int base; unsigned int limit; }; struct dt_str { word length; union { struct gate_str *g; struct segment_str *s; } base; } gdt; … gdt.base.s[200].limit = 500;