访问dynamiclly分配的数组元素 [英] accessing elements of dynamiclly allocated array
问题描述
我试图通过添加占位符阵列的大小与第一元件访问下一个元件在动态分配的。占位符阵大小为4个字节,所以如果我加4个字节把它与地址2147549788第一要素,我应该看一个存储位置2147549792.但是,相反,我在16个字节2147549804.如果我看了看地址数组元素的地址,我直接得到正确的地址。想知道为什么加入占位符的大小给予不同的结果?
另外,如果我访问外部的循环第二个元素的位置,该位置在另一个16字节即2147549820
的#includestdio.h中
的#includetime.h中
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;typedef结构{节
INT numOfStudents;
} 部分;typedef结构{学校
INT schoolId;
节段[1]; //占位符
} 学校;INT主要(无效)
{
学校* schoolA_p;
第SECTION1;
INT sizeOffset = 0;
INT I,sectionSize; INT sizeOfSchool = sizeof的(学校) - 的sizeof(段)+(的sizeof(节)* 2);
schoolA_p =(结构中学*)malloc的(sizeOfSchool); 对于(I = 0; I&2;我+ +)
{
sectionSize = sizeof的(部分); 的printf(节\\ n大小=%d个\\ N的sizeof(节));
输出(字节数添加到第一元件以访问下一个元素=%d个\\ N,sizeOffset);
的printf(在schoolA_p-的制造&gt环位置;部分内容[%d] =为0x%LX \\ n,我,及(schoolA_p->部分[0])+ sizeOffset); sizeOffset + = sectionSize;
} 输出(schoolA_p-的制造&gt \\ n位置;节[0] =为0x%LX \\ n,及(schoolA_p->部分[0]));
输出(schoolA_p-的制造&gt \\ n位置;部分[1] =为0x%LX \\ n,及(schoolA_p->部分[1])); 的printf(\\ n schoolA_p-的制造&gt lopop位置之外;部分[0] =为0x%LX \\ n,及(schoolA_p->部分[0])+ sizeOffset); 免费(schoolA_p);
返回0;
}
指针算法是这样的:如果 P
是一个指向数组元素 A [N]
,那么 p + I
是一个指向 A [N + I]
。换句话说,指针运算在指针被声明为,不是字节任何基本类型的单元移动。
I'm trying to access the next element in dynamically allocated by adding the size of placeholder array to the first element. Size of placeholder array is 4 bytes, so if I add 4 bytes to it first element with address 2147549788, I should be looking at next memory location 2147549792. But, instead I'm looking at an address in 16 bytes 2147549804. If I read the address of array elements directly I get the right address. Wondering why adding size of placeholder gives different results ? Also, if I access the location of second element outside the for loop, the location is off by another 16 bytes i.e 2147549820
#include "stdio.h"
#include "time.h"
#include <stdlib.h>
#include <string.h>
typedef struct Section {
int numOfStudents;
} Section;
typedef struct School {
int schoolId;
Section sections[1]; //placeholder
} School;
int main(void)
{
School *schoolA_p;
Section section1;
int sizeOffset = 0;
int i,sectionSize;
int sizeOfSchool = sizeof(School) - sizeof(Section) + (sizeof(Section)*2);
schoolA_p = (struct School *) malloc(sizeOfSchool);
for(i=0;i<2;i++)
{
sectionSize = sizeof(Section);
printf("\nsize of section = %d\n", sizeof(Section));
printf("Number of bytes to add to first element to access the next element = %d\n", sizeOffset);
printf("In Loop Location of schoolA_p->sections[%d]= 0x%lx\n",i, &(schoolA_p->sections[0]) + sizeOffset);
sizeOffset += sectionSize;
}
printf("\n Location of schoolA_p->sections[0]= 0x%lx\n",&(schoolA_p->sections[0]));
printf("\n Location of schoolA_p->sections[1]= 0x%lx\n",&(schoolA_p->sections[1]));
printf("\n Outside of lopop Location of schoolA_p->sections[0]= 0x%lx\n", &(schoolA_p->sections[0])+ sizeOffset);
free(schoolA_p);
return 0;
}
Pointer arithmetic works like this: If p
is a pointer to an array element a[n]
, then p + i
is a pointer to a[n + i]
. In other words, pointer arithmetic moves in units of whatever base type the pointer is declared as, not bytes.
这篇关于访问dynamiclly分配的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!