I am guessing this is not possible(At least with arduino) given that I could not find any results remotely related to this problem.
I was wondering if it is possible to create a nameless/unamed array and pass it to a pointer. Here is an example of how I want it to work:
uint8_t *Pointer = (uint8_t)[20]; // allocate 20 temporary bytes in STACK memory not heap, and grab pointer to it
//do whatever with pointer:
Pointer[0] = 5;
Pointer[3]=23;
best way to describe it is I want to create a variable similar to maloc() but in the stack memory instead of the heap memory.
A locally declared array goes on the stack.
@jremington
Yes but I want to declare one without a name, just allocate the space and get a pointer
each variable is created roughly in this format:
type name;
e.g.
char Mychar;
I want one without the name part
Of what possible use is that? The actual names exist only in the program code.
The name IS the pointer.
@jremington
I need to create an global array/buffer for a class e.g.:
Array[20];
Myclass obj(Array, 20);
Scince I will never need that Array ever in the main code, I want there to be a number/pointer instead of a name.
Not on the stack, you don't. Stack data are ephemeral.
I need to declare multiple instances of the same class in the same scope. I dont want to have to do this:
Array[20];
Array2[20];
Array3[20];
Myclass obj(Array, 20);
Myclass obj2(Array2, 20);
Myclass obj3(Array3, 20);
//so on
This makes no sense to me, so I'll bow out. Good luck with your project.
@jremington
To give a little more of an idea of what I am dealing with, I want to create a macro that will declare a variable, but if I call that macro mutltiple times, I will get an error because of redefinition, hence I want to create a pointer and just increment when I need to create a new var e.g.:
#define Mem uint8_t Var = 0
.
.
.
Mem;
Mem; // <<< Error, Redefined "Var"
How can I make a macro that creates a new and unique name each time called!?
If this is too confusing, I am sorry, I was just wondering if it was possible in C++
The literature suggest if the memory is "static" then a private destructor is created but never called.
You must have some hook to return the SRAM address unless you want to go the route of makefile. Once you have the address, you can map any type or struct you wish. Again, this is how read the lit, but reality is often different.
Make the array a class member variable.
3 Likes
I don't understand why you explicitly don't want a name. The name is not kept when the program is compiled. I think before I can help at all, I need to understand the bigger picture of what you think you are trying to accomplish.
look at post #11
How can I get a Macro to define a different name each time? I need a pointer not a name
This is still a simplified example and really I just want to know if this is possible.
You can do this inside a class definition. Outside a class definition, it doesn't really make any sense. Macros don't make sense to use like this.
What OP is asking for makes no sense whatsoever, and suggests some serious mis-understandings of how c/c++, and code in general, work. He clearly does not understand at all how data in c/c++ is represented, stored, and accessed. I cannot begin to imagine what he expects to gain...
Agreed, which is why I am trying to help him get away from false assumptions and return to the original problem.
What I am saying is that I know behind every named variable e.g byte X; is a pointer to that variable. I can even get that pointer out like this byte* point = &X;
I am asking if it is possible to create a variable: byte without a name. I dont want the extra "X" e.g.:
byte *point = &(byte);
If this is impossible in C++, then I won't try no more.
It just does not make sense to me why it would be possible to do with the heap e.g.
pointer = (byte *)malloc(1);
and not with stack e.g.
pointer = &(byte);