我这样做对吗? - C代码
当我运行这个程序时,它说大小是 4,但实际上是 6。它在这里这样做:
printf("String Size: %u\n", sizeof some_string.basic_string);
我是 C 内存分配的新手,以前从未使用过 malloc。我使用 malloc 对吗?
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct String String;
struct String {
char *basic_string;
};
String String_New(char basic_string[]) {
String temp;
temp.basic_string = (char *) malloc(sizeof basic_string);
strcpy(temp.basic_string, basic_string);
return temp;
}
void String_Delete(String *string) {
free(string->basic_string);
string->basic_string = NULL;
}
int String_GetSize(String string) {
int i = 0, s = 0;
while (string.basic_string[i] != '\0') {
i++;
s++;
}
return s;
}
int main(int argc, char *argv[]) {
String some_string = String_New("hello");
printf("String Literal: %s\n", some_string.basic_string);
printf("String Size: %u\n", sizeof some_string.basic_string);
printf("String Length: %d\n", String_GetSize(some_string));
String_Delete(&some_string);
if (some_string.basic_string == NULL) {
return 0;
}
return 1;
}
when i run this it says the size is 4 when it is really six. it does this here:
printf("String Size: %u\n", sizeof some_string.basic_string);
i am new to c memory allocation and never used malloc before. am i using malloc right?
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct String String;
struct String {
char *basic_string;
};
String String_New(char basic_string[]) {
String temp;
temp.basic_string = (char *) malloc(sizeof basic_string);
strcpy(temp.basic_string, basic_string);
return temp;
}
void String_Delete(String *string) {
free(string->basic_string);
string->basic_string = NULL;
}
int String_GetSize(String string) {
int i = 0, s = 0;
while (string.basic_string[i] != '\0') {
i++;
s++;
}
return s;
}
int main(int argc, char *argv[]) {
String some_string = String_New("hello");
printf("String Literal: %s\n", some_string.basic_string);
printf("String Size: %u\n", sizeof some_string.basic_string);
printf("String Length: %d\n", String_GetSize(some_string));
String_Delete(&some_string);
if (some_string.basic_string == NULL) {
return 0;
}
return 1;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
char *basic_string;
是一个指向某个内存的指针,该指针的大小(在 32 位系统上)是 32bits = 4bytes。 Sizeof 不知道您在此地址保留的内存大小。通常没有办法获得 malloc 保留的内存大小 - 尽管您的系统可能有一些私有调试功能来执行此操作。
char *basic_string;
is a pointer to some memory, the size of the pointer (on a 32bit system) is 32bits = 4bytes. Sizeof doesn't know about the size of memory you have reserved at this address.There is generally no way to get the size of memory reserved by malloc - although your system may have some private debugging features to do this.
不,
String_New
中的malloc
应该使用字符串的实际运行时长度,例如,但您应该只使用
strdup
例如temp.basic_string = strdup(basic_string);
No, the
malloc
inside yourString_New
should use the actual run-time length of the string, e.g.but you should just use
strdup
e.g.temp.basic_string = strdup (basic_string);
在你的辅助函数 String_New() 中,当你说“sizeof(basic_string)”时,你要求的是指针的大小而不是字符串的大小,这就是为什么你得到 4 而不是 6。我认为你想要的是this:-
sizeof 在编译时测量类型的大小;您的字符串参数将是运行时变量。
In your helper function String_New(), you are asking for the size of the pointer as opposed to the size of the string when you say "sizeof(basic_string)", thats why you get 4 and not 6. I think what you want is this:-
sizeof measures the sizes of types at compile time; your string arguments will be runtime variables.
我认为您期望
sizeof
执行与strlen()
相同的操作,或者您的String_GetSize()
执行的操作。这不是它的目的。您正在对
char*
指针执行sizeof
,即“这个char*
的大小是多少”,这是一个与“这个char*
指向的字符串有多长”。在大多数(如果不是全部)32 位平台上,sizeof(char*)
实际上是4
。I think you're expecting
sizeof
to do the same thing thatstrlen()
does, or what yourString_GetSize()
does. That's not what it's intended for.You're performing
sizeof
on thechar*
pointer, i.e. "what is the size of thischar*
", which is a different question from "how long is the string pointed to by thischar*
". On most (if not all) 32-bit platformssizeof(char*)
will in fact be4
.在 C 中,“字符串”不是真正的数据类型。 sizeof 运算符采用数据类型或具有“类型”作为操作数的对象。在您的情况下,对象是
some_string.basic_string
,其类型为char*
,系统上指针的大小为 4。解决方案是将 String 结构定义为有一个大小成员:
并在
String_New()
中分配时存储大小。这将简化并提高String_GetSize()
函数的效率(由于 s == i,该函数已经过于复杂)。还要注意,在
String_New()
中,basic_string
参数也是一个指针(尽管其签名中使用了“数组语法”)。我会避免使用这种语法,它具有误导性,因为在 C 中,除非将数组嵌入到结构中,否则无法通过复制传递数组;当作为参数传递时,数组总是“降级”为指针。此外,在任何情况下调用者都可以传递指针而不是数组。因此,在大多数情况下,您分配的内存太少(4 字节)。您应该使用strlen()
或最初在String_GetSize()
中使用的方法来确定长度。In C a "string" is not a true data type. The sizeof operator takes a data type, or an object that has "type" as an operand. In your case the object is
some_string.basic_string
which has typechar*
, and the size of a pointer on your system is 4.The solution is to define your String structure to have a size member:
And store the size when allocated in
String_New()
. This would simplify and make more efficient yourString_GetSize()
function (which is already over complicated since s == i).Be aware also that in
String_New()
, that thebasic_string
parameter is also a pointer (despite the "array syntax" used in its signature). I would avoid this syntax, it is misleading since in C you cannot pass an array by copy unless the array is embedded in a struct; arrays always "degrade" to pointers when passed as arguments. Moreover the caller may pass a pointer rather than an array in any case. So in most cases you will have allocated too little memory (4 bytes). You should usestrlen()
or the method you originally used inString_GetSize()
to determine the length.我认为你的函数
String_New
有问题,当你将basic_string
传递给String_New
时,它实际上是一个指针,即char *
。所以,在32位机器中,sizeof(char *)
是4I think there is something wrong in your func
String_New
,when you passbasic_string
toString_New
, it is a pointer actually, that ischar *
.So, in 32bit machine,sizeof(char *)
is 4