-2

I am trying to code my own simple array, in hopes of better understanding how an array internally works.

Since conceptually, I believe an array is just a pointer to its base element and incrementing that pointer by 1 would point to the next subsequent element.

I wrote this code as a foundation to my simple array:

int x = 2;
int *ptr = &x;

int y = 3;
&y = ptr + 1;

Here we create a variable x and use ptr to point to it. We then create a variable y and try to change its memory address to the next memory block after ptr (ptr + 1). This is to create a simulation of an array.

Now, this didn't work as I later learned that once a variable is created, its memory is fixed no matter what and there's no way to modify it. So, what are some other ways I can exploit to be able to create my own array?

Beginner
  • 91
  • 1
  • 5
  • 6
    *Since conceptually, I believe an array is just a pointer to its base element* This is not correct. An array is an array, a pointer is a pointer. They are two different types. Do yourself a favor and do not think of an array as being a pointer. An array name can decay to a pointer, but that's it, the array is still an array, and knows extra stuff like how many objects are in it. – NathanOliver Mar 10 '20 at 15:07
  • On way to simulate an array would be to use a linked list. It would perform much worse than an array of consecutive memory locations, but would teach you the concepts. – stark Mar 10 '20 at 15:10
  • 3
    *and try to change its memory address to the next memory block after ptr (ptr + 1)* -- Even if you could do this, you are accessing memory that you did not reserve. It is undefined behavior to step on memory that your program did not allocate or reserve for itself. – PaulMcKenzie Mar 10 '20 at 15:10
  • @NathanOliver your explaination seems somekind of advance to the OP. as i see, he's totally noob in c++ and kinda enjoying learning by observations. please give a more simple example to differentiate array and a pointer "in" c++. – acegs Mar 10 '20 at 15:28
  • 1
    You could use std::vector - or at least read the std::vector implementation. – Wyck Mar 10 '20 at 15:36
  • 1
    See [What is array decaying?](https://stackoverflow.com/questions/1461432/what-is-array-decaying). A common misconception among beginners is that _"an array is just a pointer"_. That's not correct. – Drew Dormann Mar 10 '20 at 16:05
  • @NathanOliver I respectfully disagree with you, an array is just a pointer that points to its first element. It's how an array works internally in C++. For example, if you declare an array "int list[100];", the C++ compiler treats that the it as "&list[0]". – Beginner Mar 11 '20 at 16:08
  • 1
    @Beginner No, it really isn't. If an array was really just a pointer to the first element, `sizeof(int[4])` would give you 4 or 8 instead of 16. You really need to not think of an array as a pointer, because it isn't. Also see [this](https://stackoverflow.com/questions/3959705/arrays-are-pointers) – NathanOliver Mar 11 '20 at 16:12
  • I think @NathanOliver is just strict on the meaning of the `array`. Conceptually it's an array. if you go deep down internally everything is an array. anything any object can be treated as array. regarding with programming discipline or based from experience that really works better, specifically to a certain programming language, yes, you shouldn't treat array as a pointer. there is just few cases in c++ where an array variable can look like it's a pointer syntactically, but a fixed one. though that is a useful feat but kinda confusing. like any other languages, you just have to be used to i – acegs Mar 12 '20 at 01:54

1 Answers1

-1

I hope this helps:

int x = 2;
int *ptr = &x;     //ptr points to address of x. yes correct.

int y = 3;         
&y = ptr + 1;      //the address of next integer after x(internally, 
                   //   which is ptr) will be set to address of y(this 
                   //   won't work.)

on the last line, if your intention is make y become the storage that is internally next to x, this won't happen because int y is a declaration that y will be data holder and won't move it's address. it's address is fixed forever.

in addition, this is the internal memory based on your code.

[ 1 byte ]    //4 bytes for integer x
[ 1 byte ]
[ 1 byte ]
[ 1 byte ]
[ 1 byte ]    //4 bytes for address holder ptr (in 32bit system)
[ 1 byte ]
[ 1 byte ]
[ 1 byte ]
[ 1 byte ]    //4 bytes for integer y
[ 1 byte ]
[ 1 byte ]
[ 1 byte ]

you can change y via x by treating it as an array.

(&x)[2] = 100;   //setting 100 to y indirectly.

note: this might not work on different compilers due to internal optimizations, so it's a dangerous type of code if you use it on real world projects. but you can 100% use it for fun and exploration. one way to love programming more.

another crazier example.

(&x)[1] = 0x123498;   //setting any address value to ptr.
(&x)[1] = &y;         //setting address of y to ptr.

to your original question(i hope i get it)

So, what are some other ways I can exploit to be able to create my own array?

to "create" your own array, or let's say create dynamically at runtime, you have to allocate first a sequence of bytes that will fit to the array of the size you need. and then access the elements or any kind of manipulation via pointer or reference.

//---allocate using malloc and free.
int *pIntBuffer = (int*)malloc( 500 * sizeof(int));  //request chunk of memory.
pIntBuffer[10] = 123;  //use the array any way you want.

free(pIntBuffer);      //be sure to destroy your requested chunk of memory if you don't need it anymore.

//---allocate using new/delete.
int *pAnotherBuff = new int[1000];
pAnotherBuff[900] = *pIntBuffer[10];

delete [] pAnotherBuff;
Ðаn
  • 10,934
  • 11
  • 59
  • 95
acegs
  • 2,621
  • 1
  • 22
  • 31
  • Do note that `(int*)malloc( 500 * sizeof(int));` doesn't actually create an array and is undefined behavior until C++20. Not sure a compiler would actually not "do the right thing", but it doesn't have to. – NathanOliver Mar 11 '20 at 16:16
  • @NathanOliver we're talking about `array` that means consecutive bytes storage right? or no? it seems the array object you mean is a higher level or more modern concept now. – acegs Jul 28 '20 at 01:36