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?