开发者

How to modify a C++ structure with int *

I have the following structure:

struct CountCarrier
{
    int *CurrCount;
};

And this is what I want to do:

int main()
{
    CountCarrier carrier = CountCarrier();
    *(carrier.CurrCount) = 2;  // initialize the *(carrier.CurrCount) to 2
   开发者_如何学Go IncreaseCount(&carrier);  // should increase the *(carrier.CurrCount) to 3
}


void IncreaseCount(CountCarrier *countCarrier)
{
    int *currCounts = countCarrier->CurrCount;
    (*currCounts)++;
}

So, my intention is specified in the comments.

However, I couldn't get this to work. For starters, the program throws an exception at this line:

*(carrier.CurrCount) = 2;

And I suspect the following line won't work as well. Anything I did wrong?


    struct CountCarrier 
    {     
        int *CurrCount;  //No memory assigned
    }; 

You need to allocate some valid memory to the pointer inside the structure to be able to put data in this.

Unless you do so, What you ar trying to do is attempting to write at some invalid address, which results in an Undefined Behavior, which luckiy in this case shows up as an exception.

Resolution:

    struct CountCarrier 
    {     
        int *CurrCount;  //No memory assigned
        CountCarrier():CurrCount(new(int))
        {

        }
    }; 

Suggestion:
Stay away from dynamic allocations as long as you can.
When you think of using pointers always think whether you really need one. In this case it doesn't really seem that you need one, A simple int member would be just fine.


You need to create the pointer. ie. carrier->CurrCount = new int;


*(carrier.CurrCount)

This is dereferencing the pointer carrier.CurrCount, but you never initialized it. I suspect this is what you want:

carrier.CurrCount = new int(2);


I seriously doubt that your program throws an exception at the line:

*(carrier.CurrCount) = 2;

While throwing an exception is certainly allowed behaviour, it seems much more likely that you encountered an access violation that caused the process to be killed by the operating system.

The problem is that you are using a pointer, but your pointer is not initialised to point at anything. This means that the result of the pointer dereference is undefined.

In this situation there does not seem to be any advantage to using a pointer at all. Your CurrCount member would work just as well if it was just a plain int.


If you are using C++, then you should encash its facilities. Instead of correcting your code, I am showing here that how the code should look like:

struct CountCarrier
{
  int CurrCount; // simple data member

  CountCarrier(int count) : CurrCount(count) {} // constructor

  CountCarrier& operator ++ ()  // overloaded operator
  {
    ++ CurrCount;
    return *this;
  }
};

We are overloading operator ++, because you have only one data member. You can replace with some named method also, like void IncrementCount().

CountCarrier carrier(2);
++ carrier;


As Als said, you need to provide some memory for the code to work.

But why make it so complicated? You don't need any pointers for the code you have to work. The "modern C++" way looks more like this:

struct CountCarrier 
{ 
public:
    CountCarrier(int currCount) : currCount(currCount) {}
    void IncreaseCount() { ++currCount; }
    int GetCount() const { return currCount; }
private:
    int currCount; 
}; 

int main() 
{ 
    CountCarrier carrier(2); // Initialize carrier.currCount to 2 
    carrier.IncreaseCount();  // Increment carrier.currCount to 3
}

Note how much cleaner and less error prone that is. Like I said, pick up a good introductory C++ book and read through it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜