The Geek Forum

  • March 28, 2024, 06:15:04 AM
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

News:

Due to the prolific nature of these forums, poster aggression is advised.

*

Recent Forum Posts

Shout Box

Members
Stats
  • Total Posts: 129468
  • Total Topics: 7101
  • Online Today: 48
  • Online Ever: 1013
  • (January 12, 2023, 01:18:11 AM)

Author Topic: Annoying void pointers  (Read 3379 times)

Novice

  • Forum Ninja
  • Hacker
  • ****
  • Coolio Points: +205/-10
  • Offline Offline
  • Gender: Male
  • Posts: 1463
  • Thread Killer 3.0
    • View Profile
Annoying void pointers
« on: November 16, 2010, 09:57:17 AM »

I usually make it a point not to use void pointers. However, I currently have no choice and am running into an issue:

I'm creating a separate thread for my 'producer' function:

void * producer(void * num){
     printf("\nProducer %d",(int)num);
}

int main(){
     int prodId = 1;
     pthread_create(&tid,&attr,producer,&prodId);
}


If you don't know, the pthread_create function creates my new thread. The 3rd argument is the function the new thread runs (my producer function), and the 4th argument is whatever I want to pass to it. However, the 4th argument is expecting a void pointer (this is set by the pthread_create function so I can't just pass a regular int).

Well, I'm passing the address of an integer as you can see. Everything compiles, but the output in my 'producer' function is junk:

>./run
Producer -1079038060

Everything online says I should be casting like so: (int*)num
However, when I do I get an error in the output about %d expecting a int but I'm using *int.

I'm sure there's some pointer logic that is escaping me. Any help would be appreciated. BTW, here's the whole thing:
Code: [Select]
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <semaphore.h>
#include <pthread.h>

#define BUFFERSIZE 5
#define ZERO 0
#define FACTOR 0.000000001

int Buffer[BUFFERSIZE] = {0}; //Our "empty" value is going to be 0
int in = 0;
int out = 0;
sem_t empty;
sem_t full;
pthread_mutex_t mutex;

void insertItem(int num);
void removeItem(int * num);
void * producer(void * num);
void * consumer(void * num);

int main (int argc, char *argv[]) {
//Set sleeptime, number of procuder, and number of consumers from CLI
int sleeptime = atoi(argv[1]);
int numprod = atoi(argv[2]);
int numcons = atoi(argv[3]);
//Set Ids = 1. The user probably won't start counting at 0.
int prodId = 1;
int consId = 1;

//Initialize mutex lock and semaphores for use in producer and consumer
pthread_mutex_init(&mutex,NULL);
sem_init(&empty,0,5);
sem_init(&full,0,0);

while(prodId!=(numprod+1)){ //Producer loop. Create producer threads. While loop because it's easier.
pthread_t tid; //thread identifier.
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid,&attr,producer,&prodId); //Start producer thread and tell it which producer it is.
pthread_join(tid,NULL);
prodId++;
}

while(consId!=(numcons+1)){ //Consumer loop. Create Consumer threads.
pthread_t tid; //thread identifier.
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid,&attr,consumer,&consId); //Start consumer thread and tell it which consumer it is.
pthread_join(tid,NULL);
consId++;
}

sleep(sleeptime); //sleep

return 0;
}

void insertItem(int num){
pthread_mutex_lock(&mutex);
Buffer[in] = num;
if(in==4){
in = 0;
}
else{
in++;
}
pthread_mutex_unlock(&mutex);
}
void removeItem(int * num){
pthread_mutex_lock(&mutex);
num = &Buffer[out];
Buffer[out]=0;
if(out==4){
out = 0;
}
else{
out++;
}
pthread_mutex_lock(&mutex);
}
void * producer(void * num){
int item;
while(true){
sleep(rand()*FACTOR);
item = rand();
sem_wait(&empty);
insertItem(item);
printf("\nProducer %d produced %d",(int)num,item);
sem_post(&full);
}
return 0;
}
void * consumer(void * num){
int item;
while(true){
sleep(rand()*FACTOR);
sem_wait(&full);
removeItem(&item);
printf("\nComsumer %d consumes %d",(int)num,item);
sem_post(&empty);
}
return 0;
}
« Last Edit: November 16, 2010, 04:03:20 PM by Novice »
Logged
Look at you, hacker: a pathetic creature of meat and bone, panting and sweating as you run through my corridors. How can you challenge a perfect, immortal machine?

Probie

  • Professional Blogger
  • ***
  • Coolio Points: +223/-5
  • Offline Offline
  • Posts: 644
  • The natural nap catalyst
    • View Profile
    • SoggyBlog
Re: Annoying void pointers
« Reply #1 on: November 16, 2010, 11:15:29 AM »


in your printf dereference your pointer (int)*num i think.
Logged
// ------- Probie ------- //
// ----------------------- //
// --- T3H GEEKERY --- //
// ----------------------- //
www.niccisixx.com - I can put this here, because I'm awesome.

Probie

  • Professional Blogger
  • ***
  • Coolio Points: +223/-5
  • Offline Offline
  • Posts: 644
  • The natural nap catalyst
    • View Profile
    • SoggyBlog
Re: Annoying void pointers
« Reply #2 on: November 16, 2010, 11:16:55 AM »


yeah it looks like you are casting the pointer address into an integer
Logged
// ------- Probie ------- //
// ----------------------- //
// --- T3H GEEKERY --- //
// ----------------------- //
www.niccisixx.com - I can put this here, because I'm awesome.

Novice

  • Forum Ninja
  • Hacker
  • ****
  • Coolio Points: +205/-10
  • Offline Offline
  • Gender: Male
  • Posts: 1463
  • Thread Killer 3.0
    • View Profile
Re: Annoying void pointers
« Reply #3 on: November 16, 2010, 12:44:26 PM »

Thanks for the reply. Actually, I've tried that. Let me summarize what I've tried and the outcome:

printf("\nProducer %d",(int)num);
and
printf("\nProducer %d",(int*)num);

Gives a junk output.

printf("\nProducer %d",(int)*num);

Gives "void* is not a pointer-to-object type"



I think it might actually be related to the fact that I am passing this address to a new thread. The new thread will have a different local address space, so perhaps it is not able to point to main's local address space.

Although, it still fails when prodId is a global. . .
« Last Edit: November 16, 2010, 12:47:27 PM by Novice »
Logged
Look at you, hacker: a pathetic creature of meat and bone, panting and sweating as you run through my corridors. How can you challenge a perfect, immortal machine?

Novice

  • Forum Ninja
  • Hacker
  • ****
  • Coolio Points: +205/-10
  • Offline Offline
  • Gender: Male
  • Posts: 1463
  • Thread Killer 3.0
    • View Profile
Re: Annoying void pointers
« Reply #4 on: November 16, 2010, 01:09:46 PM »

int *y;
y = (int)* num
printf("\nProducer %d",*y);


Yeah. This worked.
Enough programming! Nap time.

* Novice looks at Probie.
« Last Edit: November 16, 2010, 01:30:53 PM by Novice »
Logged
Look at you, hacker: a pathetic creature of meat and bone, panting and sweating as you run through my corridors. How can you challenge a perfect, immortal machine?

Probie

  • Professional Blogger
  • ***
  • Coolio Points: +223/-5
  • Offline Offline
  • Posts: 644
  • The natural nap catalyst
    • View Profile
    • SoggyBlog
Re: Annoying void pointers
« Reply #5 on: November 17, 2010, 06:19:50 AM »


interesting, never saw that before. Good catch. :)
Logged
// ------- Probie ------- //
// ----------------------- //
// --- T3H GEEKERY --- //
// ----------------------- //
www.niccisixx.com - I can put this here, because I'm awesome.