Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Double pointers question

Double pointers question

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformancehelp
4 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Budric B
    wrote on last edited by
    #1

    Hi, I have this problem I can't find much information about. Suppose I have the following function:void test(float arg[][3]) { //do nothing } ... float ** vertices = Allocate2DArray(10,3); //assume that's correct test(vertices); //doesn't work! ... float vertices2[10][3]; test(vertices2); //works
    So basically in the first case it can't convert ** to [][3]. What's the difference? How can I cast it? I can't change the function because the real one is already written that way. Finally a question about freeing the memory. Suppose I haveBYTE * temp = new BYTE[TotalSize] float ** result = (float **) temp; ..followed by a loop to assign result[i] = (float *) to a location in temp[i*cols*sizeof(float)];
    Now if I type delete[] result. Will that delete all the memory I allocated? After all I allocated a BYTE[], but then just did a bunch of assignments. Thanks.

    D J T 3 Replies Last reply
    0
    • B Budric B

      Hi, I have this problem I can't find much information about. Suppose I have the following function:void test(float arg[][3]) { //do nothing } ... float ** vertices = Allocate2DArray(10,3); //assume that's correct test(vertices); //doesn't work! ... float vertices2[10][3]; test(vertices2); //works
      So basically in the first case it can't convert ** to [][3]. What's the difference? How can I cast it? I can't change the function because the real one is already written that way. Finally a question about freeing the memory. Suppose I haveBYTE * temp = new BYTE[TotalSize] float ** result = (float **) temp; ..followed by a loop to assign result[i] = (float *) to a location in temp[i*cols*sizeof(float)];
      Now if I type delete[] result. Will that delete all the memory I allocated? After all I allocated a BYTE[], but then just did a bunch of assignments. Thanks.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Budric B. wrote: What's the difference? Even though they can sometimes be used interchangeably, pointers and arrays are not the same thing. One way to achieve what you want is like:

      double (*vertices)[3] = (double (*)[3]) malloc(10 * sizeof(*vertices));
      for (int x = 0; x < 10; x++)
      {
      for (int y = 0; y < 3; y++)
      vertices[x][y] = 0.0; // initialize 'em
      }

      test(vertices);


      "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

      1 Reply Last reply
      0
      • B Budric B

        Hi, I have this problem I can't find much information about. Suppose I have the following function:void test(float arg[][3]) { //do nothing } ... float ** vertices = Allocate2DArray(10,3); //assume that's correct test(vertices); //doesn't work! ... float vertices2[10][3]; test(vertices2); //works
        So basically in the first case it can't convert ** to [][3]. What's the difference? How can I cast it? I can't change the function because the real one is already written that way. Finally a question about freeing the memory. Suppose I haveBYTE * temp = new BYTE[TotalSize] float ** result = (float **) temp; ..followed by a loop to assign result[i] = (float *) to a location in temp[i*cols*sizeof(float)];
        Now if I type delete[] result. Will that delete all the memory I allocated? After all I allocated a BYTE[], but then just did a bunch of assignments. Thanks.

        J Offline
        J Offline
        John R Shaw
        wrote on last edited by
        #3

        FIRST PART: If test(float* in){...} vertices2 // is a pointer test(vertices2) // ok test(&vertices2) // error vertices // is a pointer to a pointer test(*vertices) // ok test(vertices) // error In this case a pointer to array of pointers, it gets a bit confusing. float* pr = vertices[r]; float f = pr[c]; Therefore, float f = (vertices[r])[c]; SECOND PART: (*result)[i] == temp[i] result[i] == &temp[i] No the == are not typos. I hope that helps. INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen

        1 Reply Last reply
        0
        • B Budric B

          Hi, I have this problem I can't find much information about. Suppose I have the following function:void test(float arg[][3]) { //do nothing } ... float ** vertices = Allocate2DArray(10,3); //assume that's correct test(vertices); //doesn't work! ... float vertices2[10][3]; test(vertices2); //works
          So basically in the first case it can't convert ** to [][3]. What's the difference? How can I cast it? I can't change the function because the real one is already written that way. Finally a question about freeing the memory. Suppose I haveBYTE * temp = new BYTE[TotalSize] float ** result = (float **) temp; ..followed by a loop to assign result[i] = (float *) to a location in temp[i*cols*sizeof(float)];
          Now if I type delete[] result. Will that delete all the memory I allocated? After all I allocated a BYTE[], but then just did a bunch of assignments. Thanks.

          T Offline
          T Offline
          Toby Opferman
          wrote on last edited by
          #4

          So, what is the difference between ** and [x][y]? Well, one is a static memory location and the other are pointers.

          float **x;
          float y[10][3];

          x is a pointer to a pointer. So, what the compiler does it it goes to the address of the variable X. It then reads this address and then goes to the address stored at that location. It then uses this address as the place to store the float! If you understand single pointers you can see how the double pointer works:

          First Address = *x;
          Second Address = *First Address;
          The Actual Float Value = *Second Address;

          At any level you can actually allocate a "block" of pointers that you can index into at that time. So what's the difference with an array?

          y\[1\]\[2\] = 12;
          

          Is actually

          y\[3 + 2\] = 12;
          

          It's a single block of memory! There are no pointers! In the case of even a single float *, the address of the variable stores the address that then stores the actual value! In the case of an array, the address of the variable stores the actual value! So, a multi-dimentional pointer is actually a single block of memory that is then index by the compiler as one block. So if you have: rows by columns: float x[row][column] each row is maxcolumns size. So if you want to go to rowx, coly, you could simply do rowx*maxcolumns + coly. This would index that block of memory. The compiler knows how to generate the correct code if it's an array or pointer. Remember, an array's address stores the actual data. The pointer's address, stores another address. That address then stores the data (in a single indirection case). 8bc7c0ec02c0e404c0cc0680f7018827ebee

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups