-1

In libwebsockets library examples there is a variable of type struct lws_http_mount initialized here. The stuct lws_http_mount was declared here. Below is the snippet of its declaration and definition for convenience.

struct lws_http_mount {
   const struct lws_http_mount *mount_next;
   const char *mountpoint;
   const char *origin;
   const char *def;
   const char *protocol;
   const struct lws_protocol_vhost_options *cgienv;
   const struct lws_protocol_vhost_options *extra_mimetypes;
   const struct lws_protocol_vhost_options *interpret;
   int cgi_timeout;
   int cache_max_age;
   unsigned int auth_mask;
   unsigned int cache_reusable:1;
   unsigned int cache_revalidate:1;
   unsigned int cache_intermediaries:1;
   unsigned char origin_protocol;
   unsigned char mountpoint_len;
   const char *basic_auth_login_file;
   void *_unused[2];
};

static const struct lws_http_mount mount_localhost1 = {
   /* .mount_next */           NULL,
   /* .mountpoint */           "/",
   /* .origin */               "./mount-origin-localhost1",
   /* .def */                  "index.html",
   /* .protocol */              NULL,
   /* .cgienv */                NULL,
   /* .extra_mimetypes */       NULL,
   /* .interpret */             NULL,
   /* .cgi_timeout */           0,
   /* .cache_max_age */         0,
   /* .auth_mask */             0,
   /* .cache_reusable */        0,
   /* .cache_revalidate */      0,
   /* .cache_intermediaries */  0,
   /* .origin_protocol */       LWSMPRO_FILE,
   /* .mountpoint_len */        1,
   /* .basic_auth_login_file */ NULL,
}

The member mountpoint of this struct is of type const char*. This has been initialized to "\" in the variable mount_localhost1. what will be the actual char array size allocated inside this struct for its member mountpoint?

I only know that char array member declaration inside the struct should be done as char mountpoint[string_length] instead of const char* mountpoint.

Necktwi
  • 2,483
  • 7
  • 39
  • 62

1 Answers1

1

The string literal (char array) "/" is created in memory, and the constant char pointer mountpoint is initialized with the address to that memory.

There is no "actual char array size allocated inside this struct".

It is not different from initializing a local variable like:

const char *mountpoint = "/";

The size of the variable (const char *) is only the size of the pointer. The actual string (char array) goes somewhere else, which is unrelated to the character pointer itself.

Ignatius
  • 2,745
  • 2
  • 20
  • 32