Default Size of Integral Literal Constant

1
2
3
4
5
6
7
8
9
10
void foo(void* arg)
{

}

int main(int argc, char** argv)
{
  foo((void*)1);
  return 0;
}


Is there any standard that compilers are to meet that defines the default size (as in bytes) of the integral literal constant passed to foo?
I believe the type of integral literal constants is int, so their size would be sizeof(int)
They have the option of making them any size that they want, tending towards the smaller.

Such code is dangerous simply because you cannot guarantee that the literal value is stored in any place that is usable, or that even exists.

sorry for the rought respose -- hard to type with cat in lap.
oh wait.
(void*)1 is a void pointer with a value of 1. this pointer does not point to an integer of value 1. In fact, it doesn't point to anything at all..
I think he meant foo(&1).
A good compiler should reject that anyway.

(void*)1 could very well point to something, but for user applications, such an address is usually not within their range of valid choices to access, and attempting to do so will produce an access violation.

With modern systems (and virtual addressing), the thing at (void*)1 is probably not what you think it is anyway...
I meant foo((void*)1). The reason behind it is that a callback function in an API I'm using has a void* argument, and I want to check the value of the argument. I also was to set the value of this argument through another function.

For some reason I had the misconception that the "void pointer" data type itself could be any size, but I realized it's actually always the size of a pointer, or intptr_t defined in stdint.h. So I was asking the wrong question. Ignore this thread :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void callback(void* arg)
{
  //arg would be 1, how would I check its value if <misconception>void* could be any size</misconception>
}

int main(int argc, char** argv)
{
  //void on_keypress(void (*callback_func)(void*), void* value);
  on_keypress(callback, (void*)1);

  loop();

  return 0;
}
Last edited on
If it is a void*, you have to simply know the original type and cast it to that type before you use it.
Topic archived. No new replies allowed.