0

I am creating a 2D array in C++. The code I use to do this is as follows

int** scopedata = new int*[1000];
 for(int i=0; i<1000; i++){
  scopedata[i] = new int[arraysize];
 }

arraysize is an int with value 400. The problem I have is that when performing the code

cout << scopedata[999][500];

The value 0 is returned. I would expect a memory address error as the 500th element should not exist, if arraysize is 400. Can anyone shed some light on this for me? Is the 2D array being created actually larger than it should be?

Adam
  • 177
  • 1
  • 8
  • It's undefined behaviour, and use a 2D matrix class such as Boost's MultiArray. – chris Jul 23 '14 at 17:32
  • 5
    Indexing past the end of an array is undefined behavior. It could do anything. – aruisdante Jul 23 '14 at 17:32
  • 1
    C++ is not a "safe" language. If you do something you're not supposed to do (*undefined behaviour*), you don't generally get a friendly notice about it. Often by the time the bug manifests (with an unfriendly error) it's in some unrelated part of the code. – Cameron Jul 23 '14 at 17:33
  • I am using the C++ code in a program called ROOT. It would be nice if there is a way to natively do this in C++ without the use of external classes if possible, as installing a third party class is quite tricky. I am not attached to the method of arrays however so if vectors are better this is possible. I tried to use vectors but couldn't get it to work. – Adam Jul 23 '14 at 17:35
  • what does *'couldn't get it to work'* mean? However note that even vectors won't throw errors if you index past the end of them using ``[]``, only if you use ``at()``. Basically, C++ is all about *performance*. Performing safety checks is slow. So many C++ primitives don't do it. They assume you'll write good code, rather than make you pay for safety checks even once you've written good code that will never violate them. – aruisdante Jul 23 '14 at 17:35
  • my C++ experience is limited, and i'm coming from a java background. I just was more drawn to the idea of a 2D array from the start because of the ease of implementation in java. Maybe i should read up more on vectors if they sound more correct for this situation. So is it safe to say that the array created in my original question will work up to 1000 columns and 400 rows? I assumed there was a problem but now it seems like maybe it is fine. – Adam Jul 23 '14 at 17:38
  • http://stackoverflow.com/a/22995901/2756719 – T.C. Jul 23 '14 at 17:38
  • `std::vector > scopedata(1000);` This will do exactly what your first line does. Where were you having trouble with the vectors? – scohe001 Jul 23 '14 at 17:39
  • 1
    @Adam - `coming from a java background` One word of advice, and please remember this: *C++ is not Java*. Don't write C++ code using Java techniques, in other words pretend Java doesn't exist. Otherwise you'll make a slew of mistakes and missteps. – PaulMcKenzie Jul 23 '14 at 17:41
  • 1
    For instance, you're using ``new`` here. If you never explicitly ``delete`` these arrays (and note you can't just do ``delete [] scopedata``, you have to loop through the first dimension calling ``delete[]`` on each subarray!) you're now leaking 4MB of memory every time this block is hit. – aruisdante Jul 23 '14 at 17:44
  • thanks for all the advice, I see now that the array was working fine it was just my expectation that was wrong. Would vectors be more suitable in this case? Why would you use a vector instead of an array? – Adam Jul 23 '14 at 17:44
  • @Adam - `std::vector > scopedata(1000, std::vector(400));` That is the complete definition. That one line creates a "2d array" with dimensions of 1000 x 400. – PaulMcKenzie Jul 23 '14 at 17:44
  • @aruisdante the array is deleted further on in the code, i thought not to include it as it was just extra code for everyone to read not directly related to my problem – Adam Jul 23 '14 at 17:45
  • @PaulMcKenzie and it will all be properly freed when it goes out of scope, or by a single ``delete scopedata`` call if allocated with ``new``. – aruisdante Jul 23 '14 at 17:45
  • 1
    @Adam a vector can be resized, a vector knows its own size when passed to a function (unlike regular arrays), and those are just two reasons out of the many to use vector over arrays. And note, your code is *not* using an array. It is using an `int**`, not a true 2 dimensional array. Yet another hurdle to overcome when learning C++, and that is to remove the false notion that pointers are *not* arrays. – PaulMcKenzie Jul 23 '14 at 17:46
  • @PaulMcKenzie yep, which will have all kinds of fun with cache-access locality, since the rows of the array could exist anywhere in memory. – aruisdante Jul 23 '14 at 17:49
  • I remember now why i stopped trying to use vectors. When using the code exactly provided by @PaulMcKenzie, i recieve an error that Function vector(400)) is not defined in vector >scopedata(1000,std – Adam Jul 23 '14 at 17:53
  • @Adam, Just to add, even using your technique, there are much better ways of creating a dynamic 2d array from an `int**`.. See here: http://stackoverflow.com/questions/21943621/how-to-create-a-contiguous-2d-array-in-c/21944048#21944048 This creates a 2d array in contiguous memory. – PaulMcKenzie Jul 23 '14 at 17:56
  • @Adam - See here. http://ideone.com/uBx4q9 Maybe you're using a compiler that needs whitespace between the ">>" characters. – PaulMcKenzie Jul 23 '14 at 18:02
  • ROOT uses the CINT compiler, i will try and see if i can find out anything about it and the vector syntax – Adam Jul 23 '14 at 18:14

0 Answers0