Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8778069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:26:11+00:00 2026-06-13T19:26:11+00:00

The void del function cannot set the class pointer inside of obj_slot to NULL;

  • 0

The void del function cannot set the class pointer inside of obj_slot to NULL;

class test_object {
public:
     char *name;
     int  id;

};

int  current_amount;
test_object  *obj_slot[512];

void add(test_object *obj)
{
  if(current_amount < 512)
  {
    obj->id = current_amount;
    obj_slot[current_amount]  = obj;
    current_amount ++;
  }
  else {
    std::cout<<"max exceeded";
  }

}

void printList(char *status){

  printf("%s\n",status);
  for(int i = 0 ; i <  current_amount ; i ++)
  {
     printf("list object id %i; string is %s,pointer:%p\n",obj_slot[i]->id,obj_slot[i]->name,obj_slot[i]);

  }

}
void del(test_object *obj)
{

  printList("before:");

  if(!obj)
    return;

    printf("deleting %s id %i,pointer %p\n",obj->name,obj->id,obj);

  for(int i =  obj->id ; i <  current_amount - 1 ; i ++)
  {

     obj_slot[i] = obj_slot[i + 1];

  }

   delete obj;
   obj = NULL;
   current_amount--;

   printList("after:");
}

//this is the test program:

   int main(int argc, char **argv) {
            std::cout << "Hello, world!" << std::endl;
            for(int i = 0 ; i < 5; i ++)
            {
               test_object *test  = new  test_object();
               char  a[500];
               sprintf(a,"random_test_%i",i);
               test->name = (char *)malloc(strlen(a) + 1);
               strcpy(test->name,a);
              add(test);
            }
            test_object *test  = new  test_object();
            test->name = "random_test";
           add(test);
           del(test); 
           printf("test pointer after delete is %p\n",test); 
            return 0;
        }

I’ve set the pointer address that I want to delete in the del function to NULL; but the console output is still this:

before:
list object id 0; string is random_test_0,pointer:0x706010

list object id 1; string is random_test_1,pointer:0x706050

list object id 2; string is random_test_2,pointer:0x706090

list object id 3; string is random_test_3,pointer:0x7060d0

list object id 4; string is random_test_4,pointer:0x706110

list object id 5; string is random_test,pointer:0x706150

deleting random_test id 5,pointer 0x706150

after:
list object id 0; string is random_test_0,pointer:0x706010

list object id 1; string is random_test_1,pointer:0x706050

list object id 2; string is random_test_2,pointer:0x706090

list object id 3; string is random_test_3,pointer:0x7060d0

list object id 4; string is random_test_4,pointer:0x706110

test pointer after delete is 0x706150

* Exited normally *

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:26:12+00:00Added an answer on June 13, 2026 at 7:26 pm

    That’s because in the del function the variable obj is a local variable, and all changes to it will not be visible outside of that function. If you want to modify it you should pass it as a reference instead:

    void del(test_object *&obj)
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I declared a delegate: public delegate void Del(string message); Then i created a function
void say(char msg[]) { // using pointer to print out the first char of
I have a function as below. private void AddPrice(String value) { Delegate del=new Action<String>(AddToCollection);
This is my code: void get_pass(char *p); int main(){ char *host, *user, *pass; host
I am attempting to pass a managed function pointer void (*)(void *) to my
void test() { unsigned char c; c = (~0)>>1 ; printf(c is %u\n,c); }
- (void)TargetHit:(int)target{ void (^threadBlock)(void) = ^{ NSLog(@respond to selector %d, [self respondsToSelector:@selector(changeImageOfTarget:)]); [[NSOperationQueue mainQueue]
void SaveFiles(out XElement Attachments) { Attachments = null; if (Uploader1.UploadedFiles.Count() > 0) { Attachments
void foo(Node* p[], int size){ _uint64 arr_of_values[_MAX_THREADS]; for (int i=0 ; i < size
The following function is defined in ./kernel/sched/rt.c(v3.5.4): static void enqueue_pushable_task(struct rq *rq, struct task_struct

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.