0

I have a problem with sizeof(). I have a pointer in a class that I have created before. And this pointer is initialized as

 static Book* books;

so it does point nothing. I want to determine if the pointer does point any array or not. So first question is that what are the solutions to do that because I can change the address of this pointer during the runtime to point an array. I try to use sizeof(pointer) but it does not help me enough. The return is the number 4. I can evaluate in this way if it refers anything or not.

This is my class :

class Tool
{

private:
    static Book* books;

public:

    static char* pgetStringIntoArray(string);
    static string* pgetStringFromArray(char*);
    static void printCharArray(char*);
    static bool* addBook(Book*);
    static bool* isStored(Book*);
};

And this is the method having a problem :

bool* Tool::isStored(Book* book)
{
    bool* stored = new bool(false);
    if(Tool::books)
    {
        cout << "NULL"  << endl;
    } else {
        return stored;
    }

}
2
  • 1
    sizeof(books) is just a convenient alternative for sizeof(Book*). In standard C++, sizeof is always evaluated at compile time and will never actually inspect the value at runtime. It just looks at the types involved. You can even write sizeof(1/0) without getting a division-by-zero error ;) Aug 25, 2012 at 16:54
  • possible duplicate of How to find the sizeof(a pointer pointing to an array)
    – Bo Persson
    Aug 25, 2012 at 19:45

3 Answers 3

4

No, you can't use sizeof to determine if the pointer is pointing to something. sizeof a pointer will be always the same size in the same architecture.

You can do this:

Tool::Book* books = NULL;

And, if you want to check if it's pointing to a valid non-null value, just do:

if (Tool::books) {
    // books is pointing to something
}

Whenever you allocate memory for your pointer (array), if the allocation succeeds, the pointer will no longer be null. Since you are using C++, consider using std::vector to hold arrays of dynamic dimensions instead of arrays (pointers).

11
  • Thx but the compiler gives me an error : error: invalid in-class initialization of static data member of non-integral type ‘Book*’ Aug 25, 2012 at 16:26
  • Ok but now it says in the if statement this : undefined reference to `Tool::books' Aug 25, 2012 at 16:31
  • @ErayTuncer: what if statement?
    – thkala
    Aug 25, 2012 at 16:32
  • I expended the post above you can find the if statement part and it says undefined reference to `Tool::books' Aug 25, 2012 at 16:35
  • @ErayTuncer: now that we can see more code, I updated the answer. Aug 25, 2012 at 16:39
1

Much like C, C++ is statically typed - the various type sizes as returned by the sizeof operator are determined at compile time and do not change.

The proper way to do what you are asking would be to initialize your pointer to NULL and check for that afterwards. NULL is guaranteed not to refer to any valid memory address, which makes it a good marker for a non-existent pointer target. Initializing your pointers is a good practice anyway - it saves you from potential issues from dereferencing uninitialized pointers, issues that at best will result in nasty memory errors.

And yes, explicitly initializing the rest of your variables is also a good idea...

EDIT:

One not-so-minor detail: static fields in C and C++ programs are stored in a special section (e.g. BSS) of the executable and are automatically initialized to zero as the program loads. Therefore, you do not need to explicitly initialize static pointers to NULL; that has already been done for you...

1

Your problem is more with understanding pointers than with sizeof.

sizeof is a compile-time operation. For a pointer it always return sizeof(void*) (which is 8 bytes on most 64 bits architectures). Except for VLAs, sizeof returns a compile time constant.

C don't know the used size of any pointed zone -think of the result of malloc as an example- (because you could do pointer arithmetic). You need to manage it by yourself if needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.