I came across this in someones code... I don't know if it is correct (because even tho it looks wrong, it works). Could someone clarify whether this is correct or not, why so, and why it works anyway ?
我在某些代码中遇到过这个...我不知道它是否正确(因为即使它看起来不对,它也有效)。有人可以澄清这是否正确,为什么如此,以及为什么它仍然有效?
In short, we want to store all the arguments (given as command-line), concatenated, in 1 string.
简而言之,我们希望将所有参数(以命令行给出)存储在1个字符串中。
Note: each string has at least 1 character.
注意:每个字符串至少包含1个字符。
Snippet:
片段:
int main(int argc, char **argv) {
// Declaring a pointer to a string
char *desintation_string;
// Allocating enough memory to store all arguments (given as command-line) concatenated
destination_string = malloc((argc) * sizeof(char)); /* <————— is this correct ? does
it indeed allocate
enough memory to fit
all the arguments
concatenated ? */
. . .
}
The question is:
问题是:
Does this line "destination_string = malloc((argc) * sizeof(char));
" allocate enough memory to do so ?
这行是“destination_string = malloc((argc)* sizeof(char));”分配足够的内存来做到这一点?
Can someone explain exactly what this is doing ? Cuz I read it as: it is allocating (argc * 1 Byte). Yet, when you run it and copy the arguments to it, it works, could someone explain that too ?
有人能解释一下这是做什么的吗?因为我把它读作:它正在分配(argc * 1 Byte)。然而,当你运行它并将参数复制到它时,它有效,有人可以解释一下吗?
3 个解决方案
#1
4
No. Say your arguments are "foo" "bar"
. This makes argc = 2
. With malloc((argc) * sizeof(char))
you are allocating memory for only 2 chars in this case.
不。说你的论点是“foo”“bar”。这使得argc = 2.对于malloc((argc)* sizeof(char)),在这种情况下,您只为2个字符分配内存。
argv
is a 2D array (hence argv**
). You need to check length of each argument fist in order to allocate memory for them.
argv是一个2D数组(因此是argv **)。您需要检查每个参数的长度,以便为它们分配内存。
What malloc((argc) * sizeof(char))
does: argc
is the number of arguments you pass. sizeof(char)
returns the number of bytes needs to be allocated for a char variable. So you get malloc(<number of bytes needed to store argc number of char variables>)
. malloc()
allocates that number of bytes in the heap.
malloc((argc)* sizeof(char))的作用:argc是您传递的参数数量。 sizeof(char)返回需要为char变量分配的字节数。所以你得到malloc( <存储argc char变量数所需的字节数> )。 malloc()在堆中分配该字节数。
#2
0
Does this line "destination_string = malloc((argc) * sizeof(char));" allocate enough memory to do so ?
这行是“destination_string = malloc((argc)* sizeof(char));”分配足够的内存来做到这一点?
No. You need to allocate enough memory. for example here
不,你需要分配足够的内存。比如这里
#define BUFSIZE YOUR_EXPECTED_SIZE
destination_string = malloc((BUFSIZE) * sizeof(char));
for example command line: a.out foo bar
例如命令行:a.out foo bar
snprintf(foo, 1024, "%s - %s\n", argv[1], argv[2]);
#3
0
Your code will work for the only a short type of arguments (i.e. -x
). But for the long type of arguments (i.e. --list
), it will be failed.
您的代码仅适用于短类型的参数(即-x)。但是对于长类型的参数(即--list),它将失败。
This is how you can do it.
这就是你如何做到的。
int main(int argc, char **argv) {
// Declaring a pointer to a string.
char *desintation_string;
int Arg_Size = 0;
// Allocating enough memory to store all arguments concatenated.
// argv[0] is path not argument given in command line
for (int i=1, i <= argc, i++)
Arg_Size += sizeof(argv[i]);
destination_string = malloc(Arg_Size);
. . .
}
#1
4
No. Say your arguments are "foo" "bar"
. This makes argc = 2
. With malloc((argc) * sizeof(char))
you are allocating memory for only 2 chars in this case.
不。说你的论点是“foo”“bar”。这使得argc = 2.对于malloc((argc)* sizeof(char)),在这种情况下,您只为2个字符分配内存。
argv
is a 2D array (hence argv**
). You need to check length of each argument fist in order to allocate memory for them.
argv是一个2D数组(因此是argv **)。您需要检查每个参数的长度,以便为它们分配内存。
What malloc((argc) * sizeof(char))
does: argc
is the number of arguments you pass. sizeof(char)
returns the number of bytes needs to be allocated for a char variable. So you get malloc(<number of bytes needed to store argc number of char variables>)
. malloc()
allocates that number of bytes in the heap.
malloc((argc)* sizeof(char))的作用:argc是您传递的参数数量。 sizeof(char)返回需要为char变量分配的字节数。所以你得到malloc( <存储argc char变量数所需的字节数> )。 malloc()在堆中分配该字节数。
#2
0
Does this line "destination_string = malloc((argc) * sizeof(char));" allocate enough memory to do so ?
这行是“destination_string = malloc((argc)* sizeof(char));”分配足够的内存来做到这一点?
No. You need to allocate enough memory. for example here
不,你需要分配足够的内存。比如这里
#define BUFSIZE YOUR_EXPECTED_SIZE
destination_string = malloc((BUFSIZE) * sizeof(char));
for example command line: a.out foo bar
例如命令行:a.out foo bar
snprintf(foo, 1024, "%s - %s\n", argv[1], argv[2]);
#3
0
Your code will work for the only a short type of arguments (i.e. -x
). But for the long type of arguments (i.e. --list
), it will be failed.
您的代码仅适用于短类型的参数(即-x)。但是对于长类型的参数(即--list),它将失败。
This is how you can do it.
这就是你如何做到的。
int main(int argc, char **argv) {
// Declaring a pointer to a string.
char *desintation_string;
int Arg_Size = 0;
// Allocating enough memory to store all arguments concatenated.
// argv[0] is path not argument given in command line
for (int i=1, i <= argc, i++)
Arg_Size += sizeof(argv[i]);
destination_string = malloc(Arg_Size);
. . .
}