Thread: Vector doesn't init this way....

  1. #1
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158

    Vector doesn't init this way....

    I was dynamically creating a struct using new, this struct had a vector in it. The vector worked fine. Now I'm dynamically creating the struct by using GlobalAlloc, but the vector doesn't init like it's suppose to, now, I know why this is the case, but I don't know how to manually init vectors. Is it even possible? What do I do at this point?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Could you perhaps call the constructor directly?

    [edit] It seems that you can call a destructor directly, if you really wanted to, but I can't see how you could call a constructor directly. Perhaps you could use new for that part of the structure? e.g.
    Code:
    struct data {
        vector<int> *v;
        int x, y, z;
        /* ... */
    };
    data x = GlobalAlloc();  // I don't know how to use this function
    x->v = new vector<int>;
    [/edit]
    Last edited by dwks; 07-09-2008 at 03:46 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Perhaps you need to call the constructor using placement new?

    If I'm not mistaken GlobalAlloc doesn't call the constructor of the struct (even if it is generated by the compiler it would call the constructor of the vector). You'll probably need to treat its return value as raw memory.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    http://www.parashift.com/c++-faq-lit....html#faq-10.3
    BTW do NOT try to achieve this via placement new. Some people think they can say new(this) Foo(x, int(x)+7) within the body of Foo::Foo(char). However that is bad, bad, bad. Please don't write me and tell me that it seems to work on your particular version of your particular compiler; it's bad. Constructors do a bunch of little magical things behind the scenes, but that bad technique steps on those partially constructed bits. Just say no.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, something along the lines of:

    Code:
    struct data {
        vector<int> *v;
        int x, y, z;
        /* ... */
    };
    data x = GlobalAlloc();  // I don't know how to use this function
    new (x) data; // Placement new
    Should initialize it properly before you start venturing further.
    To my knowledge, only new calls constructors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why do you need to use GlobalAlloc like that? Would it make more sense to override the operator new() for that class, and inside that call GlobalAlloc?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Yarin View Post
    Now I'm dynamically creating the struct by using GlobalAlloc, but the vector doesn't init like it's suppose to, now, I know why this is the case, but I don't know how to manually init vectors.
    Well I can't say I haven't needed to do something like that myself before, but I do need to ask why you would use GlobalAlloc instead of new in this instance. You won't be able to just pass the whole thing to something else that uses GlobalFree on it anyway, since that would leak the vector's contents. You can't marshall it via COM etc. You really must seriously question yourself whether it is the right thing to do. Please explain.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Elysia View Post
    Yes, something along the lines of:

    Code:
    struct data {
        vector<int> *v;
        int x, y, z;
        /* ... */
    };
    data x = GlobalAlloc();  // I don't know how to use this function
    new (x) data; // Placement new
    Should initialize it properly before you start venturing further.
    To my knowledge, only new calls constructors.
    More precisely, it should be
    Code:
    void *mem = GlobalAlloc(...);
    data *x = new (mem) data;
    There are a few reasons for that.
    One, the return value of GlobalAlloc points to uninitialized memory, not a data object, so you shouldn't pretend it does.
    Two, you need to do it that way, since placement new isn't actually guaranteed to return the same address it was passed. That is, the single-object variant is, but the array variant is not. For consistency, make array and single-object placement new look the same.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    I am doing this because I want my c++ code to be manageable through c, and c doesn't support the new and delete ops. And I read that you should never free memory using an operator/function that aren't directly paired. (I.E. don't free malloc with delete etc.)

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    So you can use std::vector, but you cannot use new/delete?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    I got the solution. Now I'm just having the C code call imported C++ to do that for me (the vector was created in the imported C++ anyway)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 12:42 PM
  3. Array of structures in a class, init
    By seizmic in forum C++ Programming
    Replies: 13
    Last Post: 09-17-2005, 01:12 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. How to init an array dynamically on the heap?
    By karantsch in forum C Programming
    Replies: 6
    Last Post: 01-25-2003, 01:07 AM