• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

<newb> ptr in c++

Status
Not open for further replies.
Level 5
Joined
Sep 22, 2012
Messages
90
right now I'm very confused about pointers,
JASS:
#include <iostream>
using namespace std;
//funcs
void print(int *get,int size){
    for(int i=0;i<size;i++){
        cout<<"abyss["<<i<<"]: "<<get[i]<<" abyss reference: "<<&get[i]<<endl;                
    }
}
//=====

int main()
{
    int size,del_index;
    int *abyss=0,*deleter=0;
    cout<<"enter size";
    cin>>size;
    abyss=new int[size];
    for(int i=0;i<size;i++){
        cout<<"enter element ["<<i<<"]: ";
        cin>>abyss[i];
        cout<<"reference of abyss[iter]: "<<&abyss[i]<<endl;                
    }
    print(abyss,size);
    //work around to empty an element
    cout<<"delete index element: ";
    cin>>del_index;
    deleter=&abyss[del_index];
    delete deleter;
    cout<<"=========deleted=========="<<endl;
    //printagain
    print(abyss,size);
    abyss = NULL;
    system("PAUSE");
    return 0;
}

how do i delete a specific element?
 
Level 5
Joined
Sep 22, 2012
Messages
90
how do i null a specific element from a newly allocated array? for ex i {1,4,6,3} and i wanted to null the 6 ( 2nd index )?

<another>
If I'm dealing with pointers, is zero also equivalent to null?
 
Last edited:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,201
NULL is a special platform dependant constant that people have agreed on as representing an invalid pointer. Attempts to dereference NULL should cause a segmentation fault but do not have to (depends on memory arrangement). Dereferencing NULL is always unintended behaviour.

NULL is platform dependant, and as such does not have a known value. For convenience most platforms assign NULL the value 0 and protect memory around 0 incase of accidental derefencing.

The type of NULL is also platform dependant. It is the same type as the pointer for the given platform. The size of the pointer type depends on the memory addressing structure used by a platform. Simple microcontrolers can use 2-3 byte pointers. Traditional PC software uses 4 byte pointers. Modern PC software could potentially use up to 8 byte pointers.

NULL should only ever be used with respect to pointers. The following will demonstrate how to null an element in an array.

Code:
// some int type data
int a = 1;
int b = 2;
int c = 3;
int d = 4;

// an array of pointers to int type data
int* pointers[4] = {&a, &b, &c, &d};

void removeElement(int index){
    pointers[index] = NULL;
}

Calling removeElement(0) will make pointers[0] evaluate to NULL.
The value that was pointed to at index 0 (the value of a) is left unchanged so a will still evaluate to 1;
Attempting to change the value pointed to by index 0 is now invalid and will probably cause a segmentation fault.

Code:
removeElement(0);
*pointers[0] = 0; // THIS IS BAD, index 0 is NULL due to above function call so we are dereferencing NULL
 
Level 8
Joined
Aug 13, 2009
Messages
466
Hmm, does c++ have pointer arithmetic in the way c does? I mean, you could do something like
Code:
*(pointers+2) = NULL
If you want to be cool but harder to understand

Also, what kind of variable name is "abyss" ???
 
Level 5
Joined
Sep 22, 2012
Messages
90
I wanted to make my own custom sort, perhaps this was made by someone or I don't know what do they call these steps.

(1)
--------------------
farray{12,34,17,78}
copy {x, x, x, x}
---------------------
//find the least number and put in the first index of copy array

(2)
--------------------
farray{x,34,17,78}
copy {12, x, x, x}
--------------------
//exclude/remove the least number from the first array

(3)
---------------------
farray{x,34,17,78}
copy {12, x, x, x}
---------------------
//find the min and so fort until we reach the copy array size
(this way the i won't come across the previously picked least number since I've excluded/remove it: the picked number-12)

(3)
----------------------
farray{x ,34, x, 78}
copy {12, 17, x, x}
---------------------
//and so fort then use the "delete []" to remove the first array.

and that's my problem :( the "exlude/remove" part, though i may try what spellmakers here always use like indexers/array resizing?

JASS:
#include <iostream>
using namespace std;
//funcs
void print(int *get,int size){
    for(int i=0;i<size;i++){
        cout<<"farray["<<i<<"]: "<<get[i]<<" farray reference: "<<&get[i]<<endl;                
    }
}
int ret_min(int *get,int size){
    int temp=get[0];
    for(int i=0;i<(size);i++){
             if(get[i]<=temp){
             temp =get[i];
             }
    }
    return temp;
}
void sort(int *get,int size){
     for(int i=0;i<size;i++){
             //this is my problem :(
     }
}
//=====
int main()
{
    int size,target;
    int *copy=0,*farray=0;
    cout<<"enter size";
    cin>>size;
    copy=new int[size];
    farray=new int[size];
    //inputs
    for(int i=0;i<size;i++){
        cout<<"enter element farray["<<i<<"]: ";
        cin>>farray[i];
        cout<<"reference of farray[iter]: "<<&farray[i]<<endl;                
    }
    print(farray,size);
    cout<<"\n\n"<<endl;
    //print minimum
    cout<<"minimum is: "<<ret_min(farray,size)<<endl;
    //print(farray,size);
    //sort(farray,size);
    delete [] farray;
    system("PAUSE");
    return 0;
}

JASS:
#include <iostream>
using namespace std;
//globals
//endglobals
//funcs
void print(int *get,int size){
    for(int i=0;i<size;i++){
        cout<<"--->"<<get[i]<<endl;                
    }
}
void ret_min(int *get,int size,int *ret_least,int *ret_least_index){
    int temp=get[0];
    for(int i=0;i<=(size);i++){
             if(get[i]<=temp){
             temp =get[i];
             *ret_least=temp;
             *ret_least_index=i;
             }
    }
}
void sort(int *first_get,int *second_get,int fsize,int ssize){
     int min_index,min;
     for(int i=0;i<ssize;i++){
             //find minimum of first array
             ret_min(first_get,fsize,&min,&min_index);
             //assign to second array
             second_get[i]=min;
             //recycle the first array
             first_get[min_index]=first_get[fsize];
             fsize=fsize-1;
             /*
             cout<<"fsize: "<<fsize;
             system("PAUSE");
             */
     }
}
//=====
int main()
{
    int fsize,ssize,sizer;
    int *copy=0,*farray=0;
    cout<<"enter size";
    cin>>sizer;
    fsize=sizer;
    ssize=sizer;
    copy=new int[fsize];
    farray=new int[ssize];
    //inputs
    for(int i=0;i<fsize;i++){
        cout<<"enter element farray["<<i<<"]: ";
        cin>>farray[i];
        cout<<"reference of farray[iter]: "<<&farray[i]<<endl;                
    }
    cout<<"\n\nprint primary array"<<endl;
    print(farray,fsize);
    cout<<"\n\n"<<endl;
    sort(farray,copy,fsize,ssize);
    delete [] farray;
    cout<<"\n\nsorted second array"<<endl;
    print(copy,ssize);
    system("PAUSE");
    return 0;
}
 
Last edited:
Level 8
Joined
Aug 13, 2009
Messages
466
Well, what you're looking at is a selection sort, although the way you're making it it seems a bit strange. I think a fair part of your problems are made by you trying to use two arrays for some reason. I'm sure it's possible, but you'll need all sorts of flag variables and stuff...
Additionally, you can't really "remove" an element from an array, you can only change its value. Making an array of pointers to accomplish this, since NULL can be used, is pretty inefficient as well due to unnecessary dereferencing and stuff; also big memory jumps to the pointers maybe. You can remove one from a linked list, but linked lists are significantly less efficient to sort than arrays...

What I would probably do (in pseudocodish, yay!) is:

for (int i = 0; i < array_length; i++) // OUTER LOOP
find_lowest_element_and_swap

Basically, we start fleas (lol this was unintended) at array. fleas searches this subset of the array for the lowest number, recording its position in the array with a variable. As it reaches the end of the array, array_length - i something presumably, it swaps the values of where it found the lowest value (array[lowest]) with the start of the current subset of the array (array). Then it finishes, i++, rinse restart.

Selection sorts are really inefficient sorts anyway though ^^

EDIT: I realize you might be doing something similar in your second try. I'm a bit lazy at reading code. Anyway, while partitioning the whole thing into functions is pretty, I'd say it adds a lot of unneeded complications involving pointers and such. I advise you to just use one function to do the whole sort.
 
Status
Not open for further replies.
Top