deleting variables from structure

Hi everyone, I need some help.

I am trying to write a program in c that completely deletes all of the variables of this structure;

struct user {
char y [10];
int num1;
int num2;
int num3;
};

I have been trying for two days now and I can't figure it out. I tried looking for similar programs but it just won't work...
I would really appreciate it if someone can explain to me how to do it

thanks in advanced :)


When you say "delete", what do you mean?

If you mean you want it to be as if they never existed, that they're gone, you can't. The closest you can do is let the object fall out of scope so the whole object vanishes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct user 
{
  char y [10];
  int num1;
  int num2;
  int num3;
};

int main()
{
  {
    user newUser;  // Here an instance of the structure exists
  }
// Here, newUser does NOT exist anymore
}


Last edited on
To add to what Repeater said, I think you can also dynamically allocate a user object and then delete it, that way it doesn't exist anymore.
What if I want to delete only one variable from a structure?

Say I defined the next structure:

struct user x = {{"cake"},5, 7, 0};

and I want to delete the last number so that the structure will only contain 3 variables
is it possible?
No, it's not possible. If you have no more use for the variable just don't use it.
Last edited on
Also, for future reference, when on the subject of C++, probably best not to use the word "delete" unless you actually mean the actual delete keyword in C++ (which does NOT delete anything).
And to explain why you can't "delete" a member variable ...

When you create an object enough memory is set aside for storing all the member variables. So if you create a user object it will reserve some memory to store an array of 10 chars and 3 ints. The member variables will be stored next to each other in memory (possibly with padding bytes in between) in the order that you declared them inside the struct.

When you say you want to "delete" a member variable I guess that is because you want to free up some memory so that it can be used for other things. Unfortunately that is not possible, but for good reasons.

All objects of a certain type has the same size. It is not possible to change the size of an object after it has been created. This is the reason why accessing array elements by index is very fast. It just needs to multiply the index with the size of an element and add it to the starting address of the array in order to find the position in memory where the element is stored.

If a language had fine grain control where you could delete any member variable you wanted at runtime it would need to store extra information inside the objects, possibly splitting it up into different parts of memory, and all this would just make the program slower and use more memory.

If your concern is not about memory usage, and you only want a way to model that a user can lack a num3 value then you could represent that using an extra bool.

1
2
3
4
5
6
7
8
struct user 
{
  char y [10];
  int num1;
  int num2;
  int num3;
  bool have_num3;
};

And then you can use have_num3 to keep track of if the user has a num3 or not. If it doesn't (have_num3 == false) you just don't use num3 but it will still be there in memory. Another option is to use std::optional that was added in C++17.
Last edited on
One can, of course, define more than one structs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct user3
{
  char y [10];
  int num1;
  int num2;
  int num3;
};

struct user2
{
  char y [10];
  int num1;
  int num2;
};

When your program has no longer need for struct user3 foo, it can create struct user2 bar, copy all data (but num3) from foo to bar, and then get rid of the foo object.


Yet another approach:
1
2
3
4
5
6
struct user 
{
  char y [10];
  int* foo;
  int size;
};

The struct user has no "numbers" at all, but it has a pointer foo and number size. The foo is set to point to a separately allocated array of integers. The size stores info on how many elements that array has.

Now you can dynamically reallocate the array "owned by the struct" to have two, three, or fortytwo "num"s. The size of struct object will not change.

A C++ version:
1
2
3
4
5
struct user 
{
  std::string y;
  std::vector<int> foo;
};

Or, for a more modern approach, use std::optional:

https://en.cppreference.com/w/cpp/utility/optional

or, if you're not able to use C++17, boost::optional:

https://theboostcpplibraries.com/boost.optional

(OK, maybe these aren't for beginners, but I felt it was worth mentioning.)
Might as well add my two cents, despite the topic being checkmark'd.

@yoli, you never explained why you want to remove a variable from a struct. If you actually told us the end-result goal of what you are trying to accomplish, we could tell you how to do in C++.
If you want a dynamic list of named objects, I suggest something akin to std::map. If you just want a dynamic list of objects that you can refer to by index(0, 1, 2, 3..) use std::vector.
Last edited on
Topic archived. No new replies allowed.