Dynamic Array Length ?
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

Thread: Dynamic Array Length ?

  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
    Posts
    12,057

    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
    Wallisellen (ZH), Switzerland
    Posts
    17,301

    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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,277

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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

    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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,277

    Re: Dynamic Array Length ?

    Ignore.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center