I missed my class today thanks to my car breaking down. Would array of pointers to type be something like the the following?
void *ary[10];
ary[0] = new int();
ary[1] = new float();
I’m still kinda fuzzy on what it is.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If this is
Ccode, there are a few things to point out. First off (as mentioned below),newis not validC. It is valid inC++, but you cannot allocate memory this way inC. Instead, you will need to usemalloc()to allocate memory andfree()in lieu of C++’sdelete(these functions are included by#include <stdlib.h>).Now, in response to your comment. It seems like you are confused as to what a pointer actually is. A pointer is something which points to a location in memory. That is, a pointer is of some type on your system (i.e. maybe an
unsigned long) which ultimately holds some number. This number is the memory location of some data which you allocated (note that in practice due to virtual memory, these numbers do not correspond directly to the physical RAM addresses).So in
Cwhen you do something like belowYou allocate a chunk of memory which for a single
char. The return value frommalloc()is avoid*which is why we casted it tochar*since that is what we want to use this memory as. Thisvoid*is the location to the memory you requested (by callingmalloc()) which the operating system assigned to you for use. In order to actually use this memory, you need to dereference (i.e. the*operator) the memory location. So after we have this memory allocated, we can try something likeNow the value at memory location
xisa. In code, this can be denotedIt is important to note that
sizeof(int*) == sizeof(char*)even thoughsizeof(int) != sizeof(char). This is an important distinction. Notice how the first comparison compares sizes of pointer addresses and the second compares the size of the data structures (or, in this case, type primitives). The reason that this is important is that it enables your code above to actually work. Since all pointer addresses are of equal size, you can create an array ofvoid*and store any pointer type you wish in it without overwriting the buffer (i.e. each pointer nicely fits inarray[i]respectively). The power ofvoid*is that you can pass anything as avoid*(yes, you can technically cast anything to anything else, but this is convenient at times). The major loss ofvoid*, however, is that you cannot directly use it. That said, if I have code like this:I have to know my data type to use the
void*(or you must use metadata or similar to know how to handle this).Now that we have some basis on memory allocation and what pointers actually do, I will address your comment. Since pointers simply point to memory addresses, if you have two pointers which point to the same memory address, then you simply have to ways of accessing the same data. For instance, consider the snippet below
If you are not yet too familiar with the concept of pointers, this example below may be slightly clearer since it deals purely with heap memory:
As you can see, you are simply accessing data in particular locations in memory. The pointer just points to that location in memory. Moving pointers around and making them point to different locations is known as
pointer manipulation. So that is why you can change memory at one location and it changes the value for any other pointer which points to that memory location.Original Response (Question originally tagged
C++)In reality, what you have done is more
C-style. This is technically valid (as long as you cast), but to do anything with these types later, you would have to know exactly what type is in what position and cast it back appropriately.In the example you have given, you created an array which can hold
voidpointers. That is,void*is pretty much any “vanilla” type (since you can technically cast to anything but if you do not access the memory appropriately you are sure to crash). But you cannot dereference or do any real work on avoid*so you need to know what the memory actually is in order to cast it back. In any event, it is good to note that inC++it is considered good practice to not use()for default/empty constructors – so simplynew float;will do fine.The reason you can do this without messing anything up (i.e. different data structures have different sizes) is because pointers are simply
ptr_ttypes (on many machines maybe anunsigned longor similar). So, realistically, the pointer just points to some location in memory, but all pointers (i.e. the addresses) are of equal size even if the objects the point to are different sizes.In
C++it is better to use polymorphism to do this. For instance, create an array of “Ball” pointers which all are (at least with a base class of) of the classBall. Then, you can make them more specific. For instance, you could have a baseball, basketball, etc.I am not entirely sure what you are trying without proper context, but this should look something like this
Then, in general, if you use this as follows:
This would print “Basketball” followed by “Baseball.” If you could provide some more context as to what you are looking to do, I could tailor this response a little better to target your question.