Oceans11
04-02-2014, 14:59
Per esercizio, sto tentando di implementare le varie funzioni di string.h.
Ho dei problemi con memmove.
Partiamo da memcpy, caso particolare di memmove in cui i puntatori passati come parametro non puntano ad oggetti in memoria che si sovrappongono:
void *memcpy(void *dest, const void *src, size_t n) {
unsigned char *d = dest;
const unsigned char *s = src;
while (n--) {
*d++ = *s++;
}
return dest;
}
codice non testato, anche se sembra abbastanza semplice. Qui l'unico dubbio è nell'uso di "unsigned": io l'ho usato, ma non saprei dire se va bene, se ci vuole "signed" e quali sono (se ci sono) differenze.
Memmove, invece, dovrebbe coprire anche il caso in cui gli oggetti sorgente e destinazione overlappano.
Dalla pagina di manuale:The memmove() function copies n bytes from memory area src to memory
area dest. The memory areas may overlap: copying takes place as
though the bytes in src are first copied into a temporary array that
does not overlap src or dest, and the bytes are then copied from the
temporary array to dest.
void *memmove(void *dest, const void *src, size_t n) {
// dest < src, then copy from the beginning
return memcpy(dest, src, n);
}
// otherwise copy from the end
unsigned char *d = dest;
const unsigned char *s = src;
s += n;
d += n;
while (n--) {
*--d = *--s;
}
return dest;
}
Leggendo in rete, trovo un riferimento ad un paragrafo dello standard (in realtà è un documento che si avvicina allo standard ISO/IEC 9899:TC2 WG14/N1124) che riporto per intero:
6.5.8.5 When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object or incomplete types both point to the same object, or both point one past the last element of the same array object, they compare equal. If the objects pointed to are members of the same aggregate object, pointers to structure members declared later compare greater than pointers to members declared earlier in the structure, and pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values. All pointers to members of the same union object compare equal. If the expression P points to an element of an array object and the expression Q points to the last element of the same array object, the pointer expression Q+1 compares greater than P. In all other cases, the behavior is undefined.
e qui nascono i problemi, per tutti i casi non citati (ossia oltre a puntatori allo stesso array, struct o union)
1) cosa si intende per "object"?
2) cosa significa "relative locations in the address space"
3) perchè non posso confrontare i 2 puntatori come 2 interi per verificare se le regioni si sovrappongono?
Ho dei problemi con memmove.
Partiamo da memcpy, caso particolare di memmove in cui i puntatori passati come parametro non puntano ad oggetti in memoria che si sovrappongono:
void *memcpy(void *dest, const void *src, size_t n) {
unsigned char *d = dest;
const unsigned char *s = src;
while (n--) {
*d++ = *s++;
}
return dest;
}
codice non testato, anche se sembra abbastanza semplice. Qui l'unico dubbio è nell'uso di "unsigned": io l'ho usato, ma non saprei dire se va bene, se ci vuole "signed" e quali sono (se ci sono) differenze.
Memmove, invece, dovrebbe coprire anche il caso in cui gli oggetti sorgente e destinazione overlappano.
Dalla pagina di manuale:The memmove() function copies n bytes from memory area src to memory
area dest. The memory areas may overlap: copying takes place as
though the bytes in src are first copied into a temporary array that
does not overlap src or dest, and the bytes are then copied from the
temporary array to dest.
void *memmove(void *dest, const void *src, size_t n) {
// dest < src, then copy from the beginning
return memcpy(dest, src, n);
}
// otherwise copy from the end
unsigned char *d = dest;
const unsigned char *s = src;
s += n;
d += n;
while (n--) {
*--d = *--s;
}
return dest;
}
Leggendo in rete, trovo un riferimento ad un paragrafo dello standard (in realtà è un documento che si avvicina allo standard ISO/IEC 9899:TC2 WG14/N1124) che riporto per intero:
6.5.8.5 When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object or incomplete types both point to the same object, or both point one past the last element of the same array object, they compare equal. If the objects pointed to are members of the same aggregate object, pointers to structure members declared later compare greater than pointers to members declared earlier in the structure, and pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values. All pointers to members of the same union object compare equal. If the expression P points to an element of an array object and the expression Q points to the last element of the same array object, the pointer expression Q+1 compares greater than P. In all other cases, the behavior is undefined.
e qui nascono i problemi, per tutti i casi non citati (ossia oltre a puntatori allo stesso array, struct o union)
1) cosa si intende per "object"?
2) cosa significa "relative locations in the address space"
3) perchè non posso confrontare i 2 puntatori come 2 interi per verificare se le regioni si sovrappongono?