7 Replies - 1619 Views - Last Post: 13 December 2010 - 04:42 PM Rate Topic: -----

#1 rafi1082  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 02-January 09

problem with *void and comperator

Posted 13 December 2010 - 03:10 PM

i got this exercise
Posted Image
and i wrote this code.
i have errors with the compare part in this row
if (shared[j] > shared[j + 1])

and a warning in the swap.
if someone please explain exactly how to fix this problem (or beter fix it) i'l be happy.
tnx allot
# include <fcntl.h>
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <sys/types.h>
# include <sys/ipc.h>
# include <sys/stat.h>
# include <sys/shm.h>
# include <sys/sem.h>
# include <sys/wait.h>
# include <sys/types.h>
# include <unistd.h>
# include <string.h>
#define swap(x,y) do \
   { unsigned char swap_temp[sizeof(x) == sizeof(y) ? (signed)sizeof(x) : -1]; \
     memcpy(swap_temp,&y,sizeof(x)); \
     memcpy(&y,&x,       sizeof(x)); \
     memcpy(&x,swap_temp,sizeof(x)); \
    } while(0)

union semun {
	int val; /* Value for SETVAL */
	struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
	unsigned short *array; /* Array for GETALL, SETALL */
	struct seminfo *__buf; /* Buffer for IPC_INFO (Linux-specific) */
};

union semun semarg;
int main(int argc, char *argv[]) {
	void *shared; // the shared memory will be attached to it
	int i, j, semid, size, fd, proc = atoi(argv[2]), num_of_byte =
			atoi(argv[3]), shmid, type = 0, read_test, fdfork;
	struct sembuf sops[1]; // for the semaphore
	/* semaphore initialization */
	semid = semget(IPC_PRIVATE, 1, 0600);
	semarg.val = 1;
	semctl(semid, 0, SETVAL, semarg);
	sops->sem_num = 0;
	sops->sem_flg = 0;

	if (argc < 4) {
		perror("not enough arguments");
		exit(-1);
	}
	if (strcmp(argv[4], "signed") == 0) {
		type = 1;
	}
	fd = open(argv[1], O_RDONLY, 0600);
	if (fd == -1) {
		perror("open");
		exit(-1);
	}
	struct stat st;
	stat(argv[1], &st);
	size = st.st_size / num_of_byte;
	//create IPC memory
	if ((shmid = shmget(IPC_PRIVATE, (st.st_size + 1), 0600 | IPC_CREAT)) < 0) {
		perror("shmget error");
		exit(1);
	}
	// attach the shared memory segment to the right shared memory type

	if ((shared = shmat(shmid, NULL, 0)) == (void*) -1) {
		perror("shmat error");
		exit(1);
	}
	read_test = read(fd, shared, st.st_size);
	if (read_test == -1) {
		perror("open");
		exit(-1);
	}
	switch (num_of_byte) {
	case 8:
		if (strcmp(argv[4], "signed"))
			shared = (long long int*) &shared;
		else
			shared = (unsigned long long int*) &shared;
		break;
	case 4:
		if (strcmp(argv[4], "signed"))
			shared = (int*) &shared;
		else
			shared = (unsigned int*) &shared;
		break;
	case 2:
		if (strcmp(argv[4], "signed"))
			shared = (short int*) &shared;
		else
			shared = (unsigned short int*) &shared;
		break;
	case 1:
		if (strcmp(argv[4], "signed"))
			shared = (char*) shared;
		else
			shared = (unsigned char*) &shared;
		break;
	}

	if ((shmdt(shared)) == -1) {
		perror("shmdt error");
		exit(1);
	}
	for (i = 0; i < proc; i++) {
		fdfork = fork();
		if (fdfork == -1) {
			perror("fork");
			exit(-1);
		}
		if (fdfork == 0) {//son
			for (i = 0; i < size - 1; i++) {
				for (j = 0; j < i; j++) {
					if (shared[j] > shared[j + 1]) {
						sops->sem_op = -1; // set semaphore to lock other processes
						semop(semid, sops, 1); // LOCK the critical section
						swap(shared[j], shared[j + 1]);
						sops->sem_op = 1; // set the semaphore to allow other processes to enter the critical section
						semop(semid, sops, 1); // UNLOCK the critical section
					}
				}
			}
			exit(0);
		}
	}
	//father
	for (i = 1; i < proc; i++) // father waits for all child processes to die
		wait(NULL);
	write(1, shared, st.st_size);
	//the father merges the sorts
	// remove the shared memory segment and check if the remove was successful. if not -> exit(1)
	if ((shmctl(shmid, IPC_RMID, NULL)) == -1) {
		perror("shmctl error");
		exit(1);
	}
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: problem with *void and comperator

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 5451
  • View blog
  • Posts: 16,977
  • Joined: 25-December 09

Re: problem with *void and comperator

Posted 13 December 2010 - 03:34 PM

Please explain exactly what is wrong with your code.


If you have errors or warnings please post the entire error/warning messages.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 rafi1082  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 02-January 09

Re: problem with *void and comperator

Posted 13 December 2010 - 03:59 PM

View Postjimblumberg, on 13 December 2010 - 02:34 PM, said:

Please explain exactly what is wrong with your code.


If you have errors or warnings please post the entire error/warning messages.

Jim

sorry
first here is the exercise Attached Image
the error is
"void value not ignored as it ought to be" on line 115
and warning
dereferencing ‘void *’ pointer line 112
dereferencing ‘void *’ pointer line 115

This post has been edited by rafi1082: 13 December 2010 - 04:00 PM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: problem with *void and comperator

Posted 13 December 2010 - 04:04 PM

See also here and here.

Yeah, I'd say you've got some problems:
gcc -Werror -pedantic -std=c99 -o rafi rafi.c
rafi.c:21: error: redefinition of ‘union semun’
cc1: warnings being treated as errors
rafi.c: In function ‘main’:
rafi.c:112: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:112: warning: dereferencing ‘void *’ pointer
rafi.c:112: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:112: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:112: warning: dereferencing ‘void *’ pointer
rafi.c:112: error: void value not ignored as it ought to be
rafi.c:112: error: void value not ignored as it ought to be
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: invalid application of ‘sizeof’ to a void type
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: invalid application of ‘sizeof’ to a void type
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: invalid application of ‘sizeof’ to a void type
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: dereferencing ‘void *’ pointer
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: invalid application of ‘sizeof’ to a void type
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: dereferencing ‘void *’ pointer
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: invalid application of ‘sizeof’ to a void type
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: dereferencing ‘void *’ pointer
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: dereferencing ‘void *’ pointer
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: dereferencing ‘void *’ pointer
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: invalid application of ‘sizeof’ to a void type
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: dereferencing ‘void *’ pointer
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: dereferencing ‘void *’ pointer
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: dereferencing ‘void *’ pointer
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: invalid application of ‘sizeof’ to a void type
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: dereferencing ‘void *’ pointer
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: dereferencing ‘void *’ pointer
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: invalid application of ‘sizeof’ to a void type
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: dereferencing ‘void *’ pointer
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: dereferencing ‘void *’ pointer
rafi.c:115: warning: pointer of type ‘void *’ used in arithmetic
rafi.c:115: warning: invalid application of ‘sizeof’ to a void type



As you might be able to surmise from these errors/warnings, you cannot dereference a void pointer or use it in pointer arithmetic. A void pointer is just a pointer to a location in memory. The compiler cannot perform any sort of arithmetic because it could be pointing to any type.
Was This Post Helpful? 0
  • +
  • -

#5 rafi1082  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 02-January 09

Re: problem with *void and comperator

Posted 13 December 2010 - 04:17 PM

i know what is the problem i need a solution and sending me links from other forums where no one helped me is not helping.
if you have a solution write it else...
Was This Post Helpful? -1
  • +
  • -

#6 rafi1082  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 02-January 09

Re: problem with *void and comperator

Posted 13 December 2010 - 04:22 PM

i can write the program 8 times but i'm looking for a more sophisticated way

This post has been edited by rafi1082: 13 December 2010 - 04:30 PM

Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: problem with *void and comperator

Posted 13 December 2010 - 04:29 PM

The problem is all this:

switch (num_of_byte) {
	case 8:
		if (strcmp(argv[4], "signed"))
			shared = (long long int*) &shared;
		else
			shared = (unsigned long long int*) &shared;
		break;
	case 4:
		if (strcmp(argv[4], "signed"))
			shared = (int*) &shared;
		else
			shared = (unsigned int*) &shared;
		break;
	case 2:
		if (strcmp(argv[4], "signed"))
			shared = (short int*) &shared;
		else
			shared = (unsigned short int*) &shared;
		break;
	case 1:
		if (strcmp(argv[4], "signed"))
			shared = (char*) shared;
		else
			shared = (unsigned char*) &shared;
		break;
	}

has absolutely no effect. You don't go through that and end up with shared automatically knowing to what it's pointing.
Was This Post Helpful? 0
  • +
  • -

#8 rafi1082  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 02-January 09

Re: problem with *void and comperator

Posted 13 December 2010 - 04:42 PM

i can know easily what is pointing on but then i need to split my program into 8 parts that are doing all the same thing. isn't there a more elegant solution?
i should just declare 8 variables of char, unsigned char... and cast the void pointer and do the sorting? i'm sure the TA how gave this exercise has a trick in the design.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1