簡體   English   中英

c中的可變數組大小

[英]Variable array size in c

我試圖聲明由用戶輸入給定的可變大小的數組。

到目前為止,我有這樣的事情:

typedef struct _object{   
    int rowsAmount;  
    int columsAmount;
    int* rows;
    int* colums;
} object;

object* newObject(int ra, int ca){
    object* o = malloc(sizeof(object));
    o->rowsAmount = ra;
    o->columsAmount = ca;
    o->rows = [ra];    
    o->colums = [ca];
    return o;
}

int main(){
    newObject(3,4);
}

我以為這是行不通的,但是我想要這樣的東西,而且我也不知道該怎么做。

看起來您基本上是在這里實現動態Matrix對象。 您想要類似的東西:

typedef struct _object{   
    int rowsAmount;  
    int columsAmount;
    int* matrix;
    int** rows;
} object;

object* newObject(int ra, int ca){
    object* o = malloc(sizeof(object));
    o->rowsAmount = ra;
    o->columsAmount = ca;
    o->matrix = malloc(ra * ca * sizeof(int));
    o->rows = malloc(ra * sizeof(int*));
    for (size_t i = 0; i != ra; ++i) o->rows[i] = o->matrix + (i * ca);
    return o;
}

您還應該創建一個析構函數destroyObject ,該函數類似地free分配給oo->matrix的所有內存。

編輯

但是,您的評論是:

“我只是想學習c,這僅與設置大小有關。我只是碰巧嘗試了2個數組”

...使這個問題有些令人困惑,因為它表明盡管您在此處使用了“行” /“列”術語,但實際上您並未嘗試創建矩陣(2D數組),但是您只是想了解如何在C中動態分配數組。

在這種情況下,將使用指針變量和malloc動態分配C中的數組:

size_t array_size = 10; /* can be provided by user input */
int* array = malloc(sizeof(int) * array_size);

然后,一旦使用完動態分配的數組,就必須釋放它:

free(array);

要在C中動態分配二維數據數組,請執行以下操作:

  • 為整個數據分配內存。 該內存由arrayData
  • 為每行分配一個1D指針數組
  • 將這些指針指向每一行對應的內存地址

碼:

int *arrayData = malloc(sizeof(int) * rows * columns);
int **array = malloc(sizeof(int*) * rows);
for(int i=0; i < rows;++i){
   array[i] = arrayData + i * columns;
}

您現在可以以array[row][col]訪問內存。

您可以使用用戶輸入的大小來創建數組而無需結構。

int *array1;
int size;
// get input from user
array1 = malloc(sizeof(int)*size);
// do your stuff
free(array1);

如果您想要2D陣列,

int **array2;
int row, col;
int i;
array2 = malloc(sizeof(int*)*row);
for(i=0;i<row;++i)
    array2[i] = malloc(sizeof(int)*col);
//use the array
for(i=0;i<row;++i)
    free(array2[i]);
free(array2);

如果您確實需要結構數組,則在newObject()函數中為其分配內存

typedef struct _object{   
    int rowsAmount;  
    int columsAmount;
    int** array;
    //int* colums;
} object;

object* newObject(int ra, int ca){
    int i;
    object* o = malloc(sizeof(object));
    o->rowsAmount = ra;
    o->columsAmount = ca;
    o->array = malloc(sizeof(int*)*ra);    
    for(i=0;i<ra;i++)
        o-<array[i]=malloc(sizeof(int)*ca);
    return o;
}

int main(){
    newObject(3,4);
}

我認為人們經常在可以使用范圍變量的情況下使用動態內存分配。 例如,根據用戶輸入大小的數組可以在不使用malloc / free的情況下分配到堆棧上:

int array_size;
scanf("%d", &array_size);
if (array_size > 0) {
    /* Allocate array on stack */
    float array[array_size];

    /* ... do smth with array ... */
}
/* Out of scope, no need to free array */

當然,如果您的數據塊很大,則必須有堆內存,但是對於較小的分配范圍就可以了。

最簡單的方法是使用boost :: multi_array不僅會得到任意數量的維,而且還非常有效地存儲為單個連續的內存塊,而不是n維數組。

CPU的設計目的是快速遍歷數組,您可以使用此功能潛在地利用編譯器的緩存/預取/流水線功能。

例如

// 2 dimensions
int xDim;
int yDim;
cin >> xDim; // From user..
cin >> yDim;
// Initialise array
boost::multi_array<int,2> my2dgrid(boost::extents[xDim][yDim]);
// Iterate through rows/colums
for(int j = 0 ; j < yDim-1; j++) { // Row traversal
for(int i = 0 ; i < xDim-1; i++) { // Column traversal
   int value = grid[j][i]; // Get a value
   grid[j][i] = 123; // set a value
   // Do something...
}
#include <stdio.h>
#include <stdlib.h>

typedef struct _object{   
    int rowsAmount;  
    int columsAmount;
    int **rows;
//  int* colums;
} object;

object* newObject(int ra, int ca){
    int r;
    object* o = malloc(sizeof(object));
    o->rowsAmount = ra;
    o->columsAmount = ca;
    o->rows = (int **)malloc(ra*sizeof(int *));
    for(r=0;r<ra;++r)
        o->rows[r] = (int*)malloc(ca*sizeof(int));
    return o;
}

int main(){
    object *obj= newObject(3,4);
    obj->rows[2][3]=5;
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM