I am trying to create a char array dynamically. However, do not matter which number I put as the desired value of the size array it just keep using 4 has the size. If I place 3,2 or even 8, 10 it does not change...
我正在尝试动态创建一个char数组。但是,无论我把哪个数字作为大小数组的期望值,它只是保持使用4具有大小。如果我放置3,2甚至8,10它不会改变......
Part where I initialize it...
我初始化它的部分......
char *x;
x = (char *)calloc(10, sizeof(char));
if(x == NULL )
{
return;
}
cout <<"Size_Vector = "<<sizeof(x)<<endl; //Keep making the size of the array 4..no matter what
4 个解决方案
#1
2
sizeof(x)
returns the size of the pointer not the allocated memory or the size of array.
sizeof(x)返回指针的大小而不是分配的内存或数组的大小。
It always returns 4
size of an pointer on your envrionment is 4
and it will be the same for all.
它总是返回4个大小的指针在你的环境是4,它将是相同的所有。
You will have to keep track of how much memory you allocated yourself & also ensure that you do not write beyond the bounds of that allocated memory.
您必须跟踪自己分配的内存量,并确保不要超出分配的内存范围。
#2
2
x
is a pointer of type char *
. sizeof(x)
returns the size of the pointer instead of the array size.
x是char *类型的指针。 sizeof(x)返回指针的大小而不是数组大小。
Generally, there is no way to know the array size from a pointer type even if it's pointing to an array, because of array decaying.
通常,由于数组衰减,即使它指向数组,也无法从指针类型知道数组大小。
char *x = malloc(sizeof(char)*10);
// This will give you 4
printf ("%d\n", sizeof(x));
But you can get the size of an array type.
但是你可以得到一个数组类型的大小。
char x[10];
// This will give you 10
printf ("%d\n", sizeof(x));
#3
0
The malloc call returns a void pointer and when you say sizeof(pointer) you ask for the size of the pointer and not the chunk of memory that you just declared.So it gives you 4/8/... bytes depending on the m/c you are using.
malloc调用返回一个void指针,当你说sizeof(指针)时,你要求指针的大小,而不是你刚刚声明的内存块。所以它给你4/8 / ......字节,具体取决于m / c你正在使用。
Try this out :
试试这个:
char *cptr;
int *iptr;
printf("size of integer pointer is : %d\n",sizeof(iptr));
printf(“整数指针的大小为:%d \ n”,sizeof(iptr));
printf("size of character pointer is : %d\n",sizeof(cptr));
printf(“字符指针的大小为:%d \ n”,sizeof(cptr));
Note : malloc points to the address of the first element of the chunk of memory(ie 10 * 1 = 10 bytes) that you have dynamically obtained by calling it.
注意:malloc指向通过调用它动态获得的内存块的第一个元素的地址(即10 * 1 = 10个字节)。
#4
0
This is a short piece of code i wrote for you in case you really want to find the size of the memory piece you reserved by calling calloc though its obvious that when you say 10 spaces of 1 byte each then you are going to get 10 bytes like the case here which says you want 10 * sizeof(char) :
这是我为你写的一小段代码,如果你真的想通过调用calloc找到你保留的内存块的大小,虽然很明显,当你说10个1个字节的空格时,你将得到10个字节就像这里说你想要10 * sizeof(char)的情况:
include
include
main(){
char *ptr = NULL;
int i = 0;
int size = 0;
int n = 0;
printf("Enter the size of the dynamic array in bytes : ");
scanf("%d",&n);
ptr = (char*) calloc (n,sizeof(char));
for(i=0;i<n;i++){
size = size + sizeof(ptr[i]);
}
printf("Size is %d\n",size);
}
#1
2
sizeof(x)
returns the size of the pointer not the allocated memory or the size of array.
sizeof(x)返回指针的大小而不是分配的内存或数组的大小。
It always returns 4
size of an pointer on your envrionment is 4
and it will be the same for all.
它总是返回4个大小的指针在你的环境是4,它将是相同的所有。
You will have to keep track of how much memory you allocated yourself & also ensure that you do not write beyond the bounds of that allocated memory.
您必须跟踪自己分配的内存量,并确保不要超出分配的内存范围。
#2
2
x
is a pointer of type char *
. sizeof(x)
returns the size of the pointer instead of the array size.
x是char *类型的指针。 sizeof(x)返回指针的大小而不是数组大小。
Generally, there is no way to know the array size from a pointer type even if it's pointing to an array, because of array decaying.
通常,由于数组衰减,即使它指向数组,也无法从指针类型知道数组大小。
char *x = malloc(sizeof(char)*10);
// This will give you 4
printf ("%d\n", sizeof(x));
But you can get the size of an array type.
但是你可以得到一个数组类型的大小。
char x[10];
// This will give you 10
printf ("%d\n", sizeof(x));
#3
0
The malloc call returns a void pointer and when you say sizeof(pointer) you ask for the size of the pointer and not the chunk of memory that you just declared.So it gives you 4/8/... bytes depending on the m/c you are using.
malloc调用返回一个void指针,当你说sizeof(指针)时,你要求指针的大小,而不是你刚刚声明的内存块。所以它给你4/8 / ......字节,具体取决于m / c你正在使用。
Try this out :
试试这个:
char *cptr;
int *iptr;
printf("size of integer pointer is : %d\n",sizeof(iptr));
printf(“整数指针的大小为:%d \ n”,sizeof(iptr));
printf("size of character pointer is : %d\n",sizeof(cptr));
printf(“字符指针的大小为:%d \ n”,sizeof(cptr));
Note : malloc points to the address of the first element of the chunk of memory(ie 10 * 1 = 10 bytes) that you have dynamically obtained by calling it.
注意:malloc指向通过调用它动态获得的内存块的第一个元素的地址(即10 * 1 = 10个字节)。
#4
0
This is a short piece of code i wrote for you in case you really want to find the size of the memory piece you reserved by calling calloc though its obvious that when you say 10 spaces of 1 byte each then you are going to get 10 bytes like the case here which says you want 10 * sizeof(char) :
这是我为你写的一小段代码,如果你真的想通过调用calloc找到你保留的内存块的大小,虽然很明显,当你说10个1个字节的空格时,你将得到10个字节就像这里说你想要10 * sizeof(char)的情况:
include
include
main(){
char *ptr = NULL;
int i = 0;
int size = 0;
int n = 0;
printf("Enter the size of the dynamic array in bytes : ");
scanf("%d",&n);
ptr = (char*) calloc (n,sizeof(char));
for(i=0;i<n;i++){
size = size + sizeof(ptr[i]);
}
printf("Size is %d\n",size);
}