Home > Net >  Allocating pointer to a pointer
Allocating pointer to a pointer

Time:12-05

I have 5 buffers and 20 frames to write in them. Being one frame per buffer, at a certain moment i will have to overwrite buffers with the newest frame.

At random moments i need to read the oldest frame(its id and data) from all the buffers.

I am obliged to use a pointer to a pointer for my buffers, but since i suck at pointers, not even the allocation works, giving me a SEGMENTATION FAULT and not sure why.

What i have until now:

 void fakeFrame(uint16_t *data)
 {
    for (auto i = 0; i < 1440; i  )
       for (auto j = 0; j < 1440; j  )
           data[(i * 1440)   j] = std::rand()%2;
       
  }
    

int main()
{
        uint16_t         **p_frameBuffers;
        uint32_t          *p_frameIdxs;
        uint16_t           wrIdx = 0;
        uint16_t           reIdx = 0;
        uint16_t           currentFrameCounter = 0;
        uint16_t           nbBuffers = 5;

        for(auto i =0; i< nbBuffers; i  )
        {
           p_frameBuffers[i] = (uint16_t*)malloc(1440*1440*2);
        }


        while(currentFrameCounter <= 20)
        {
             wrIdx  ;
             wrIdx %= nbBuffers;

    
             if(wrIdx == reIdx)
             {
                    std::cout<<"i passed the limit";
             }
             currentFrameCounter  ;

             p_frameIdxs[wrIdx] = currentFrameCounter;
             fakeFrame(p_frameBuffers[wrIdx]);
         }

         std::cout<<"\n";


         return 0;
 }

CodePudding user response:

I can see a few different problems with this code here.

You declare the long-form of the function for fakeFrame() in the beginning of the program, when the standard is usually to declare the function header first.

This is like a warning to the program that a function is about to be used, and that it's not part of a class or anything. Just standalone.

Example:

#include <iostream>

void fakeFrame();

int main()
{
    return 0;
}

void fakeFrame()
{
for (auto i = 0; i < 1440; i  )
       for (auto j = 0; j < 1440; j  )
           data[(i * 1440)   j] = std::rand()%2;
}

You're also using some of these 16 and 32 bit unsigned ints as if they were arrays, so I was deeply confused about that. Did you mean to set them as arrays?

You also have some variables being declared in a non-array context but being used as arrays. I'm not deeply familiar with the uint variable/object types but I know they aren't usually meant to function as standalone arrays.

Also, no variable called m_pFrameBuffers is actually declared in the code you provided. Plus this is also used as an array, so it should really be declared as one.

I hope this provides at least some insight into what's not working. I'm actually kind of surprised that the void function ran before, it's improperly formatted.

CodePudding user response:

In the end this is what did it: the pointer to a pointer is actually an array of pointers (which i did not know, of course)

p_frameBuffers = (uint16_t**)malloc((sizeof(uint16_t*)*nbBuffers));

for(auto i = 0; i < nbBuffers; i  )
{
    p_frameBuffers[i] = (uint16_t*)malloc(1440*1440*2);
}
  • Related