If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.
![]() |
|
|
#1 |
|
Junior Member
Join Date: Feb 2006
Location: galaxy
Posts: 25
|
high every one
what does extern to a function ?? ============================= xt( ){ void **rv; int sv; int *d; //sv=rv; d = malloc//bla bla bla ; //*d=1120; *rv=d; } main(){ xt(); } ============================= execute:error Segmentation fault actualy " *rv=d; " cause the seg fault but when i had extern to the function ============================= main(){ extern xt(); } ============================= it's work i understand what extern does with variables but not with functions so if anyone can enlight me on this that would help humm... if you think that i should go back read a programming book please do so ^^ |
|
|
|
|
|
#2 |
|
Darlek ******
Join Date: Jun 2004
Posts: 9,663
|
well a function holds a value just like a variable
__________________
Guardian of the Most holy Two Terabytes of Gaming Goodness™ |
|
|
|
|
|
#3 |
|
Member
Join Date: Dec 2004
Location: Melbourne, Australia
Posts: 233
|
You don't mention what programming language you're using, but it looks like C. Your assignment to *rv is undefined behaviour since you never initialised rv to any value. So it could point anywhere, and in effect you'd be scribbling to some random memory location.
The extern keyword indicates external linkage - it tells the linker to look outside the current translation unit for the definition of the function. I can only guess what's going on, but I think the expression "extern xt();" doesn't actually call xt(). It declares a function called xt, which returns an int, which takes any number of arguments, and which has external linkage. But it's only a declaration, not a function call. Thus, your undefined behaviour in xt is never called. |
|
|
|
|
|
#4 |
|
Darlek ******
Join Date: Jun 2004
Posts: 9,663
|
wouldnt it call xt if xt returned a void ?
for example imagine xt() being somewhere else in another file : xt(void) { printf ("3+2 \n"); } //main program main ( ) { extern xt(); }
__________________
Guardian of the Most holy Two Terabytes of Gaming Goodness™ |
|
|
|
|
|
#5 |
|
Crazy coder
|
Nope. It's still just a declaration, not a function call.
|
|
|
|
|
|
#6 |
|
Darlek ******
Join Date: Jun 2004
Posts: 9,663
|
That will teach me for learning pascal instead of c
__________________
Guardian of the Most holy Two Terabytes of Gaming Goodness™ |
|
|
|
|
|
#7 |
|
Member
Join Date: Dec 2003
Posts: 288
|
|
|
|
|
|
|
#8 |
|
Darlek ******
Join Date: Jun 2004
Posts: 9,663
|
ok whats the bloody answer then
__________________
Guardian of the Most holy Two Terabytes of Gaming Goodness™ |
|
|
|
|
|
#9 |
|
Member
Join Date: Dec 2003
Posts: 288
|
Sc4freak already answered
extern xt() declares a local function xt with external linkage inside the function main(). This function never gets called, so basically nothing happens. |
|
|
|
|
|
#10 |
|
Member
Join Date: Dec 2004
Location: Melbourne, Australia
Posts: 233
|
I love C and C++, they're so deliciously perverse.
|
|
|
|
|
|
#11 |
|
Darlek ******
Join Date: Jun 2004
Posts: 9,663
|
i know that zengar
but how do you call it ?
__________________
Guardian of the Most holy Two Terabytes of Gaming Goodness™ |
|
|
|
|
|
#12 |
|
Registered
Join Date: May 2008
Posts: 6
|
As usual:
xt(); |
|
|
|
|
|
#13 |
|
Tea maker
Join Date: Feb 2002
Location: In the Island of Sodor, where the steam trains lie
Posts: 4,396
|
__________________
"Your work is both good and original. Unfortunately the part that is good is not original and the part that is original is not good." -(attributed to) Samuel Johnson "I invented the term Object-Oriented, and I can tell you I did not have C++ in mind." Alan Kay |
|
|
|
|
|
#14 |
|
Darlek ******
Join Date: Jun 2004
Posts: 9,663
|
Doh!!!!!
__________________
Guardian of the Most holy Two Terabytes of Gaming Goodness™ |
|
|
|
|
|
#15 |
|
Senior Member
|
I liked it. Thanks for that.
|
|
|
|
|
|
#16 |
|
Tea maker
Join Date: Feb 2002
Location: In the Island of Sodor, where the steam trains lie
Posts: 4,396
|
No worries.
Anyway, to bring this back on topic, the problem is that the OP had an uninitialised pointer and he wrote to that address. Sometimes he was "lucky" but this will usually cause the program to go "bang". The "extern" was just a red herring.
__________________
"Your work is both good and original. Unfortunately the part that is good is not original and the part that is original is not good." -(attributed to) Samuel Johnson "I invented the term Object-Oriented, and I can tell you I did not have C++ in mind." Alan Kay |
|
|
|
|
|
#17 |
|
Member
Join Date: Dec 2004
Location: Melbourne, Australia
Posts: 233
|
Bahaha, that link lists LOLCODE on it. I once wrote an entire parser/interpreter from scratch for the LOLCODE 1.2 spec, and it was even featured on the front page of lolcode.com (my real name is Adrian Tsai).
Last edited by Sc4freak; 20-Jan-2009 at 01:32. |
|
|
|
|
|
#18 |
|
Junior Member
Join Date: Feb 2006
Location: galaxy
Posts: 25
|
thx
the reason i did this ============ void **rv int *d d = malloc *rv=d ============ that's cause... ok let say that you want load : (vertex) 1house, 1tree ,somelocus, bla bla bla ... i use " *Vdominator[ ... ] " before which work well but you have to specify the array size and i wanted something dynamic anyway i still working around , there as to be some cheat |
|
|
|
|
|
#19 |
|
Tea maker
Join Date: Feb 2002
Location: In the Island of Sodor, where the steam trains lie
Posts: 4,396
|
There's no need to cheat. Simply allocate an array of pointers of sufficient length.
__________________
"Your work is both good and original. Unfortunately the part that is good is not original and the part that is original is not good." -(attributed to) Samuel Johnson "I invented the term Object-Oriented, and I can tell you I did not have C++ in mind." Alan Kay |
|
|
|
|
|
#20 |
|
Junior Member
Join Date: Oct 2007
Location: Albuquerque, NM
Posts: 54
|
If you don't mind using C++, then you can use an STL vector for arrays of unknown length.
Code:
#include <vector>
std::vector<MyDataType*> myArray;
for ( int i = 0 ; i < 10 ; i++ ) {
myArray.push_back( new MyDataType() );
}
|
|
|
|
|
|
#21 |
|
Darlek ******
Join Date: Jun 2004
Posts: 9,663
|
why not a dynamic array?
__________________
Guardian of the Most holy Two Terabytes of Gaming Goodness™ |
|
|
|
|
|
#22 |
|
Member
Join Date: Dec 2004
Location: Melbourne, Australia
Posts: 233
|
That's what std::vector is. A dynamically-sized array. But the OP was using C and not C++, unfortunately, so this is not available to him.
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|