0

Is there anyway to make a 1D dynamic and continuous array using C++11?

I will take in the size of the array via stdin and once it's created it won't need to be resized.

Right now I am using a 1D vector of enums, and my biggest performance issue is the vector [] operator.

If it can't be done in C++, I am open to ways of doing it with malloc and C, just please note the best way to delete it.

Edit: Didn't realize vectors were night and day with debug and release. I reran callgrind with -O3 and now the issue is ostream not vectors - thanks to everyone who made me second guess myself before I rewrote it all using arrays when not needed.

3
  • 2
    That operation on a vector is generally not more costly than an index into an array. Commented Feb 2, 2014 at 6:56
  • Is the issue that you are using enums? Why are you not using regular integers? Commented Feb 2, 2014 at 7:00
  • I am using enums for readability, it's a 1 byte enum and I do bitwise operations on it. Commented Feb 2, 2014 at 7:03

5 Answers 5

3
    int size = 10;
    std::unique_ptr<int[]>  a(new int[size]);

Follows RAII (that is array is autodestructed) However I don't think vector [] operator should be performance issue. In debug compilation it may be checked, however in release it should not.

In MSVC there is feature called checked iterators which may "kill" performance. However you're able to switch this feature off (just google it)

Sign up to request clarification or add additional context in comments.

Comments

3

You can always dynamically create contiguous homogeneous storage of a certain type from the heap using the new operator

Type *pType = new Type[size](initial_value)

In Order to delete the storage, you need to explicitly invoke the array delete operator

delete[] pType

But, when you say, and my biggest performance issue is the vector [] operator., I doubt. Did you profile the retail version of your code? How do you know the vector subscript is your performance bottleneck?

1 Comment

+1 "I doubt. Did you profile the retail version of your code?"
2

std::vector has enough performance to use in production. It is used for solving problems on programming contests. Maybe, you forget to compile into release?

Also, you can use new and delete operators.

To free allocated memory use free(void *ptr) function.

Comments

0

In C you create a 1-D array with

int *ptr=NULL;
int num_elements = 0;
printf("how many elements?\n");
if(scanf("%d", &num_elements)==1) {
  ptr = malloc(sizeof(int)*num_elements);
  if(ptr == NULL) {
    printf("unable to allocate %d elements\n", num_elements);
    exit 0;
  }
} 

and when you're done, you call

free(ptr);

Make sure you call it only once! Some people will do

if( ptr != NULL ) {
  free(ptr);
  ptr = NULL;
}

as defensive coding - prevents accidentally freeing it twice.

4 Comments

the if block is unnecessary - freeing a null pointer is totally okay.
@AndreasGrapentin see stackoverflow.com/questions/1938735/… - "it was not always so". I recall running into crashed on freeing NULL (Borland C on PC in early 90's maybe). I suspect the "do nothing on free(NULL) standard wasn't always there.
the accepted answer in your linked question seems to support my point :)
@andreas - yes. But further down there are other observations... We both have a point but you are absolutely correct about the standard.
0

If you are using C++, using malloc is probably a poor (inferior) idea.

int length;
cin >> length;

MyType * array = new MyType[length];

Then to delete,

delete[] MyType;

Comments

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.