LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-15-2004, 05:23 PM   #16
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59

INT, CHR, and FLT are all ints. enum just automatically numbers them for you.

I could just as easily have done:

#define INT 0
#define CHR 1
#define FLT 2

or:

int INT=0;
int CHR=1;
int FLT=2;

(At least I hope enum starts the numbering at 0; it might start at 1. Either way it's irrelevant as long as each has its own unique value )

Now that I think about it, that header code might not work on a different endian machine. It would work with the #define method or if I defined each one as a char though

So anyway, INT/CHR/FLT are just labels. They all have the same type and the value of each of them can fit into the 1-byte header (values = 0, 1, 2).
 
Old 01-15-2004, 05:32 PM   #17
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Oops. My reply was hiding on the 2nd page so I didn't think it went through. Moderators can delete this reply

Last edited by itsme86; 01-15-2004 at 05:34 PM.
 
Old 01-15-2004, 05:45 PM   #18
xailer
Member
 
Registered: Nov 2003
Posts: 77

Original Poster
Rep: Reputation: 15
hi

So we assume that INT is one byte long ? Ok , but how does pointer know how many bytes an element is long so it can move address correctly ( see my previous question for more details ) ?

thank you
 
Old 01-15-2004, 06:12 PM   #19
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Inside the fc() function in the type-dependent code, if the type is INT then it knows the next sizeof(int) bytes will be the integer.

So you could do:
Code:
case INT:
  i = (int)(ptr+1);
Each type identifier (the first byte of ptr) will always only take 1 byte. The entire length of the ptr will be 1+<data length> bytes long to accommodate the 1 byte header.

So you can assume that if the type is INT then ptr will be 1+sizeof(int) bytes long, if FLT then ptr will be 1+sizeof(float) bytes long, and if CHR then ptr will be 1+strlen(str)+1(for \0) bytes long You don't need to know the length of the string ahead of time since we null-terminated it.) You could simply do:

Code:
case CHR:
  strcpy(buf, (char *)(ptr+1));
Check out the memory arrangements in my first post.

Last edited by itsme86; 01-15-2004 at 06:15 PM.
 
Old 01-15-2004, 06:54 PM   #20
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Here's a program that I just wrote for this and actually tested!

Here it is in its entirety:

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

enum { INT, CHR, FLT };

void *fc(void *ptr)
{
  switch(*(char *)ptr)
  {
    case INT:
      printf("int: %d\n", *(int *)(ptr+1));
      break;
    case FLT:
      printf("float: %f\n", *(float *)(ptr+1));
      break;
    case CHR:
      printf("chr: %s\n", (char *)(ptr+1));
      break;
    default:
      printf("Unknown type: %d\n", *(char *)ptr);
  }

  return NULL;
}

int main(void)
{
  void *ptr;

  ptr = malloc(sizeof(int)+1);
  *(char *)ptr = INT;
  *(int *)(ptr+1) = 86;
  fc(ptr);
  free(ptr);

  ptr = malloc(sizeof(float)+1);
  *(char *)ptr = FLT;
  *(float *)(ptr+1) = 3.14;
  fc(ptr);
  free(ptr);

  ptr = malloc(strlen("Hello, world!")+2);
  *(char *)ptr = CHR;
  strcpy(ptr+1, "Hello, world!");
  fc(ptr);
  free(ptr);

  ptr = malloc(1);
  *(char *)ptr = 123; // Bogus type identifier
  fc(ptr);
  free(ptr);

  return 1;
}
Here's the output I get (and should get) when I run it:

int: 86
float: 3.140000
chr: Hello, world!
Unknown type: 123

I really hope that clears things up. If you have any more questions please ask. I love talking about C programming
 
Old 01-15-2004, 07:08 PM   #21
xailer
Member
 
Registered: Nov 2003
Posts: 77

Original Poster
Rep: Reputation: 15
hi

I think you missunderstood the question .

Quote:
Each type identifier (the first byte of ptr) will always only take 1 byte. The entire length of the ptr will be 1+<data length> bytes long to accommodate the 1 byte header.

So you can assume that if the type is INT then ptr will be 1+sizeof(int) bytes long, if FLT then ptr will be 1+sizeof(float) bytes long, and if CHR then ptr will be 1+strlen(str)+1(for \0) bytes long You don't need to know the length of the string ahead of time since we null-terminated it.)
That I understand

My question is how does pointer know where to point next . When you give (ptr+1) instruction , pointer must point to address that is away from the previous address by a number of bytes .How much bytes away it is ,depends on the element that pointer points to .If it is integer then pointer will move 4 bytes away and if it is char only 1 byte away . But the point is that pointer must know to which element it points to .

Code:
case CHR:
  strcpy(buf, (char *)(ptr+1));
My question is how does pointer in the above example know that it must move only one byte away , since afterall it is generic pointer ?

thank you

Last edited by xailer; 01-15-2004 at 07:10 PM.
 
Old 01-15-2004, 08:12 PM   #22
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
C always increments pointers based on its type. void pointers always increment by 1. So ptr+1 means to add 1 byte. Whereas if you had an 'int *ptr;' and did ptr+1 it would move sizeof(int) bytes (usually 4).

Let's assume ptr is at address 0000 for the following:

void *ptr;
printf("%d + 1 = %d", ptr, ptr+1);

Should show: 0 + 1 = 1
But it was an int pointer it would be different:

int *ptr;
printf("%d + 1 = %d", ptr, ptr+1);

Should show: 0 + 1 = 4

Is that what you were asking?
 
Old 01-16-2004, 06:39 AM   #23
xailer
Member
 
Registered: Nov 2003
Posts: 77

Original Poster
Rep: Reputation: 15
hi

Yes , that was my question . I didn't know void * pointers increment by 1 byte . I should ask more clearly , but it was pass midnight and my english sucks

Thank you for taking the time

bye
 
Old 01-16-2004, 02:14 PM   #24
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
Just thought I would point out that most system functions that return void pointers do so because they deal with data in bytes, and thus never have to worry about it's `type'. Also, the sizeof any pointer is going to be the same, it is a memory address. The idea with using void pointers is, as stated previously, to transparently deal with any data type - you should then be dealing with the memory in bytes, effectively. Malloc, for example, one of the most famous (void *) returning functions takes it's unsigned integer arguments as the number of bytes you want copied. You are expected to do the book keeping to then cast it back (implicitly or explicitly) when you are using it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between function declaration: func() and func(void) koyi Programming 4 11-19-2005 10:19 AM
"dereferencing `void *' pointer" but how else?!? f0rmula Programming 6 03-24-2005 05:32 AM
void pointer help gonnaWorkItOut Programming 1 10-12-2003 11:52 AM
c++ Pointer to Member Function Wondre Programming 0 02-15-2003 06:12 PM
g++ delete [] void pointer? ugenn Programming 5 06-01-2002 03:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration