CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Mar 2011
    Posts
    16

    Arrow dynamic array allocation

    vector 'delete' knows the amount of memory to free by the count that 'new' inserts just ahead of the allocated block while returning the reference to the beginning of the next word boundary after this...i'm trying to take advantage of this to not rely on '\0' as a termination marker to a c-style string and have embedded '\0' just like the facilities of std::string...more importantly, work with numeric arrays without passing their size..

    so for "int* a=new int[x];" i'm trying to use "*(a-1)" ... but it's neither returning the (number of elements) nor (number of elements)*sizeof(element) though each time something close to the second value...for instance if x=10 then the returned value is in the range [45 to 49] ...

    am i doing/interpreting something conceptually wrong?

  2. #2
    Join Date
    Oct 2010
    Posts
    60

    Re: dynamic array allocation

    Well, a is a pointer to the first element in the list, right? So a-1 is one before the beginning of the list, and garbage is stored there. I think you mean *(a+x), which would give the last element in the list. You can't figure it out with a alone.
    EDIT: Did not know about the size stored before the block. Maybe some invisible overhead?
    Last edited by henryswanson; March 28th, 2011 at 06:20 PM.

  3. #3
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: dynamic array allocation

    You could use a std::vector<chartype> as your string. No problem with embedded nulls but doesn't have strings rich interface. Never rely on new and delete to be implemented in any particular way. Because vectors use contiguous memory the vector<chartype> is compatible with C strings and if you are careful in some instances can be treated as one. Reading is always fine as long as its not being modified elsewhere. modifying can be dangerous without care.
    Why would you want to work with numeric arrays without knowing the size? Due to arrays decaying to pointers on passing as a parameter, the size becomes important information to be passed too. Nowadays you should prefer to avoid bare arrays anyway preferring std::vector or std::array or boost::array.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: dynamic array allocation

    Quote Originally Posted by ustulation View Post
    vector 'delete' knows the amount of memory to free by the count that 'new' inserts just ahead of the allocated block while returning the reference to the beginning of the next word boundary
    Please tell us where you got this information, since it isn't correct. That isn't the case for all compilers.

    http://www.parashift.com/c++-faq/fre...html#faq-16.14

    so for "int* a=new int[x];" i'm trying to use "*(a-1)" ... but it's neither returning the (number of elements) nor (number of elements)*sizeof(element) though each time something close to the second value...for instance if x=10 then the returned value is in the range [45 to 49] ...
    When you allocate memory, you don't know where or how that number is stored. You're not supposed to know, as that is implementation defined.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 28th, 2011 at 06:25 PM.

  5. #5
    Join Date
    Mar 2011
    Posts
    16

    Re: dynamic array allocation

    Quote Originally Posted by Paul McKenzie View Post
    Please tell us where you got this information, since it isn't correct. That isn't the case for all compilers.
    Bjarne's book..the c++ programming language spc edition Pg., 128....it's very subtle the way its mentioned there (as everything in that book is )...then to fully understand i'd to google further...see:
    http://blogs.msdn.com/b/oldnewthing/.../03/66660.aspx

    You're not supposed to know, as that is implementation defined.
    if it's known at runtime then we should too ...it lets us take so many advantages...how do u use an integer array in a function without passing the size :

    void f(int* arr) {/* how do u use arr now? */}

  6. #6
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: dynamic array allocation

    Though personally I still fail to see why its important that you dont pass the size, the simple answer is to use a vector. A vector knows its size and this can be queried with the size() member function. The vector has the advantage too of managing its own memory taking a job off your hands which you would otherwise have had to do yourself when you used new[].
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  7. #7
    Join Date
    Mar 2011
    Posts
    16

    Re: dynamic array allocation

    Quote Originally Posted by Russco View Post
    Though personally I still fail to see why its important that you dont pass the size, the simple answer is to use a vector. A vector knows its size and this can be queried with the size() member function. The vector has the advantage too of managing its own memory taking a job off your hands which you would otherwise have had to do yourself when you used new[].
    i understand and agree to your point completely...i just want to know what's happening ... i'v gone into this b-coz one of my Qt programs kept crashing and was a pain to debug it since i'm not a prolific coder (not my profession) ... found out later that i's using delete[] on a scalar pointer along with several vector pointers (not the std::vector ofcourse)...it struck me then...and bjarne wrote this in one such concise statement in a way as if it was intended that everybody missed it ... then i googled for it out of curiosity...

    btw i'v some finding on this... i see that every time the size comes out to be 9 more bytes than the actual size number of bytes allocated...and those places are initialized to zero...if i free memory within (the size of my array)+(9bytes more) range, things are fine...moment i go further the program crashes randomly...can u use try it in your systems and see if it's consistent this way for array sizes over 9?

  8. #8
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: dynamic array allocation

    You just do not do what you are trying to do with pointers and new ever. Looking after the bookkeeping is up to the compiler and relying on specific implemetation defined behaviour will eventually bite your arm off. What bjarne is telling you is how new CAN be implemented. Its for waffle and interest only. Its not behaviour mandated by the standard and it can not be relied upon. You do not own the memory allocated before your block. Dont go poking around in it.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  9. #9
    Join Date
    Mar 2011
    Posts
    16

    Re: dynamic array allocation

    ok..will avoid it..cheers !!

  10. #10
    Join Date
    Aug 2007
    Posts
    858

    Re: dynamic array allocation

    Quote Originally Posted by ustulation View Post
    if it's known at runtime then we should too ...it lets us take so many advantages...how do u use an integer array in a function without passing the size :

    void f(int* arr) {/* how do u use arr now? */}
    You pass the size. Or you encapsulate the array in an object that holds the size for you, like std::vector or std::tr1::array.

    If you want to play with things like this, none of us can stop you, but all you're going to learn is how to write code that only works for one compiler.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: dynamic array allocation

    Quote Originally Posted by ustulation View Post
    btw i'v some finding on this... i see that every time the size comes out to be 9 more bytes than the actual size number of bytes allocated...and those places are initialized to zero...if i free memory within (the size of my array)+(9bytes more) range, things are fine...moment i go further the program crashes randomly...can u use try it in your systems and see if it's consistent this way for array sizes over 9?
    How do some runtime and third-party allocators know when you overwrite memory? The runtime overallocates, making the extra bytes guard-bytes. If those guard bytes are changed when you deallocate the memory, then the runtime knows you have done something wrong.

    So I'll ask you a trick question: How many bytes are allocated with the following statememt:
    Code:
    char *p = new char[10];
    If you say 10 bytes, you are wrong. The correct answer is at least 10 bytes. Nothing stops a debugging version of the runtime to allocate more memory for the purposes I mentioned above.

    So what you are trying to do is equivalent of a dog chasing its own tail. You'll keep running around, chasing magic numbers and trying to write code, and then you get tripped up again somewhere.

    The bottom line is that a runtime library need not allocate exactly x bytes if you ask for x bytes. It can allocate x bytes, plus any number of other bytes for houskeeping, debugging, or for any other purpose the runtime deems fit.

    Also, you say you called delete[] on a scalar, and that is what made you search for these answers -- welcome to the world of C++ programming. All of us here have made errors when handling dynamically allocated memory. That is the contract you make with this language when you write programs that use new/delete. You are responsible for making sure all memory handling is done correctly.

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: dynamic array allocation

    Quote Originally Posted by ustulation View Post
    if it's known at runtime then we should too ...it lets us take so many advantages...how do u use an integer array in a function without passing the size :

    void f(int* arr) {/* how do u use arr now? */}
    You pass the size. That is how the language has been since the beginning, and since K&R C programming. That's why std::vector and std::array exists in C++, so that you get the size as part of the entire package. A pointer is just a dumb variable -- it knows nothing except that it points to something. It has no special powers beyond that.

    Secondly, if your discovery was sound, why do products such as BoundsChecker, Purify, and valgrind still exist?

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    May 2009
    Posts
    2,413

    Re: dynamic array allocation

    Quote Originally Posted by ustulation View Post
    ok..will avoid it..cheers !!
    Avoiding isn't good enougth.

    You must promise to never ever again even consider making use of C++ language implementation detail ... or else ... or else. Or else you'll be sentenced to using Java for the rest of your natural life.

  14. #14
    Join Date
    Mar 2011
    Posts
    16

    Re: dynamic array allocation

    Quote Originally Posted by nuzzle View Post
    Avoiding isn't good enougth.

    You must promise to never ever again even consider making use of C++ language implementation detail ... or else ... or else. Or else you'll be sentenced to using Java for the rest of your natural life.
    HAHAHAHAHA....thank u all...it's from valuable comments of practical programmers like you that others learn !!

  15. #15
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: dynamic array allocation

    Quote Originally Posted by ustulation View Post
    Bjarne's book..the c++ programming language spc edition Pg., 128....it's very subtle the way its mentioned there (as everything in that book is )...then to fully understand i'd to google further...see:
    http://blogs.msdn.com/b/oldnewthing/.../03/66660.aspx
    I think that it is great that you searched for more information and then got that article, but next time remember to read what you find closely, e.g., you should have seen:
    Here's how the Microsoft C++ compiler manages vector allocation. Note that this is internal implementation detail, so it's subject to change at any time, but knowing this may give a better insight into why mixing scalar and vector new/delete is a bad thing.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Page 1 of 2 12 LastLast

Tags for this Thread

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

Featured