Shaitan00
05-28-2009, 07:43 AM
I have a class such as the following:
class A
{
private:
int* integer;
void Copy(const string&);
public:
A(const string&); // Constructor
};
The function "Copy" can be used in two different instances:
a) Creation of the class (Constructor)
b) Assignment of int* integer
In case (a) I need to assign the int* using calloc while in case (b) I simply want to change the size for the new assignment using realloc. Therefore my code works something like this:
A::A(const string& sNumber)
{
Copy(sNumber);
}
void A::Copy(const string& sNumber)
{
if (integer == NULL)
integer = (int*) calloc (size, sizeof(int));
else if (originalSize != size)
integer = (int*) realloc (integer, size * sizeof(int));
}
Now this is causing me some issues, and I assume it is because of the way I am checking to see if my pointer (int* integer) is NULL or not ... I am trying to find out if (integer) has been assigned or not, if it already exists (not null) then just realloc, otherwise create it using calloc...
Running this code generates the following error:
0xC0000005: Access violation reading location 0xccccccc8
When I look (watch) integer has a value of
integer 0xcccccccc int *
So, the pointer is NOT NULL, but I've never assigned it to anything yet...
How do I check to see if I need to use calloc or realloc? How do I differentiate between both cases? Any ideas or help would be much appreciated.
Thanks,
class A
{
private:
int* integer;
void Copy(const string&);
public:
A(const string&); // Constructor
};
The function "Copy" can be used in two different instances:
a) Creation of the class (Constructor)
b) Assignment of int* integer
In case (a) I need to assign the int* using calloc while in case (b) I simply want to change the size for the new assignment using realloc. Therefore my code works something like this:
A::A(const string& sNumber)
{
Copy(sNumber);
}
void A::Copy(const string& sNumber)
{
if (integer == NULL)
integer = (int*) calloc (size, sizeof(int));
else if (originalSize != size)
integer = (int*) realloc (integer, size * sizeof(int));
}
Now this is causing me some issues, and I assume it is because of the way I am checking to see if my pointer (int* integer) is NULL or not ... I am trying to find out if (integer) has been assigned or not, if it already exists (not null) then just realloc, otherwise create it using calloc...
Running this code generates the following error:
0xC0000005: Access violation reading location 0xccccccc8
When I look (watch) integer has a value of
integer 0xcccccccc int *
So, the pointer is NOT NULL, but I've never assigned it to anything yet...
How do I check to see if I need to use calloc or realloc? How do I differentiate between both cases? Any ideas or help would be much appreciated.
Thanks,