I beat arrays.

Pages: 12
I have read on lots of tutorials that you once an array is set, it stays that size. this is false. today i got bored in school and suddenly realized that the bare base of an array swap function i wrote can resize the array. i havent figured out how to make it smaller yet, but i can make it bigger and it will be the start of my whole array library
Isn't that std::vector?
you can do it with vector, but for the people who cant get vectors because they are new to c++ can use this
Are you actually making it larger, or are you copying it to a larger array? Because the former is going to rather implementation defined. When you create an array, it's either given a set amount of space in the stack, or a set amount of space on the heap. Each of which have to be contiguous. This means you could have an array sitting right next to some other random data, in which case making it larger would be impossible as you would end up intruding into occupied memory (which the OS likely won't allow anyway).
good guess but im not doind either. i am pretty sure this will work on normal arrays.
Perhaps you should supply some some code so it can be shot down reviewed.
Are you going to tell us how? I'm rather curious. I can't imagine a way that is not copying or using a linked list
An array is fixed size by definition. Period.
Last edited on
why would it be shot down cire? im doing it just in ints right now because i havent templated it yet

1
2
3
4
5
6
7
8
void resize(int* array, int growth)
{
	int size = sizeof(array);
	int counter;
    
	for(counter = 0; counter < growth; counter = counter + 1)
		array[counter + size] = array[0];
}


i tested it too and it worked
I'm sorry, but that doesn't resize an array. It doesn't even calculate the current size of an array correctly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void resize(int* array, int growth)
{
	//Mistake #1: sizeof(array) merely returns the size of a pointer, which is usually
	//            (but not necessarily) equal to sizeof(int).
	int size = sizeof(array);
	int counter;

	for(counter = 0; counter < growth; counter = counter + 1)
		//Mistake #2: using size, which is expressed in bytes, as an index, which is
		//            expressed in elements.
		//Mistake #3: no actual resizing is taking place. You're merely overwriting some
		//            random memory that doesn't belong to array (depending on the exact
		//            value of growth).
		array[counter + size] = array[0];
}
it resized it for me
No, no it didn't. You just overwrote some random memory.
it still resized it. do you want me to take a snap shot of the code with the output?
There's no need. It didn't. The fact that the program didn't crash isn't proof of anything.
The semantics of C and C++ don't allow the resizing of arrays. There's nothing you can do to prove that an array has been resized because that's simply impossible.
its not. i printed out the array going to element fifteen and it gave the output it was supposed too for each of them
Alright, then.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

void resize(int* array, int size, int growth){
	int counter;
	for(counter = 0; counter < growth; counter = counter + 1)
		array[counter + size] = array[0];
}

int main(){
	int a=0;
	int b[10]={23};
	int c=0;
	std::cout <<"BEFORE:\n"
		"a="<<a<<std::endl;
	for (int i=0;i<10;i++)
		std::cout <<"b["<<i<<"]="<<b[i]<<std::endl;
	std::cout <<"c="<<c<<std::endl;

	resize(b,10,10);
	
	std::cout <<"\nAFTER:\n"
		"a="<<a<<std::endl;
	for (int i=0;i<20;i++)
		std::cout <<"b["<<i<<"]="<<b[i]<<std::endl;
	std::cout <<"c="<<c<<std::endl;
	return 0;
}

Output:
BEFORE:
a=0
b[0]=23
b[1]=0
b[2]=0
b[3]=0
b[4]=0
b[5]=0
b[6]=0
b[7]=0
b[8]=0
b[9]=0
c=0

AFTER:
a=23
b[0]=23
b[1]=0
b[2]=0
b[3]=0
b[4]=0
b[5]=0
b[6]=0
b[7]=0
b[8]=0
b[9]=0
b[10]=23
b[11]=23
b[12]=12
b[13]=23
b[14]=23
b[15]=23
b[16]=23
b[17]=23
b[18]=23
b[19]=23
c=23
Note the new values of a and c.
It's also interesting to note that b[12]==12. This is likely because (&b[12]) == &i on the second for loop in main().

Try calling resize() with growth == 1000. Tell us what you see.
Last edited on
When you use an array's name, what you get is the memory address of the first element. So array[20] translates to *(array + 20). (Also try 20[array] for fun.)

This is basically, pointer arithmetic. The language expects you to know what you're doing, so if this ever blows up in your face, it'll be at the operating system's discretion (segmentation fault).

Conclusion: your code is bad, even if you're not given an error or warning.
closed account (1yR4jE8b)
@Aramil of Elixia

I suggest you head what everyone is saying here, because they are right and you are wrong -- despite the appearance of you being correct. This is undefined behavior as far as the C++ standard goes: rainbow unicorns could should lasers through your windows because of your code and it would still be technically a "correct" program.

Let me ask you, which is more likely: That everyone here who is explaining to you why your code is incorrect, are in fact wrong, despite the fact that they have referenced the C++ standard as well as given you examples showcasing why you are incorrect (this is a textbook Buffer Overflow error, http://en.wikipedia.org/wiki/Buffer_overflow) OR you have discovered the magic secret 4 lines of code of mythic legend that entire libraries have been written for like the STL, Boost and others?
@Aramil,

None of us are trying to "shut you down" or kill off any creativity you might have. We're just simply saying that what you claimed to have accomplished is, first of all, impossible in modern computer architecture, and second of all, you just didn't accomplish what you think you did.

@Helios,

That is some interesting results. How did a even get touched in all that? And did you happen to find out why b[12] is 12?
Pages: 12