![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Sep 2005
Posts: 33
Rep Power: 0
![]() |
A run time error
why does this program give me a runtime error
#include<iostream.h>
union deriv1
{
int *ptr;
int arr[4];
}deriv1;
struct deriv2
{
int inte;
}deriv2;
struct base
{
union deriv1 *basep;
struct deriv2 base2;
}basicBase;
int main()
{
int a = 5;
struct base *intPtr = new struct base;
(intPtr->base2.inte) = 5;
*(intPtr->basep->ptr) = a;
cout << "data"<<*(intPtr->basep->ptr);
cout << "\ndata"<<(intPtr->base2.inte);
delete intPtr;
return 1;
} |
|
|
|
|
|
#2 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: Sep 2005
Posts: 33
Rep Power: 0
![]() |
isnt it that
(intPtr->basep->ptr) = (address of ptr) and *(intPtr->basep->ptr) will write the value at tht location ?? |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
No it will write to somewhere outside of your program, which will cause a run time error.
You need to either allocate storage for it, so assign it.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Sep 2005
Posts: 33
Rep Power: 0
![]() |
sorry but i didnt get it....why will it write beyond my program....??
and how do i avoid it??
struct deriv2
{
int inte;
int *ptr;
}deriv2;
int main()
{
struct deriv2 *obj = new struct deriv2;
(obj->ptr) = &a;
cout << "data"<<*(obj->ptr);
}works fine so why not tht??? |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
*(intPtr->basep->ptr) = a; assigns the value of a to the location pointed to by intPtr->basep->ptr; you have not established valid contents for that location yet. The contents there are either random trash or some value established by your compiler that is guaranteed to point to no useful area. Either will generate a run-time error on systems that incorporate memory protection (most desktop OSes, these days). You might want to refer to the "Pointer Basics" material referenced in my signature.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Sep 2005
Posts: 33
Rep Power: 0
![]() |
why doesnt this work then..
(intPtr->basep->ptr) = &a; cout << "data"<<*(intPtr->basep->ptr); now i am making the location point to adress of a isnt it? still it wont work?? |
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
You _really_ need to read the Pointer Basics that's in DaWei's signature first.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
(intPtr->basep->ptr) = &a; You're missing the point. You've changed nothing regarding the pointer, which is unitialized still, you're merely changing the value contained in a to the address of the variable a. THIS IS WHAT'S BEING ASSIGNED; IT IS NOT THE POINTER, BUT WHAT YOU ARE TRYING TO STORE WHERE THE POINTER POINTS. The pointer, intPtr->basep->ptr, still contains an invalid address. If you don't want to read the material, or find some other material to read, then you need to pay attention and engage your thinking process, at the very least.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#10 |
|
Programmer
Join Date: Sep 2005
Posts: 33
Rep Power: 0
![]() |
Ok fine sorry for the trouble, i would read it and will come back again if i dont get it..
anyways thanks for ur help. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|