我这样做对吗? - C代码

发布于 2024-12-18 07:48:56 字数 1177 浏览 0 评论 0原文

当我运行这个程序时,它说大小是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

╭⌒浅淡时光〆 2024-12-25 07:48:56

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.

睡美人的小仙女 2024-12-25 07:48:56

不,String_New 中的 malloc 应该使用字符串的实际运行时长度,例如

String String_New(char basic_string[]) {
  String temp;
  int length = strlen(basic_string);
  temp.basic_string =
     (char *) malloc(length+1); /* 1 more byte for terminating \0 */
  strcpy(temp.basic_string, basic_string);
  return temp;
}

,但您应该只使用 strdup 例如temp.basic_string = strdup(basic_string);

No, the malloc inside your String_New should use the actual run-time length of the string, e.g.

String String_New(char basic_string[]) {
  String temp;
  int length = strlen(basic_string);
  temp.basic_string =
     (char *) malloc(length+1); /* 1 more byte for terminating \0 */
  strcpy(temp.basic_string, basic_string);
  return temp;
}

but you should just use strdup e.g. temp.basic_string = strdup (basic_string);

很快妥协 2024-12-25 07:48:56

在你的辅助函数 String_New() 中,当你说“sizeof(basic_string)”时,你要求的是指针的大小而不是字符串的大小,这就是为什么你得到 4 而不是 6。我认为你想要的是this:-

String String_New(const char* basic_string) {
  String temp;
  temp.basic_string = (char *) malloc(strlen(basic_string)+1);
  strcpy(temp.basic_string, basic_string);
  return temp;
}

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:-

String String_New(const char* basic_string) {
  String temp;
  temp.basic_string = (char *) malloc(strlen(basic_string)+1);
  strcpy(temp.basic_string, basic_string);
  return temp;
}

sizeof measures the sizes of types at compile time; your string arguments will be runtime variables.

葵雨 2024-12-25 07:48:56

我认为您期望 sizeof 执行与 strlen() 相同的操作,或者您的 String_GetSize() 执行的操作。这不是它的目的。

您正在对 char* 指针执行 sizeof,即“这个 char* 的大小是多少”,这是一个与“这个char*指向的字符串有多长”。在大多数(如果不是全部)32 位平台上,sizeof(char*) 实际上是 4

I think you're expecting sizeof to do the same thing that strlen() does, or what your String_GetSize() does. That's not what it's intended for.

You're performing sizeof on the char* pointer, i.e. "what is the size of this char*", which is a different question from "how long is the string pointed to by this char*". On most (if not all) 32-bit platforms sizeof(char*) will in fact be 4.

菊凝晚露 2024-12-25 07:48:56

在 C 中,“字符串”不是真正的数据类型。 sizeof 运算符采用数据类型或具有“类型”作为操作数的对象。在您的情况下,对象是 some_string.basic_string ,其类型为 char*,系统上指针的大小为 4。

解决方案是将 String 结构定义为有一个大小成员:

struct String {
  char *basic_string;
  size_t length ;
};

并在 String_New() 中分配时存储大小。这将简化并提高 String_GetSize() 函数的效率(由于 s == i,该函数已经过于复杂)。

还要注意,在 String_New() 中,basic_string 参数也是一个指针(尽管其签名中使用了“数组语法”)。我会避免使用这种语法,它具有误导性,因为在 C 中,除非将数组嵌入到结构中,否则无法通过复制传递数组;当作为参数传递时,数组总是“降级”为指针。此外,在任何情况下调用者都可以传递指针而不是数组。因此,在大多数情况下,您分配的内存太少(4 字节)。您应该使用 strlen() 或最初在 String_GetSize() 中使用的方法来确定长度。

String String_New(char* basic_string) 
{
  String temp;
  temp.length = strlen( basic_string ) ;
  temp.basic_string = (char *) malloc( temp.length + 1 );

  strcpy(temp.basic_string, basic_string);
  return temp;
}

size_t String_GetSize(String string) 
{
    return string.length ;
}

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 type char*, and the size of a pointer on your system is 4.

The solution is to define your String structure to have a size member:

struct String {
  char *basic_string;
  size_t length ;
};

And store the size when allocated in String_New(). This would simplify and make more efficient your String_GetSize() function (which is already over complicated since s == i).

Be aware also that in String_New(), that the basic_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 use strlen() or the method you originally used in String_GetSize() to determine the length.

String String_New(char* basic_string) 
{
  String temp;
  temp.length = strlen( basic_string ) ;
  temp.basic_string = (char *) malloc( temp.length + 1 );

  strcpy(temp.basic_string, basic_string);
  return temp;
}

size_t String_GetSize(String string) 
{
    return string.length ;
}
夜雨飘雪 2024-12-25 07:48:56

我认为你的函数String_New有问题,当你将basic_string传递给String_New时,它实际上是一个指针,即char *。所以,在32位机器中,sizeof(char *)是4

I think there is something wrong in your func String_New,when you pass basic_string to String_New, it is a pointer actually, that is char *.So, in 32bit machine, sizeof(char *) is 4

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文