0

Like in title, can I somehow get size of dynamic allocated array (I can't keep it separately), or somehow loop through this array without using it's size?

int *ar=new int[x]; //x-size of array, I don't know it in the beggining, 

P.S. If I wanted to use std::vector, I wouldn't ask about it, so don't tell me to use it :)

6
  • 7
    No, you can't. You need to keep the size separately. Or keep a pointer to the end (or one past the end to follow the standard library convention). Commented Apr 24, 2015 at 12:58
  • If neither std::vector nor std::valarray, then write your own container. A hand-written RAIIed non-copyable container for POD is useful enough for 95% of the cases while as simple as 10 lines of code. Commented Apr 24, 2015 at 13:09
  • 1
    You could use a sentinel value at the end of the array like how char[] are terminated with '\0'. doing this though will take that value away from being used in the array except as an sequence terminator. Also if you are creating the array with x then you already have the size stored somewhere because x exist. Commented Apr 24, 2015 at 13:14
  • @NathanOliver not always. If size is size of some string, whcich was only used in function, i lost this size after function will end Commented Apr 24, 2015 at 13:16
  • @bingo157, Then you'd return the pointer and the size, whether it be through a pair/tuple or a reference parameter, or a struct, or whatever other means you choose. Commented Apr 24, 2015 at 13:47

3 Answers 3

2

A std::vector is designed for this.

If you can't use a std::vector I can see a couple of options.

1) Use an array terminator.

If your array should only contain positive numbers (for example) or numbers within a given range then you can use an illegal value (eg -1) as an array terminator.

for(int* i = arr; *i != -1; ++i)
{
    // do something with *i
}

2) Embed the length in the array.

For a numeric array you could, by convention, store its length in the first element.

for(int i = 0; i < arr[0]; ++i)
{
    // do something with arr[i + 1]
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to store the size your dynamic array inside it, well, just do so:

#include <iostream>
#include <cstdint>
#include <cstddef>
using std::size_t;

struct head_t { size_t size; int data[]; };

int main() {
    head_t* h = static_cast<head_t*>(::operator new(sizeof(head_t) + 10 * sizeof(int)));
    h->size = 10;
    int* my_10_ints = h->data;
    // Oh noez! I forgot 10!
    size_t what_was_10_again = static_cast<head_t*>(static_cast<void*>(my_10_ints) - offsetof(head_t, data))->size;
    ::std::cout << what_was_10_again << "\n";
    ::operator delete(static_cast<void*>(my_10_ints) - offsetof(head_t, data));
}

You can even put that functionality in a libraryesque set of functions! Oh, and once you do that you realize you could just have an unordered_map that maps pointers to sizes. But that would be like using vector: Totally boring.

Comments

0

No. That's one reason everybody uses containers. If std::vector doesn't please you, you can make a container of your own.

Edit: Since dynamic array size is determined at runtime, someone must store the size somewhere (unless you're willing to use a sentry value). Not even the compiler can help, because the size is determined in runtime.

2 Comments

Main problem of a task is to create it with maximum save of memory. If I create container, or use some int size I lost at least 4 bytes and this won't pass in this task. That's the main problem.
@bingo157 lol, you're doomed from the start. You're basically on compiler's mercy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.