在C语言中创建动态可扩展的内存数组
在下面的代码中,我试图创建一个动态可扩展的内存数组在C语言中创建动态可扩展的内存数组,c,arrays,pointers,C,Arrays,Pointers,在下面的代码中,我试图创建一个动态可扩展的内存数组 #include <stdio.h> #include <stdlib.h> #define BLOCKSIZE 5 int hash_table_length = 0; int *currentblock = NULL; int size_left; int *hash_table = NULL; int *start = NULL; int *create_hash_table() { int *tm
#include <stdio.h>
#include <stdlib.h>
#define BLOCKSIZE 5
int hash_table_length = 0;
int *currentblock = NULL;
int size_left;
int *hash_table = NULL;
int *start = NULL;
int *create_hash_table() {
int *tmp;
if (currentblock == NULL || size_left == 0) {
if (currentblock == NULL) {
currentblock = (int *) malloc( BLOCKSIZE * sizeof(int));
start = currentblock;
size_left = BLOCKSIZE;
} else {
currentblock = (int *) malloc( BLOCKSIZE * sizeof(int));
size_left = BLOCKSIZE;
}
}
tmp = currentblock++;
size_left -= 1;
return tmp;
}
void build() {
int hash;
int i = 0;
for (i = 0; i < 20; i++) {
hash = i + 3;
if (hash_table_length == 0) {
hash_table = create_hash_table();
hash_table_length++;
} else {
hash_table = create_hash_table();
hash_table_length++;
}
hash_table = &hash;
printf("hash value is %d\n", *hash_table);
}
}
int main() {
build();
// How do I reach the start of the hash table again?
// the below start does not give me the first value
printf("Hash table first value is %d\n", *start);
return 0;
}
#包括
#包括
#定义块大小5
int hash_table_length=0;
int*currentblock=NULL;
int size_左;
int*hash_table=NULL;
int*start=NULL;
int*创建哈希表(){
int*tmp;
如果(currentblock==NULL | | size|u left==0){
如果(currentblock==NULL){
currentblock=(int*)malloc(BLOCKSIZE*sizeof(int));
开始=当前块;
size_left=块大小;
}否则{
currentblock=(int*)malloc(BLOCKSIZE*sizeof(int));
size_left=块大小;
}
}
tmp=电流块++;
尺寸_左-=1;
返回tmp;
}
void build(){
整数散列;
int i=0;
对于(i=0;i<20;i++){
hash=i+3;
if(哈希表长度==0){
hash_table=创建_hash_table();
哈希表长度++;
}否则{
hash_table=创建_hash_table();
哈希表长度++;
}
哈希表=&hash;
printf(“哈希值为%d\n”,*哈希表);
}
}
int main(){
build();
//如何再次到达哈希表的开头?
//下面的开始并没有给出第一个值
printf(“哈希表的第一个值是%d\n”,*start);
返回0;
}
这里的问题是我希望遍历存储在哈希表中的值。我无法访问哈希表的第一个元素/地址。我希望打印出哈希表中存储的所有值。如何做到这一点?在您的代码中,哈希值永远不会存储在哈希表中(在
currentblock
中)。在create\u hash\u table()
函数中,为新块分配内存,但从不在此块中存储值。因此,如果您尝试取消引用这些int*
位置中的任何一个,您可能会得到一个垃圾值(可能是0)。
这正是当您取消引用start
指针时,main()函数内部发生的情况。它实际上指向哈希表的开头,由于该位置未初始化,因此它的输出为0。
要在哈希表中实际存储值,请在build()中更改以下内容:
致:
现在,如果您尝试运行代码,它将输出3
进入问题的第二部分,关于如何遍历整个哈希表:不能使用此代码。这是因为在malloc'd整数块之间没有链接。malloc()调用可以从堆中分配任意可用内存块。因此,在当前形式中,您已断开无法遍历的位置块
您可以使用malloc来增加当前块的大小,而不是malloc。为较大的块分配内存,并将以前的数据复制到此新块。这基本上允许您使用start
遍历整个哈希表
以下是您可以如何做到这一点:
#include <stdio.h>
#include <stdlib.h>
#define BLOCKSIZE 5
int hash_table_length = 0;
int *currentblock = NULL;
int size_left;
int *hash_table = NULL;
int *start = NULL;
int *create_hash_table() {
int *tmp;
if (currentblock == NULL || size_left == 0) {
if (currentblock == NULL) {
currentblock = (int *) malloc(BLOCKSIZE * sizeof(int));
start = currentblock;
size_left = BLOCKSIZE;
} else {
/* Call realloc() to allocate new memory block of size (hash_table_length+BLOCKSIZE) and copy previous data*/
currentblock = ((int *) realloc(start,(hash_table_length + BLOCKSIZE) * sizeof(int))) + hash_table_length;
size_left = BLOCKSIZE;
}
}
tmp = currentblock++;
size_left -= 1;
return tmp;
}
void build() {
int hash;
int i = 0;
for (i = 0; i < 20; i++) {
hash = i + 3;
if (hash_table_length == 0) {
hash_table = create_hash_table();
hash_table_length++;
} else {
hash_table = create_hash_table();
hash_table_length++;
}
/* Store value of hash inside the hash_table */
*hash_table = hash;
printf("hash value is %d\n", *hash_table);
}
}
int main() {
int i;
build();
printf("Hash table first value is %d\n", *start);
/* Traverse the hash table */
for(i = 0; i < hash_table_length; ++i)
printf("hash_table[%d] = %d\n",i,*start++);
return 0;
}
#包括
#包括
#定义块大小5
int hash_table_length=0;
int*currentblock=NULL;
int size_左;
int*hash_table=NULL;
int*start=NULL;
int*创建哈希表(){
int*tmp;
如果(currentblock==NULL | | size|u left==0){
如果(currentblock==NULL){
currentblock=(int*)malloc(BLOCKSIZE*sizeof(int));
开始=当前块;
size_left=块大小;
}否则{
/*调用realloc()分配大小为(哈希表长度+块大小)的新内存块并复制以前的数据*/
currentblock=((int*)realloc(开始,(哈希表长度+块大小)*sizeof(int))+哈希表长度;
size_left=块大小;
}
}
tmp=电流块++;
尺寸_左-=1;
返回tmp;
}
void build(){
整数散列;
int i=0;
对于(i=0;i<20;i++){
hash=i+3;
if(哈希表长度==0){
hash_table=创建_hash_table();
哈希表长度++;
}否则{
hash_table=创建_hash_table();
哈希表长度++;
}
/*将哈希值存储在哈希表中*/
*哈希表=哈希;
printf(“哈希值为%d\n”,*哈希表);
}
}
int main(){
int i;
build();
printf(“哈希表的第一个值是%d\n”,*start);
/*遍历哈希表*/
对于(i=0;i
如果您不一定需要构建自己的动态阵列,您可以查看。。。
*hash_table = hash; // Store value of 'hash' inside the memory location pointed to by hash table(which happens to be 'current_block' inside build())
#include <stdio.h>
#include <stdlib.h>
#define BLOCKSIZE 5
int hash_table_length = 0;
int *currentblock = NULL;
int size_left;
int *hash_table = NULL;
int *start = NULL;
int *create_hash_table() {
int *tmp;
if (currentblock == NULL || size_left == 0) {
if (currentblock == NULL) {
currentblock = (int *) malloc(BLOCKSIZE * sizeof(int));
start = currentblock;
size_left = BLOCKSIZE;
} else {
/* Call realloc() to allocate new memory block of size (hash_table_length+BLOCKSIZE) and copy previous data*/
currentblock = ((int *) realloc(start,(hash_table_length + BLOCKSIZE) * sizeof(int))) + hash_table_length;
size_left = BLOCKSIZE;
}
}
tmp = currentblock++;
size_left -= 1;
return tmp;
}
void build() {
int hash;
int i = 0;
for (i = 0; i < 20; i++) {
hash = i + 3;
if (hash_table_length == 0) {
hash_table = create_hash_table();
hash_table_length++;
} else {
hash_table = create_hash_table();
hash_table_length++;
}
/* Store value of hash inside the hash_table */
*hash_table = hash;
printf("hash value is %d\n", *hash_table);
}
}
int main() {
int i;
build();
printf("Hash table first value is %d\n", *start);
/* Traverse the hash table */
for(i = 0; i < hash_table_length; ++i)
printf("hash_table[%d] = %d\n",i,*start++);
return 0;
}