memory of type void 
Author Message
 memory of type void

Hi all

I am using C and I wish to store my data in memory of type void.

I have intialized a void pointer and malloc some bytes to it and now i want
to store my integer and char data to that memory how can i do that?

The program below is not correct but it gives a fairly good picture of what
I want to do.

I want a big memory to be allocated as a generic memory. Then I want to
access it as and when I want to. Here I want to store a char at 10th byte
and an int at 80th.

#include<stdio.h>
void main()
{
        void *msg1;
        int a = 4;
        char b = 'a';
        msg1 = malloc(100);
        *(char)(msg1+10) = b;
        *(int)(msg1+80) = a;
        printf("\n %c , %d, \n", (char)msg1[10], (int)msg1[80]);
        return 0;

Quote:
}

Thanks
Subhek
--



Tue, 18 Oct 2005 15:27:35 GMT  
 memory of type void

Quote:

> I am using C and I wish to store my data in memory of type void.

> I have intialized a void pointer and malloc some bytes to it and
> now i want to store my integer and char data to that memory how
> can i do that?

> The program below is not correct but it gives a fairly good
> picture of what I want to do.

> I want a big memory to be allocated as a generic memory. Then I
> want to access it as and when I want to. Here I want to store a
> char at 10th byte and an int at 80th.

> #include<stdio.h>

add "#include <stdlib.h>"

Quote:
> void main()
> {
>         void *msg1;

change to "unsigned char *msg1;"

Quote:
>         int a = 4;
>         char b = 'a';
>         msg1 = malloc(100);
>         *(char)(msg1+10) = b;

           ^^^^^^
delete this.

Quote:
>         *(int)(msg1+80) = a;

here you have to be sure the alignment is correct for ints.  The
80 should be checked to be an integral multiple of sizeof(int).

Quote:
>         printf("\n %c , %d, \n", (char)msg1[10], (int)msg1[80]);

                                   ^^^^^^          ^^^^^^^^^^^^^
                                  delete this      ^
Here you need to dereference an int pointer, not convert a char to
int.  So you want something like: *(int*)(&msg1[80]).

Quote:
>         return 0;
> }

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!
--



Thu, 20 Oct 2005 00:20:26 GMT  
 memory of type void

Quote:

> I am using C and I wish to store my data in memory of type void.

There is no such thing.  It's the defining quality of "void" that it
occupies no memory whatsoever.  Thus, there cannot be memory of this
type.  Arbitrary memory, as seen by C, is of type "byte".  To be more
precise, it's of type "unsigned char".  A pointer to void doesn't
point to "memory of type void", it points to no particular type of
memory whatsoever.

Quote:
> I have intialized a void pointer and malloc some bytes to it and now i want
> to store my integer and char data to that memory how can i do that?

I think you should seriously reconsider that storage method you're
trying to use.  You're trying to do very assembler-like coding in a
non-assembler language.  While there's nothing strictly wrong with
that, you're not using quite the optimal tools for the task.  If you
want to write assembler code, _use_ assembler.

In the more civilized world of high-level programming languages,
you're supposed to do this kind of work using compound data types,
like structs, arrays, and occasionally unions.

Quote:
> void main()

main() returns int, not void.  

Quote:
>         void *msg1;
[...]
>         *(char)(msg1+10) = b;

This is wrong, in two ways.  First, pointer arithmetics is explicitly
left undefined on pointers to void.  Make msg1 a pointer to unsigned
char, and you'll be getting somewhere.  Second, the cast is wrong.
Make that

        *(char *)(msg1 + 10) = b

and you might be getting somewhere.
--

Even if all the snow were burnt, ashes would remain.
--



Thu, 20 Oct 2005 00:20:55 GMT  
 memory of type void

Quote:
>I am using C and I wish to store my data in memory of type void.

I strongly recommend learning C before attempting to do arbitrary things
in memory blocks.

Quote:
>I have intialized a void pointer and malloc some bytes to it and now i want
>to store my integer and char data to that memory how can i do that?

Carefully, so that your data is properly aligned for its type.

Quote:
>The program below is not correct but it gives a fairly good picture of what
>I want to do.

>I want a big memory to be allocated as a generic memory. Then I want to
>access it as and when I want to. Here I want to store a char at 10th byte
>and an int at 80th.

>#include<stdio.h>

Where is <stdlib.h> included?

Quote:
>void main()

Why didn't you read the FAQ *before* posting?

Quote:
>{
>        void *msg1;

The code is *much* simpler if using char pointers instead of void
pointers, because void pointers must be first converted to char pointers
if you want to do any kind of pointer arithmetic on them.

         char *msg1;

Quote:
>        int a = 4;
>        char b = 'a';
>        msg1 = malloc(100);

Where are you checking that malloc didn't return a null pointer ?!?

Quote:
>        *(char)(msg1+10) = b;

         msg1[10] = b; /* or *(msg1 + 10) = b; */

With msg1 being a void pointer, you'd have to write something like:
*((char *)msg1 + 10) = b;

Quote:
>        *(int)(msg1+80) = a;

         *(int *)(msg1 + 80) = a;

Again, with msg1 being a void pointer, one more cast would be necessary:
*(int *)((char *)msg1 + 80) = a;

But here there is an additional problem: no guarantee that the address
msg1 + 80 is properly aligned for an int access.  As it happens, 80 is a
fairly safe offset for an int.

There is no proper fix for that, in the framework of your
specification, short of using memcpy for accessing the int value.

Quote:
>        printf("\n %c , %d, \n", (char)msg1[10], (int)msg1[80]);

         printf("\n %c , %d, \n", msg1[10], *(int *)(msg1 + 80));

Quote:
>        return 0;

You're attempting to return a value from a void function!  Don't fix the
return statement, fix the function definition instead!

Quote:
>}

Dan
--
Dan Pop
DESY Zeuthen, RZ group

--



Thu, 20 Oct 2005 00:21:00 GMT  
 memory of type void

Quote:
> Hi all

> I am using C and I wish to store my data in memory of type void.

That's not quite how you go about it.  "void" literally means a lack of
something.    This is more or less consistent with it's usage in C

1)   A void return type means the function doesn't return anything.
2)  A void argument list means an empty argument list.
3)  A void pointer is a pointer to an area of memory, but the type
        of variables you can store in that memory is not specified.

Quote:

> I have intialized a void pointer and malloc some bytes to it and now i
want
> to store my integer and char data to that memory how can i do that?

    void *x = malloc(sizeof int);
    int *ix = x;                             /* points to the same memory as
x,
                                                       but we will treat
that memory as an int */
    *ix = 2;                       /* set the memory pointed to by ix to 2
*/

Quote:

> The program below is not correct but it gives a fairly good picture of
what
> I want to do.

> I want a big memory to be allocated as a generic memory. Then I want to
> access it as and when I want to. Here I want to store a char at 10th byte
> and an int at 80th.

> #include<stdio.h>
> void main()

int main()

Quote:
> {
>         void *msg1;
>         int a = 4;
>         char b = 'a';
>         msg1 = malloc(100);
>         *(char)(msg1+10) = b;

Try

         char *cmsg = msg1;
         *(cmsg + 10) = b;      /* sets the 10th byte */

Quote:
>         *(int)(msg1+80) = a;

         int *i_store_int = (int *)(cmsg + 80);
         *i_store_int = a;      /* store a with leftmost byte the 80th
                                              in the malloced memory */

         *((int *)(cmsg + 80)) = a;   /* functionally equivalent to
                                                         last two lines */

Quote:
>         printf("\n %c , %d, \n", (char)msg1[10], (int)msg1[80]);

printf("\n %c , %d, \n", cmsg[10], *i_store_int);

As a rough rule, it is better to try and make your code readable
(eg by using temporary variables named in a way that shows
what they hold).
--



Sat, 22 Oct 2005 11:59:29 GMT  
 memory of type void

Quote:

> Hi all

> I am using C and I wish to store my data in memory of type void.

> I have intialized a void pointer and malloc some bytes to it and now i want
> to store my integer and char data to that memory how can i do that?

> The program below is not correct but it gives a fairly good picture of what
> I want to do.

> I want a big memory to be allocated as a generic memory. Then I want to
> access it as and when I want to. Here I want to store a char at 10th byte
> and an int at 80th.

> #include<stdio.h>
> void main()
> {
>         void *msg1;
>         int a = 4;
>         char b = 'a';
>         msg1 = malloc(100);
>         *(char)(msg1+10) = b;
>         *(int)(msg1+80) = a;
>         printf("\n %c , %d, \n", (char)msg1[10], (int)msg1[80]);
>         return 0;
> }

> Thanks
> Subhek
> --


When you want to store a char you need a char pointer.  When you want to
store an int you need an int pointer. Without these the compiler doesn't
know how many bytes to store.  (A void pointer does not specify a
size.)  With your problem description you would need to index by bytes
to get the address of the 80th byte, but cast it to an int* in order to
store an int at that address.  BTW, the 80th byte is at index 79, not
80.

int* pi;
pi = (int*)((char*)msg1 + 79); // &80th byte
*pi = a;

--
Scott McPhillips [VC++ MVP]
--



Sat, 22 Oct 2005 11:59:35 GMT  
 memory of type void
On 02 May 2003 07:27:35 GMT, Subhek

comp.lang.c.moderated:

Quote:
> Hi all

> I am using C and I wish to store my data in memory of type void.

In C, void is an incomplete type that cannot be completed.  That means
that objects of that type can't exist.  So you can't have memory of
"type void".  You can have pointers to void, but that is only a
generic pointer, not a pointer to generic memory.

A pointer to void does not point to anything, it merely holds the
address of memory (if it is properly initialized), and that memory can
be used if the address is placed in a pointer to a real type.

Quote:
> I have intialized a void pointer and malloc some bytes to it and now i want
> to store my integer and char data to that memory how can i do that?

> The program below is not correct but it gives a fairly good picture of what
> I want to do.

> I want a big memory to be allocated as a generic memory. Then I want to
> access it as and when I want to. Here I want to store a char at 10th byte
> and an int at 80th.

> #include<stdio.h>
> void main()

Actually, with the line above it is not really a C program.  The C
language requires that main() be defined with a return type of int in
a hosted environment.  If you start a program with "void main()", the
C standard does not specify anything about what it will do.

Quote:
> {
>         void *msg1;
>         int a = 4;
>         char b = 'a';
>         msg1 = malloc(100);
>         *(char)(msg1+10) = b;

No, this won't work, the cast you need is:

          *((char *)msg+10) = 'b';

Of course, since C arrays start at 0, this is the 11th byte, not the
10th byte in the array.

Quote:
>         *(int)(msg1+80) = a;

This one is even nastier, because you need to do the addition to the
80th byte on type pointer to char, so it won't be scaled by
sizeof(int)...

         *(int *)((char *msg1)+80)

Again, since arrays start at 0, this is the 81st byte in the array,
and it might not be properly aligned for an int.  Even the 80th byte
might not be properly aligned for an int.  So on some architectures
this program could crash.

Quote:
>         printf("\n %c , %d, \n", (char)msg1[10], (int)msg1[80]);
>         return 0;
> }

> Thanks
> Subhek

The proper, safe, and portable way to do this is to use memcpy().  It
also makes life easier to allocate your memory to a pointer to one of
the character types, so you can directly do the pointer arithmetic.

Assuming you change the definition of msg1 to pointer to
(plain/signed/unsigned) char, then storing values is simple (assuming
<string.h> is included):

    memcpy(msg1 + 10, &b, 1);

....and:

   memcpy(msg1 + 80, &a, sizeof a);

No worries about alignment, either.  To retrieve the values, memcpy()
them back into ordinary objects of the appropriate type.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
--



Sat, 22 Oct 2005 11:59:56 GMT  
 memory of type void
On 02 May 2003 07:27:35 GMT, Subhek

Quote:

>Hi all

>I am using C and I wish to store my data in memory of type void.

>I have intialized a void pointer and malloc some bytes to it and now i want
>to store my integer and char data to that memory how can i do that?

>The program below is not correct but it gives a fairly good picture of what
>I want to do.

>I want a big memory to be allocated as a generic memory. Then I want to
>access it as and when I want to. Here I want to store a char at 10th byte
>and an int at 80th.

>#include<stdio.h>
>void main()
>{
>        void *msg1;
>        int a = 4;
>        char b = 'a';
>        msg1 = malloc(100);

If malloc succeeds, the value in msg1is guaranteed to be aligned
properly for any (or every) type.  But you really ought to check.

Quote:
>        *(char)(msg1+10) = b;

Probably a new record for error density.

     msg1 one is a pointer to void.  void is not a type and therefore
does not have a size.  Consequently, you cannot do pointer arithmetic
on it.

     If you could, the result would still be a pointer.  You then cast
that pointer value to char.  Odds are that any useful pointer value
will not fit in a char.

     Then you attempt to dereference the char.  Dereferencing can only
be applied to a pointer.

I think what you wanted was
     *((char*)msg1+10) = b;
or possibly the equivalent
     ((char*)msg1)[10] = b;

Quote:
>        *(int)(msg1+80) = a;

Ditto except now the cast is int*.

But you also have another problem.  You allocated 100 bytes of space.
You are trying to store into the 80th int in that space.  On many
system, an int is at least four bytes.  In order to do this, you would
need to allocate at least 324 bytes.

Quote:
>        printf("\n %c , %d, \n", (char)msg1[10], (int)msg1[80]);

This has the same pointer arithmetic problem.  [] has higher
precedence than the cast.  Your second operand is parsed as
(char)(msg1[10]).  msg1 is still a pointer to void.  msg1[10] is
defined by the language to be equivalent to *(msg1+10).  As before you
cannot perform arithmetic on a pointer to void.  Even if you, you are
not allowed to dereference a pointer to void since void is not a type
that the expression can evaluate to.  You need to use one of the
expressions I described above.

Quote:
>        return 0;
>}

<<Remove the del for email>>
--



Sat, 22 Oct 2005 12:00:02 GMT  
 memory of type void

Quote:

> I am using C and I wish to store my data in memory of type void.

There are no objects of type "void" in C.

Quote:
> I want a big memory to be allocated as a generic memory.

 > Then I want to access it as and when I want to.

Make it an array of type unsigned char.
--



Sat, 22 Oct 2005 12:00:07 GMT  
 memory of type void

Quote:

> Hi all
> I am using C and I wish to store my data in memory of type void.
> I have intialized a void pointer and malloc some bytes to it and now i want
> to store my integer and char data to that memory how can i do that?
> The program below is not correct but it gives a fairly good picture of what
> I want to do.
> I want a big memory to be allocated as a generic memory. Then I want to
> access it as and when I want to. Here I want to store a char at 10th byte
> and an int at 80th.
> #include<stdio.h>

Since you're using malloc() you also need

#include <stdlib.h>

Quote:
> void main()

main() is always supposed to return an int (which you seem to know
because you actually return an int), so make that

int main( void )

Quote:
> {
>         void *msg1;
>         int a = 4;
>         char b = 'a';
>         msg1 = malloc(100);
>         *(char)(msg1+10) = b;

This is wrong for several reasons. First you can't do pointer arithmetic
with void pointers, so already "msg1+10" won't work. void has no size
in the sense that the compiler could calculate how much it has to add
to 'msg1' when you ask it to add 10 to it (if 'msg1' would be a char
pointer the compiler knows it has to add 10, if it would be an int pointer
the compiler would add 10 * sizeof(int) to the address etc.). And even
if this would do what you seem to expect it to do, the cast would would
convert the result to a char, truncating the value if necessary (e.g.
on systems with CHAR_BIT set to 8 to an 8-bit value). And only then it
would take this truncated value as the address into which you try to
store the value of 'b'. What you need is something like

    * ( ( char * ) msg1 + 10 ) = b;

This way you tell the compiler to treat msg1 as a char pointer (the
smallest addressable unit of memory), so that you can do pointer
arithmetic with the value and then store the value of 'b' in the
location 10 chars after where msg1 is pointing to. BTW, do you realize
that this will put the value into the 11th byte (if a char has byte
size on your system) of the malloced memory? If you want to put it
into the 10th you must only add 9 to msg1 (after casting it to char *).

Quote:
>         *(int)(msg1+80) = a;

Same problem here. But there are some further possible problems.
You might try to use

   * ( ( int * ) ( ( char * ) msg1 + 80 ) ) = a;

i.e. converting the char pointer "( ( char * ) msg1 + 80 )" to an int
pointer before storing the value of 'a' in what it points to. But this
is not without dangers, since the pointer might not be correctly aligned
for an int: on some systems integers can only be stored e.g. at even
memory localtions or even at addresses dividable by 4. With 80 this
probably isn't going to be a problem but when you would use 81 instead
your program might die with e.g. a SIGBUS signal (or whatever your
operating system does in these cases). To be on the save side
you should use memcpy for operations involving copying more than a
single char, i.e.

    memcpy( ( char * ) msg1 + 80, &a, sizeof a );

Quote:
>         printf("\n %c , %d, \n", (char)msg1[10], (int)msg1[80]);

The msg1[10] bit will again get the compiler upset because it invokes
pointer arithmetic on avoid pointer because the compiler will replace
"msg1[10]" with "*(ms1 + 10)". The same holds, of course, for the
"msg1[80]", but with the additional possible problem with alignment.
So you need at least

print( "\n %c , %d, \n", *( ( char * ) msg1 + 10 ),
       ( int ) ( ( char * ) msg1 + 80 ) );

To be on the save side regarding printing the integer you should
memcpy() it back into a real integer before trying to print it to
avaoid problems with alignment.

As you see you always will have to convert the void pointer to a
char pointer anyway. So in the end it will probably be simpler
(and easier to read) when you make msg1 a char pointer instead of
a void pointer.

Quote:
>         return 0;

It is probably a good idea to get in the habit of using the macros
EXIT_SUCCESS or EXIT_FAILURE to be sure to return a value that will
be interpreted correctly as flagging success or failure on all
systems.
                                    Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |
 | |_| |  | |    | |          http://www.physik.fu-berlin.de/~toerring
  \___/ens|_|homs|_|oerring
--



Sat, 22 Oct 2005 12:00:14 GMT  
 memory of type void
Hi all

Thanks for your replies.

It seems I was not able to put up my problem clearly.
I dont want a debug version of the code below.

The question is how can I have a memory block (not of any particular
datatype) with a starting address and offsets. and how can I can use
it in the program.

Anyways I have found a work around.

Thanks and Regards

Subhek

P.S. : a note to the moderator:: I was not able to cancel this
article, please do it for me.

Quote:

> Hi all

> I am using C and I wish to store my data in memory of type void.

> I have intialized a void pointer and malloc some bytes to it and now i
> want to store my integer and char data to that memory how can i do that?

> The program below is not correct but it gives a fairly good picture of
> what I want to do.

> I want a big memory to be allocated as a generic memory. Then I want to
> access it as and when I want to. Here I want to store a char at 10th byte
> and an int at 80th.

> #include<stdio.h>
> void main()
> {
>         void *msg1;
>         int a = 4;
>         char b = 'a';
>         msg1 = malloc(100);
>         *(char)(msg1+10) = b;
>         *(int)(msg1+80) = a;
>         printf("\n %c , %d, \n", (char)msg1[10], (int)msg1[80]);
>         return 0;
> }

> Thanks
> Subhek

--



Mon, 24 Oct 2005 11:15:23 GMT  
 memory of type void

Quote:
>The question is how can I have a memory block (not of any particular
>datatype) with a starting address and offsets. and how can I can use
>it in the program.

By treating it as an array of bytes (aka unsigned char).

If you get it from malloc(), it's aligned for storing any object *at the
beginning*, but this guarantee is lost if you want to store an object
of a certain type at an offset.

Dan
--
Dan Pop
DESY Zeuthen, RZ group

--



Tue, 25 Oct 2005 07:03:56 GMT  
 memory of type void
Quote:


>>The question is how can I have a memory block (not of any particular
>>datatype) with a starting address and offsets. and how can I can use
>>it in the program.

> By treating it as an array of bytes (aka unsigned char).

Can we always say that unsigned char will take 1 byte ? (I guess not)
--



Wed, 02 Nov 2005 11:35:04 GMT  
 memory of type void

Quote:

> Can we always say that unsigned char will take 1 byte ? (I guess not)

The C Standard defines things so that, by definition, a "byte" is a
char/unsigned char/signed char.  This may or may not literally be an
octet.

You are assured that sizeof(char) == 1.

--

 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ War is the province of chance.
\__/  Karl von Clausewitz
--



Sat, 05 Nov 2005 04:53:34 GMT  
 memory of type void

Quote:

> Can we always say that unsigned char will take 1 byte ? (I guess not)

You guess wrong.  However, the "byte" as defined for purposes
of the C standard could be more than 8 bits wide.  Its width
is specified in the CHAR_BIT macro defined by the implementation.
--



Sat, 05 Nov 2005 04:53:42 GMT  
 
 [ 30 post ]  Go to page: [1] [2] [3]

 Relevant Pages 

1. void pointer (void *) : how to determine type?

2. from void (void*) to void (_cdecl*) (void*)

3. HRESULT return type turns into void when adding class from type library

4. what's the difference between void main(void) and int main(void)

5. difference between void foo(void) and void foo()

6. void* type

7. void type pointers

8. can char ** be passed to void * type?

9. type casting malloc() to void *

10. Int86: Error in C ANSI: (DOS.H : Value of type void is not allowed)

11. void return type

12. Type of void * p[10]

 

 
Powered by phpBB® Forum Software