[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
I just realised that int Array[] = new Array[99]; doesn't
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /r9k/ - ROBOT9001

Thread replies: 18
Thread images: 4
File: need help.png (32 KB, 633x758) Image search: [Google]
need help.png
32 KB, 633x758
I just realised that int Array[] = new Array[99]; doesn't create 0 to 99 different positions (100 in total) but rather creates only 99 different positions that start at 0 and to go to 98.

I've been "programing" for almost a year now.
>>
File: room of people who care.jpg (32 KB, 600x450) Image search: [Google]
room of people who care.jpg
32 KB, 600x450
>>27450741
mega original the comment
>>
>>27450784
About you, maybe. Lol.
>>
>>27450784
I don't care. I need to put my day into words so I can work on my communication skills.

And besides, a single (you) was worth it.
>>
Which language? c++? If c++, then that might have actually "worked" (it's undefined behavior but heap alignment might have forced power-of-2 elements to be allocated.)
>>
>>27450848
er, I mean multiple of 2 elements (if int is 4 bytes, on 64 bit, you might get that 100th element allocated)
>>
File: 1354336344780.gif (112 KB, 573x421) Image search: [Google]
1354336344780.gif
112 KB, 573x421
>>27450819
My bf disagrees
I am bi male before you ask for tits.
He also annoys me with his programming bullshit which I don't give a fuck about.
He also programs stupid shit that doesn't do anything worth shit.
>>
It doesn't matter, eventually all lower level programmers will be replaced by poo in loos
>>
>>27450848
Java.

Ah opps, I meant i"nt[] Array" and not "int Array[]"
>>
File: 1455421869836.gif (2 MB, 500x360) Image search: [Google]
1455421869836.gif
2 MB, 500x360
>>27450741
all the memory you use in programs is virtual so no matter what an array could be split up in lots of different places in the computer

just focus on algorithmic learning how programs actually work won't help you in anyway
>>
>>27450914
... ok ... so you've never gotten an ArrayOutOfBoundsException? Do you just allocate $what_I_think + $random_number??
>>
>>27450741
wat

How did you not know that? It's not that big of a deal though I'm just surprised you have been doing it for that long and never noticed.
>>
>OP fell for the Array meme
Don't worry OP, it's a first year CS mistake. Also watch out for the difference between assignment and equality operators
>>
>>27450914
Java: saving programmers from themselves since 1996
>>
>>27450741
>I've been "programing" for almost a year now.

Thats why lower lvl languages that do not allow to do things like "for i in array" are better.

If you were forced to create multidimensional array in C and use nested loops to print values, you would figure it out at first exercise.
>>
>>27451105
mean
*are better for learning basic programming
>>
>>27451105
Actually, sometimes, sadly not:

$ echo 'int main() { int **array, i, j; array = calloc(sizeof(int *), 3); for(i=0; i<4; i++) array[i] = calloc(sizeof(int), 3); for(i=0; i<4; i++) for(j=0; j<4; j++) array[i][j] = i*10+j; for(i=0; i<4; i++) for(j=0; j<4; j++) printf("array[%i][%i] = %i\n", i, j, array[i][j]); return 0;}' | gcc -x c -std=c89 -o test -
$ ./test
array[0][0] = 0
array[0][1] = 1
array[0][2] = 2
array[0][3] = 3
array[1][0] = 10
array[1][1] = 11
array[1][2] = 12
array[1][3] = 13
array[2][0] = 20
array[2][1] = 21
array[2][2] = 22
array[2][3] = 23
array[3][0] = 30
array[3][1] = 31
array[3][2] = 32
array[3][3] = 33

(Note the improperly sized calloc)
>>
>>27451222

valgrind catches it, but people don't often use it :(

$ valgrind ./test
[snip]
==28403== Invalid write of size 8
==28403== at 0x4005C6: main (in /home/no/test)
==28403== Address 0x51fc058 is 0 bytes after a block of size 24 alloc'd
==28403== at 0x4C2CC70: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==28403== by 0x400594: main (in /home/no/test)
==28403==
==28403== Invalid write of size 4
==28403== at 0x40061D: main (in /home/no/test)
==28403== Address 0x51fc0ac is 0 bytes after a block of size 12 alloc'd
==28403== at 0x4C2CC70: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==28403== by 0x4005C5: main (in /home/no/test)
==28403==
==28403== Invalid read of size 8
==28403== at 0x4005F9: main (in /home/no/test)
==28403== Address 0x51fc058 is 0 bytes after a block of size 24 alloc'd
==28403== at 0x4C2CC70: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==28403== by 0x400594: main (in /home/no/test)
==28403==
array[0][0] = 0
array[0][1] = 1
array[0][2] = 2
==28403== Invalid read of size 4
==28403== at 0x400669: main (in /home/no/test)
==28403== Address 0x51fc0ac is 0 bytes after a block of size 12 alloc'd
==28403== at 0x4C2CC70: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==28403== by 0x4005C5: main (in /home/no/test)
==28403==
array[0][3] = 3
array[1][0] = 10
array[1][1] = 11
array[1][2] = 12
array[1][3] = 13
array[2][0] = 20
array[2][1] = 21
array[2][2] = 22
array[2][3] = 23
==28403== Invalid read of size 8
==28403== at 0x400659: main (in /home/no/test)
==28403== Address 0x51fc058 is 0 bytes after a block of size 24 alloc'd
==28403== at 0x4C2CC70: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==28403== by 0x400594: main (in /home/no/test)
==28403==
array[3][0] = 30
array[3][1] = 31
array[3][2] = 32
array[3][3] = 33
Thread replies: 18
Thread images: 4

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.