Results 1 to 3 of 3

Thread: [RESOLVED] Void Pointer Function type

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Resolved [RESOLVED] Void Pointer Function type

    Hello,

    For a University assignment, I've been tasked to create a memory management Class for a program. We've been given a Class we are to design it from.

    I've basically got this all done, but we've been given a virtual function like this in a Class we must inherit:
    Code:
    virtual void* vcalloc(size_t size) {}
    This function should be able to be used as such:
    Code:
    int *someNewInt = (int*)memObj.vcalloc(sizeof(int));
    I don't understand how to do this? Can't use return, and no pointers are passed as a parameter to change. If someone can give me an example of this I would be extremely appreciative! I've Google'd around a bit, but can't find anything that has an example such as this.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Void Pointer Function type

    After messing around with it, something like this seems to work:

    Code:
    void* vcalloc(int size) { return (void *)size; }
    But I don't understand how, or if it's correct?

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Void Pointer Function type

    No unfortunately that is not correct. What you are doing is that you are casting size to a pointer type. That basically means that you are giving the caller a pointer pointing at memory that you do not own and that is very bad.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width