English 中文(简体)
1. 点子对型
原标题:Array of Pointers to Type

今天,由于我的汽车倒塌,我失去了我的班子。 形形色色的点数是否像以下的样子?

void *ary[10];
ary[0] = new int();
ary[1] = new float();

我仍然对情况感到怀疑。

最佳回答

如果是<代码>C,则要指出几个方面。 首先(如下文所述),<代码>新 <代码>C不适用。 本条在<代码>C++上有效,但不能在<代码>上对这一方式进行记忆。 C 。 相反,你需要使用<条码>小型()来分配记忆和<条码>免费(<>/条码>,代替C++条码/code>。 这些功能载于<代码>#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).

∗∗∗∗ 阁下:

char* x = (char*)malloc(sizeof(char));

You allocate a chunk of memory which for a single char. The return value from malloc() is a void* which is why we casted it to char* since that is what we want to use this memory as. This void* is the location to the memory you requested (by calling malloc()) 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 like

*x =  a ;

现在记忆点的价值<代码>x>为/code>。 在法典中,可以说明这一点。

if(*x ==  a )
  printf("True");

It is important to note that sizeof(int*) == sizeof(char*) even though sizeof(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 of void* and store any pointer type you wish in it without overwriting the buffer (i.e. each pointer nicely fits in array[i] respectively). The power of void* is that you can pass anything as a void* (yes, you can technically cast anything to anything else, but this is convenient at times). The major loss of void*, however, is that you cannot directly use it. That said, if I have code like this:

void passData(void* data)
{
  char* x = (char*)data; // I _must_ do this to access the data
  printf("%s
", x);
}

int main()
{
  char* x = (char*)malloc(10*sizeof(char));
  *x = "test";
  passData((void*)test);
  free(x);
  return 0;
}

我必须知道我的数据类型,以使用 (或你必须使用元数据或类似的元数据,知道如何处理)。

既然我们对记忆分配有某种依据,而且点人实际上做了些什么,我将谈谈你的意见。 既然点人只是记忆地址,如果你有两点人,注明了same记忆地址,那么你就不得不使用同样的数据。 例如,审议以下简称表

#include <stdio.h>
int main()
{
  int x = 0; // Stack variable

  int* xPtr = &x; // & is the reference operator - it gives you the memory location of an object

  x = 5;

  printf("%d == %d
", x, *xPtr); // xPtr == 5 since it points to the same memory location as x

  *xPtr = 10;

  printf("%d == %d
", x, *xPtr); // x == 10 since we changed the value at the memory location of xPtr which is the same memory location which x is located at

  return 0;
}

如果你不熟悉点人的概念,下面这一例子可能略为清楚,因为它仅涉及耳光:

#include <stdio.h>
int main()
{
  int* xPtr = (int*)malloc(sizeof(int));
  int* yPtr = xPtr; // Point yPtr to the same address as xPtr

  *xPtr = 10; // The value of xPtr == 10 and so does the value of yPtr since they point to the same place
  *yPtr = 12; // In a similar way, we modified the value again for both pointers.

  yPtr = NULL; // Unset the memory location of yPtr - now yPtr no longer points to the same memory location as xPtr.
               // In fact, now yPtr == NULL, so do _NOT_ try to dereference it or you will crash. In any event, the values of the pointers are now independent

  *xPtr = 15; // Memory at the location xPtr points to has changed to 15. But yPtr no longer points to that location, so the value at the location yPtr points to has not changed.

  yPtr = xPtr; // Change the memory location of yPtr to point to xPtr. Now yPtr s value is exactly the same as xPtr s value.

  free(xPtr); // Only free ONE of the items pointing to the same memory location (otherwise you will double free)
  return 0;
}

你可以看到,你只是从记忆中获取特定地点的数据。 这位发言者只是要记住这个地点。 环绕点和向不同的地点点点点点点点点,称为<条码>点码>。 因此,你可以在某个地点改变记忆,并改变显示该记忆地点的任何其他点的价值。

www.un.org/spanish/ecosoc (问题原先编号C++)

在现实中,你做了更多的工作,即<条码>C型。 这在技术上是有效的(只要是你所投的),但为了在以后与这些类型做任何事,你必须知道exactly什么类型的位置,并适当退。

如你所示,你创建了一个阵列,可以持有<条码>。 也就是说, und* 基本上是指任何“vanilla”类型(因为你在技术上可以 anything取任何东西,但如果你不能适当获取你确信会坠毁的记忆)。 但是,你不能在<0> > void*上回避或做任何真正的工作,因此,你需要知道记忆是怎样的,才能回来。 无论如何,值得注意的是,在<代码>上。 C++ 据认为,不使用<代码>(<>>>>作为违约/无效建筑商使用的良好做法——即<代码>新浮动;将予以罚款。

如果不说明任何情况(即不同的数据结构有不同的大小),你就可以这样做,因为点人只是<代码>ptr_t。 类型(许多机器可能是unsign long或类似)。 因此,现实地说,点名人只是记忆中的某些地点,但所有点(即地址)的大小相同,即使点标的物体大小不同。

缩略语 最好利用多变方式做到这一点。 例如,创建一系列“Ball”点,所有点(至少是基级)为<代码>Ball。 然后,你可以使其更加具体。 例如,你可能有一个基球、篮球等。

我不完全肯定你在没有适当背景的情况下试图做些什么,但这应该看像这样的情况。

#include <isotream>
using namespace std;
class Ball
{
public:
  virtual void ballType() const = 0;
};

class Basketball : public Ball
{
public:
  virtual void ballType() const {
    cout << "Basketball" << endl;
  }
};

class Baseball : public Ball
{
public:
  virtual void ballType() const {
    cout << "Baseball" << endl;
  }
};

那么,一般而言,如果你使用:

Ball* x[10];
x[0] = new Basketball;
x[1] = new Baseball;

x[0]->ballType();
x[1]->ballType();

delete x[0];
delete x[1];

这将印刷“篮球”,然后是“篮球”。 如果你能够就你所期望的做些什么提供更多情况,那么我就能够使这一反应更好地针对你的问题。

问题回答

点击器是“闪电”号,可以指你希望记忆中的任何东西,如果你有空话,那么你所说的阵列现在取决于你如何利用它来打上暗中或其他东西和物体。

if you use void *array[10] that creates an array of pointers, now how you use the pointers its up to you. You can create objects or assign them to what ever data type you want,but this is not type safe since it`s a void pointer without type specification. it also requires strong type casting for usage with data types.

从技术上讲,你可以在该阵列内拥有不同数据类型的每一个点。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签