At the moment, I try to understand dynamic arrays in C. When I allocate the memory for the pointer "ptr", it is working without entering the numbers of elements (in the malloc function) I need.
Now, the problem is, don't understand why it is working.
Would be great, if someone could me some advice. Thanks.
目前,我试图理解c语言中的动态数组。当我为指针“ptr”分配内存时,它在工作时不会输入我需要的元素数量(在malloc函数中)。现在的问题是,不要理解它为什么有效。如果有人能给我一些建议,那就太好了。谢谢。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct daten
{
char name[20];
int alter;
};
int main(void)
{
struct daten *ptr;
int i;
ptr = malloc(sizeof(struct daten *)); //works fine!!
//ptr = malloc(sizeof(struct daten *)*4);
strcpy(ptr[0].name, "Daniel");
ptr[0].alter = 23;
strcpy(ptr[1].name, "F*");
ptr[1].alter = 29;
strcpy(ptr[2].name, "Helmut");
ptr[2].alter = 34;
strcpy(ptr[3].name, "Katrin");
ptr[3].alter = 21;
for(i = 0; i<4; i++)
{
printf("%s\t", ptr[i].name);
printf("%d\n", ptr[i].alter);
}
return 0;
}
5 个解决方案
#1
2
The keyword is that it doesn't "work fine". At most it seems to be working.
关键字是它不能“正常工作”。充其量,它似乎在起作用。
What you have here is undefined behavior (since you're allocating space for one struct, however, you're writing to a much larger space). Undefined behavior can do anything; it doesn't mean the program must crash. This includes that it may "work fine".
这里有一个未定义的行为(因为您正在为一个结构体分配空间,但是,您正在编写一个更大的空间)。未定义的行为可以做任何事情;这并不意味着程序必须崩溃。这包括它可能“运行良好”。
Also, you're misunderstanding how much memory you should be allocating. For a type T
, assigned to a pointer of type T *
, sizeof(T)
bytes should be allocated, like this:
此外,您还误解了应该分配多少内存。对于类型T,分配给类型T *的指针,应该分配sizeof(T)字节,如下所示:
ptr = malloc(sizeof(struct daten) * 4);
Even better:
更好的是:
ptr = malloc(sizeof(*ptr) * 4);
#2
2
Since it is an undefined behavior, anything can happen: your program may work fine. But is doesn't mean that it is a correct program.
由于它是一种未定义的行为,所以任何事情都可能发生:您的程序可能正常工作。但是is并不意味着它是一个正确的程序。
Thus you must allocate enough memory to do this, ie sizeof(struct daten) * 4
bytes.
因此,您必须分配足够的内存来实现这一点,即sizeof(struct daten) * 4字节。
#3
0
Hmm, you just write to unallocated area. This can work, but on the long run you will experience crahes, because its very likely that your heap gets corrupted by writing into unallocated areas, so successive mallocs might fail or crash your program.
嗯,你只写未分配的区域。这可以工作,但是从长远来看,您将会遇到一些问题,因为您的堆很可能由于写入到未分配的区域而受到破坏,因此连续的mallocs可能会失败或导致程序崩溃。
#4
0
ptr[3]
could also be expressed as *(ptr + 3)
, where '3' is scaled to (3 * sizeof(*p))
. So it is copying data into some memory location way beyond the memory you have allocated.
ptr[3]也可以表示为*(ptr + 3),其中‘3’被缩放到(3 * sizeof(*p)。它将数据复制到内存位置远超过你分配的内存。
You are lucky, or maybe I should say you are unlucky. What you have done is a bit like randomly stabbing someone, you might hit a vital organ, or you might not. This time you didn't, compile the code without debug (for example) to change memory layout and you might hit something that would kill the process. Or maybe not kill it, but just injure it.
你很幸运,或者我应该说你很不幸。你所做的有点像随机刺人,你可能会击中要害器官,也可能不会。这一次您没有这样做,而是在没有调试(例如)的情况下编译代码,以更改内存布局,您可能会遇到一些会破坏进程的东西。也可能不是杀死它,而是伤害它。
You have come across one of the reasons why programming in C has to be disciplined. No it didn't "work", your tests were not good enough to show that it failed.
您已经遇到了在C语言中编程必须遵守的一个原因。不,它没有“工作”,你的测试不够好,显示它失败了。
See also calloc
.
也看到calloc。
#5
0
This is undefined behaviour. You are simply (un)lucky that its working. If you try this on a different system, it may or may not work.
这是未定义的行为。你很幸运,它起作用了。如果您在另一个系统上尝试此操作,它可能会工作,也可能不会工作。
You should do :
你应该做的是:
ptr = malloc(sizeof(struct daten)*4);
The operations are succeeding as the data is probably being written to the memory just after the allocated space. As this is unallocated space, you are not having any problems and it is working
. You will have problems when you try to use this space as well.
操作是成功的,因为数据可能是在分配的空间之后被写入内存的。由于这是未分配的空间,所以您没有遇到任何问题,而且它正在工作。当您试图使用这个空间时,也会遇到问题。
#1
2
The keyword is that it doesn't "work fine". At most it seems to be working.
关键字是它不能“正常工作”。充其量,它似乎在起作用。
What you have here is undefined behavior (since you're allocating space for one struct, however, you're writing to a much larger space). Undefined behavior can do anything; it doesn't mean the program must crash. This includes that it may "work fine".
这里有一个未定义的行为(因为您正在为一个结构体分配空间,但是,您正在编写一个更大的空间)。未定义的行为可以做任何事情;这并不意味着程序必须崩溃。这包括它可能“运行良好”。
Also, you're misunderstanding how much memory you should be allocating. For a type T
, assigned to a pointer of type T *
, sizeof(T)
bytes should be allocated, like this:
此外,您还误解了应该分配多少内存。对于类型T,分配给类型T *的指针,应该分配sizeof(T)字节,如下所示:
ptr = malloc(sizeof(struct daten) * 4);
Even better:
更好的是:
ptr = malloc(sizeof(*ptr) * 4);
#2
2
Since it is an undefined behavior, anything can happen: your program may work fine. But is doesn't mean that it is a correct program.
由于它是一种未定义的行为,所以任何事情都可能发生:您的程序可能正常工作。但是is并不意味着它是一个正确的程序。
Thus you must allocate enough memory to do this, ie sizeof(struct daten) * 4
bytes.
因此,您必须分配足够的内存来实现这一点,即sizeof(struct daten) * 4字节。
#3
0
Hmm, you just write to unallocated area. This can work, but on the long run you will experience crahes, because its very likely that your heap gets corrupted by writing into unallocated areas, so successive mallocs might fail or crash your program.
嗯,你只写未分配的区域。这可以工作,但是从长远来看,您将会遇到一些问题,因为您的堆很可能由于写入到未分配的区域而受到破坏,因此连续的mallocs可能会失败或导致程序崩溃。
#4
0
ptr[3]
could also be expressed as *(ptr + 3)
, where '3' is scaled to (3 * sizeof(*p))
. So it is copying data into some memory location way beyond the memory you have allocated.
ptr[3]也可以表示为*(ptr + 3),其中‘3’被缩放到(3 * sizeof(*p)。它将数据复制到内存位置远超过你分配的内存。
You are lucky, or maybe I should say you are unlucky. What you have done is a bit like randomly stabbing someone, you might hit a vital organ, or you might not. This time you didn't, compile the code without debug (for example) to change memory layout and you might hit something that would kill the process. Or maybe not kill it, but just injure it.
你很幸运,或者我应该说你很不幸。你所做的有点像随机刺人,你可能会击中要害器官,也可能不会。这一次您没有这样做,而是在没有调试(例如)的情况下编译代码,以更改内存布局,您可能会遇到一些会破坏进程的东西。也可能不是杀死它,而是伤害它。
You have come across one of the reasons why programming in C has to be disciplined. No it didn't "work", your tests were not good enough to show that it failed.
您已经遇到了在C语言中编程必须遵守的一个原因。不,它没有“工作”,你的测试不够好,显示它失败了。
See also calloc
.
也看到calloc。
#5
0
This is undefined behaviour. You are simply (un)lucky that its working. If you try this on a different system, it may or may not work.
这是未定义的行为。你很幸运,它起作用了。如果您在另一个系统上尝试此操作,它可能会工作,也可能不会工作。
You should do :
你应该做的是:
ptr = malloc(sizeof(struct daten)*4);
The operations are succeeding as the data is probably being written to the memory just after the allocated space. As this is unallocated space, you are not having any problems and it is working
. You will have problems when you try to use this space as well.
操作是成功的,因为数据可能是在分配的空间之后被写入内存的。由于这是未分配的空间,所以您没有遇到任何问题,而且它正在工作。当您试图使用这个空间时,也会遇到问题。