1

I found the following syntax in a c++ source file (not header):

template <typename Type_> static void aFunction(Type_ &function, ...) {
    uintptr_t value(astruct.val); //confusing
}

Is that confusing line in question a declaration for value? I tried writing a hello world program like:

int main(){ uintptr_t a(80); return 0;}

and it returns an expected declaration specifiers or ‘...’ before numeric constant, may i know what does that confusing line do? Thanks!

Edit: I think i should reveal the full function declaration:

    template <typename Type_>
static void nlset(Type_ &function, struct nlist *nl, size_t index) {
    struct nlist &name(nl[index]);
    uintptr_t value(name.n_value);// this is the confusing line
    if ((name.n_desc & N_ARM_THUMB_DEF) != 0)
        value |= 0x00000001;
    function = reinterpret_cast<Type_>(value);
}
gigasai
  • 564
  • 4
  • 23
  • What was the error? And where did `astruct` come from and what is its type? – David Schwartz Feb 27 '12 at 10:22
  • What did you expect would happen? Your code would print hello world? – CashCow Feb 27 '12 at 10:23
  • A `uintptr_t` is defined as an unsigned integer that is guaranteed to have the same size as an pointer. Did you forget to include `` (C++03)? – Zeta Feb 27 '12 at 10:27
  • The error i got was "expected declaration specifiers or ‘...’ before numeric constant." I was expecting a to be initialised to 80 or sth.. – gigasai Feb 27 '12 at 10:32
  • possible duplicate of [What is uintptr_t data type](http://stackoverflow.com/questions/1845482/what-is-uintptr-t-data-type) – CashCow Feb 27 '12 at 10:34
  • i'm quite aware of what is uintptr_t datatype, but i'm not sure what value() does or is.. :) – gigasai Feb 27 '12 at 10:36

2 Answers2

2

As you seem to know, std::uintptr_t is a standard type from the header <cstdint> (since C++11).

The line you don't understand is a simple variable declaration + definition:

uintptr_t value(name.n_value);

This declares a variable named value of type uintptr_t and initializes it with the value of name.n_value. For fundamental types, this is equivalent to:

uintptr_t value = name.n_value;

I tried writing a hello world program like:

int main(){ uintptr_t a(80); return 0;}

and it returns an error [...]

What error? Always post the error message you are getting, nobody can help you with "I got an error"!

I suspect the error you are getting is caused by the fact that uintptr_t is not defined unless you include <cstdint>, and then you should use the std namespace prefix.

Ferdinand Beyer
  • 64,979
  • 15
  • 154
  • 145
  • thanks for replying, but the error i got was: expected declaration specifiers or ‘...’ before numeric constant – gigasai Feb 27 '12 at 11:04
  • Ok, i got it, Ferdinand was right, but on top of that, I used a gcc instead of g++ to compile my simple program. Thanks for the replies! I wonder why is the c compiler throwing up at the same int a(80); syntax.. – gigasai Feb 27 '12 at 11:18
  • @LeeWangHao: Constructor-style initialization is a C++ feature and not valid C, hence you get an error when using a C compiler. – Ferdinand Beyer Feb 27 '12 at 11:50
1

uintptr_t is an unsigned integer (at least) the size of a pointer.

It is not a native type though and you need to include <stdint.h> or <cstdint>

Really it is "evil" to cast pointers to ints and store them as integral variables but there is a lot of "legacy" code that does it and relies on it, and so you need an int size big enough to store it.

Storing them as integers allows you to do things you can do with ints "safely" but not with pointers, such as compare them when they are not part of the same range, in order to use them in memory-leak checkers, etc.

CashCow
  • 30,981
  • 5
  • 61
  • 92
  • 1
    uintptr_t is an unsigned integer memsize-type that can safely store a pointer regardless of the platform capacity. The type uintptr_t is similar to the types size_t and UINT_PTR. The size of the type depends upon the data model. – steveoh Feb 27 '12 at 10:23
  • according to what standard, steveoh? uintptr_t is not a C++ standard type. – CashCow Feb 27 '12 at 10:30