0

I am trying to assign the buffer a size of THREAD_BUFFER_SIZE which I get from the option arguments on the command line using buffer = (int*) malloc(THREAD_BUFFER_SIZE); located at the bottom of the code below.

Here is my code:

int* buffer;

int main(int argc, char *argv[])
{
  int c;
  char *root_dir = default_root;
  int port = 10000;

  // Variable holders created for below
  int n_threads;
  int THREAD_BUFFER_SIZE;
    
  while ((c = getopt(argc, argv, "d:p:t:b:")) != -1)
    switch (c) {
    case 'd':
      root_dir = optarg;
      fprintf(stderr, "Root: %s\n", root_dir);
      break;
    case 'p':
      port = atoi(optarg);
      fprintf(stderr, "Port: %d\n", port);
      break;
    case 't':
      // NUMBER OF CONSUMER THREADS
      n_threads = atoi(optarg);
      fprintf(stderr, "Threads: %d\n", n_threads);
      break;
    case 'b':
      // BUFFER SIZE
      THREAD_BUFFER_SIZE = atoi(optarg);
      fprintf(stderr, "Thread Buffer Size: %d\n", THREAD_BUFFER_SIZE);
      break;
    default:
      fprintf(stderr, "usage: wserver [-d basedir] [-p port] [-t threads] [-b buffer]\n");

      fprintf(stderr, "%s\n", root_dir);
      fprintf(stderr, "%d\n", port);
      fprintf(stderr, "%d\n", n_threads);
      fprintf(stderr, "%d\n", THREAD_BUFFER_SIZE);

      exit(1);
    }

  // SHARED BUFFER
  buffer = (int*) malloc(THREAD_BUFFER_SIZE);

  fprintf(stderr, "actual buffer size: %d\n", sizeof(buffer));
}

When I execute this with ./wserver -d . -p 8080 -t 8 -b 10 I get the following output:

Root: .
Port: 8080
Threads: 8
Thread Buffer Size: 10
actual buffer size: 8

The actual buffer size should be 10.

When I execute this with ./wserver -d . -p 8080 -t 10 -b 4 I get the following output:

Root: .
Port: 8080
Threads: 10
Thread Buffer Size: 4
actual buffer size: 8

The actual buffer size should be 4.

Why is it that my actual buffer size is 8 and not the same as my THREAD_BUFFER_SIZE?

Joey O'Neill
  • 124
  • 1
  • 6
  • 3
    `sizeof(buffer)` That's the same as `sizeof(int*)`. It is not the size of the allocated buffer that the variable points to, and it could not be since the pointer does not know the size of that allocation. – dxiv Mar 09 '21 at 01:43
  • @dxiv is the malloc correctly allocating the space then? – Joey O'Neill Mar 09 '21 at 01:47
  • If [`malloc`](https://en.cppreference.com/w/c/memory/malloc) returns a non-NULL pointer then it means it succeeded. – dxiv Mar 09 '21 at 01:49
  • @dxiv yes that helps. – Joey O'Neill Mar 09 '21 at 01:50
  • _Side note:_ Is `THREAD_BUFFER_SIZE` a byte length or an element count? Your `malloc` assumes byte length. If the value is a count, you may want: `buffer = malloc(sizeof(*buffer) * THREAD_BUFFER_SIZE);` – Craig Estey Mar 09 '21 at 01:51
  • You don't have to know the actual allocated size by `malloc`. If the value of the pointer returned by `malloc` isn't NULL, you can assume that `malloc` has allocated at least the size specified in its argument. – M. Nejat Aydin Mar 09 '21 at 01:53
  • `sizeof(buffer)` is the size of the _pointer_ to your buffer [which is 8 bytes on a 64 bit machine]. It's _not_ how much space is _allocated_ for the buffer. It's an illusion because you did: `-b 4`. Try: `-b 37` and the value will _still_ be 8. – Craig Estey Mar 09 '21 at 01:55

0 Answers0