Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 5th, 2006, 8:55 AM   #1
aloksave
Programmer
 
aloksave's Avatar
 
Join Date: Sep 2005
Posts: 33
Rep Power: 0 aloksave is on a distinguished road
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;
}
can anyone help me out??
aloksave is offline   Reply With Quote
Old Feb 5th, 2006, 9:03 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
*(intPtr->basep->ptr) = a;
I'm getting old and blind, but I don't see where you're ever establishing a value for ptr.
__________________
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
DaWei is offline   Reply With Quote
Old Feb 5th, 2006, 9:10 AM   #3
aloksave
Programmer
 
aloksave's Avatar
 
Join Date: Sep 2005
Posts: 33
Rep Power: 0 aloksave is on a distinguished road
isnt it that
(intPtr->basep->ptr) = (address of ptr)
and *(intPtr->basep->ptr) will write the value at tht location ??
aloksave is offline   Reply With Quote
Old Feb 5th, 2006, 9:40 AM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Feb 5th, 2006, 9:48 AM   #5
aloksave
Programmer
 
aloksave's Avatar
 
Join Date: Sep 2005
Posts: 33
Rep Power: 0 aloksave is on a distinguished road
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???
aloksave is offline   Reply With Quote
Old Feb 5th, 2006, 9:55 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
*(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
DaWei is offline   Reply With Quote
Old Feb 5th, 2006, 10:00 AM   #7
aloksave
Programmer
 
aloksave's Avatar
 
Join Date: Sep 2005
Posts: 33
Rep Power: 0 aloksave is on a distinguished road
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??
aloksave is offline   Reply With Quote
Old Feb 5th, 2006, 10:20 AM   #8
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Feb 5th, 2006, 10:27 AM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
(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
DaWei is offline   Reply With Quote
Old Feb 5th, 2006, 10:32 AM   #10
aloksave
Programmer
 
aloksave's Avatar
 
Join Date: Sep 2005
Posts: 33
Rep Power: 0 aloksave is on a distinguished road
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.
aloksave is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:28 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC