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

    Dynamic Array Length ?

    Hello,

    How can i change or add a cell to an array without knowing its length ? (I mean that I shouldn't know the length I need to make the length dynamic so i can add a cell)we just learned Dynamic assignment(I hope this is the correct name) and Pointers.

    Thank you

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Dynamic Array Length ?

    You would need to know the length or you could use the built in dynamic array, vector.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Dynamic Array Length ?

    Quote Originally Posted by ron_a View Post
    How can i change or add a cell to an array without knowing its length ?
    Have a look at CArray class and similar classes here
    Victor Nijegorodov

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Dynamic Array Length ?

    You can change an element of an array by knowing its position in the array. As you are just changing the contents of the element, it doesn't change the size of the array. So providing you access a valid element (and c++ doesn't provide a check for this) you need to know its length to make sure the access is valid.

    If you want to add a cell to an array, it depends upon how the array is implemented. To provide further help and guidance could you explain further what you mean by 'an array'. As you mentioned Dynamic Assigment, this seems like you are using dynamically allocated memory for the array? The STL vector is an array internally implemented via dynamically allocated memory. So using vector you can add elements to the end of the vector without knowing its size as the vector implementation deals with the dynamic memory requirements.

    Can you clarify a bit more as what to are trying to do or want to know?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: Dynamic Array Length ?

    Specifically, I think you are looking for CArray::SetAtGrow, which will dynamically enlarge the array.
    http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx
    Nobody cares how it works as long as it works

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Dynamic Array Length ?

    Ignore.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 1 of 2 12 LastLast

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