user2399378
user2399378

Reputation: 871

Remove elements from pointer array

I have a program in which I need to work with pointer arrays. My problem is that I do not know how to delete element i from pointer p (p[i]). I will detail the problem below.

I have the structure:

struct CuttingLine
    {
        NxU32 linePoints[150];
        NxU32 lineLength;
        NxVec3 normal;
    };

Then I declare the pointer:

CuttingLine* cuttingLines; 

I initialize the pointer like this:

cuttingLines = (CuttingLine*)malloc(sizeof(CuttingLine) * 10);

And then I add some elements to it (please notice that this is just for demonstration purposes, in my program, line is created and given values):

for(int i=0;i<3;i++)
    cuttingLines[i] = line;

Then I want to go through the pointer again, and delete the three elements, but not free the pointer (I understand that you can delete the pointer by calling free(cuttingLines)). How can I do that? I just want to delete the elements inside it, but not deallocate the memory allocated in the beginning.

Upvotes: 3

Views: 5024

Answers (2)

Antti
Antti

Reputation: 12479

To me it looks like you want to have an array of pointers, and dynamically allocate the elements of the array. In this case you'd want to set the value of the pointer to NULL, which means the pointer doesn't point to anything. Like this:

cuttingLines[i] = NULL;

This means "I have a pointer in my array but it doesn't point to anything".

However if you don't free the memory (by calling free(cuttingLines[i])) you will have a memory leak, so remember to free the memory later in your program. For this you will need to have another pointer somewhere that points to whatever each call to malloc returned.

Just to be pedantic: you can't free a pointer (except in the sense of allocating e.g. sizeof(CuttingLine*) using malloc); you can free memory, so calling free(ptr) frees the memory pointed to by the pointer. Also, you treat cuttingLines like an array of pointers, i.e. pointer of pointers, so its type should be CuttingLine** and you'd declare and allocate it by:

CuttlingLine** cuttingLines;
cuttingLines = (CuttingLine**)malloc(sizeof(CuttingLine*) * 10);

Also, you shouldn't cast the return value of malloc (see here) unless you're writing C++ in which case you shouldn't need to use pointers to pointers in the first place.


If on the other hand you want to have an array of CuttingLines then you're on the right track in the sense that you allocate cuttingLines correctly, and you don't need to free the memory used by individual CuttingLine. However in this case you don't know which elements in the array are valid and which aren't unless you either track the indices in the code or in the elements themselves by e.g. setting lineLength to 0 and checking its value.

Upvotes: 1

jxh
jxh

Reputation: 70392

Because cuttingLines is a pointer to CuttingLine, your loop is actually performing a structure copy of line into each of the three positions. There is no need to delete these deep copies. You just have to not use the information anymore when you are done with it, and note that the cuttingLines variable contains unused data. You can then reuse the cuttingLines variable later (filling it with new lines) at your convenience.

Upvotes: 1

Related Questions