問題在標題中
void deleteLastValue(dynIntArray* t){ //function should responds to unexpected values
if(t->tab == NULL){ //if the array is empty we should return an error
fprintf(stderr,"The array is empty");
exit(1);
}
t->tab[(t->size)-1] = 0; /**I'm not sure what to do here should I free the pointer using free function**/
//t->tab[(t->size)-1] = NULL; is it necessary ?
//free(t->tab[(t->size)-1]);
t->tab = (int*)realloc(t->tab,((t->size)-1)*sizeof(int)); //we reallocate but with the new size
t->size--;//size - 1; but capacity stills the same
}
typedef struct{ //structure of an dynamic array
int * tab; //an array of
unsigned int capacity; // the space in memory
unsigned int size;// size of array
} dynIntArray;
如果您可以查看我的代碼,我對如何處理陣列的最后一個元素有疑問。如果我想讓程式回應意外的輸入以使其正常作業,我應該做更多的事情嗎?謝謝你。你太棒了!
uj5u.com熱心網友回復:
就像您realloc每次一樣,您只需要陣列的大小。您還可以使用靈活的陣列來簡化分配和解除分配。
typedef struct{ //structure of an dynamic array
unsigned int size;// size of array
int tab[]; //an array of
} dynIntArray;
dynIntArray *deleteLastValue(dynIntArray* t)
{
if(t && t -> size)
{
t = realloc(sizeof(*t) (t -> size - 1) * sizeof(t -> tab[0]));
if(t) t -> size--;
}
return t;
}
uj5u.com熱心網友回復:
Apparenty它不是很清楚你如何capacity和size有關系。
realloc可能是一個相當昂貴的功能,可能需要相當長的時間。因此,如果realloc每次更改大小時都呼叫,您的代碼將可以作業,但可能會很慢。因此capacity使用 。
大致是這樣完成的:
- 您使用 a
size= 0 和capacity= 100(或一些大于 0 的適當值)初始化動態陣列。 - 現在,您可以將 100 個元素添加到陣列中而
realloc不會被呼叫。 - 如果再添加一個元素,則添加 100
capacity并呼叫realloc - 現在容量為 200,您可以在不
realloc呼叫的情況下再添加 100 個元素。
如果你想降低動態陣列的大小,你就可以減少size,而無需呼叫realloc留下capacity一個人。如果之間的差別size,并capacity變得比一些大的值或者也許如果capacity比大兩倍size,那么你就可以減少容量和通話realloc
有幾種關于如何size和如何capacity相關以及何時增加/減少的策略capacity。
void deleteLastValue(dynIntArray* t) {
if (t->tab == NULL) {
fprintf(stderr, "The array is empty");
exit(1);
}
t->size--; // that's all you need to do
// now optionally you might want to decrease the capacity
// if the capacity is larger than twice the size
// or based on some other strategy
if (t->capacity > 2 * t->size)
{
t->capacity = t->size;
t->tab = realloc(t->tab, t->capacity);
if (t->tab == NULL) {
fprintf(stderr, "realloc error in deleteLastValue");
exit(1);
}
}
}
請注意,您仍然需要檢查陣列是否為空(IOW 如果size為 0。我將此留作練習。
如果您想了解更多資訊,您應該向我們展示其他功能。
回顧你的功能:
void deleteLastValue(dynIntArray* t) {
if (t->tab == NULL) {
fprintf(stderr, "The array is empty");
exit(1);
}
t->tab[(t->size) - 1] = 0; // this is useless, the element is no longer part of the array
// you don't need to overwrite it with 0
// the call to free below is completly wrong, t->tab[(t->size) - 1] is not a pointer but an int
// free(t->tab[(t->size) - 1]);
// this reallocation is correct (except there is no error checking and the (int*) cast is useless)
t->tab = (int*)realloc(t->tab, ((t->size) - 1) * sizeof(int));
t->size--;
// you leave the capacity alone which is maybe what you want
}
uj5u.com熱心網友回復:
謝謝大家!它作業得很好。我在這里為后代提交答案。(為了練習,我們不需要改變容量)
void deleteLastValue(dynIntArray* t){
if(t->tab == NULL || t->size == 0){
fprintf(stderr,"The array is empty");
exit(1);
}
t->size--;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/324789.html
標籤:C
上一篇:如何僅保存我需要的某些位元組而不是全部保存在陣列中?
下一篇:強制最多一次包含在嵌入式C中
