Welcome, Unregistered.

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.

Reply
Old 16-Jan-2009, 16:18   #1
nv_dv
Junior Member
 
Join Date: Feb 2006
Location: galaxy
Posts: 25
Default extern function

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 ^^
nv_dv is offline   Reply With Quote
Old 16-Jan-2009, 21:57   #2
Davros
Darlek ******
 
Join Date: Jun 2004
Posts: 9,663
Default

well a function holds a value just like a variable
__________________
Guardian of the Most holy Two Terabytes of Gaming Goodness™
Davros is offline   Reply With Quote
Old 16-Jan-2009, 22:32   #3
Sc4freak
Member
 
Join Date: Dec 2004
Location: Melbourne, Australia
Posts: 233
Default

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.
Sc4freak is offline   Reply With Quote
Old 17-Jan-2009, 19:13   #4
Davros
Darlek ******
 
Join Date: Jun 2004
Posts: 9,663
Default

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™
Davros is offline   Reply With Quote
Old 17-Jan-2009, 21:30   #5
Humus
Crazy coder
 
Join Date: Feb 2002
Location: Stockholm, Sweden
Posts: 3,216
Send a message via ICQ to Humus Send a message via MSN to Humus
Default

Nope. It's still just a declaration, not a function call.
__________________
[ Visit my site ]
I speak for myself and only myself.
Humus is offline   Reply With Quote
Old 17-Jan-2009, 21:58   #6
Davros
Darlek ******
 
Join Date: Jun 2004
Posts: 9,663
Default

That will teach me for learning pascal instead of c
__________________
Guardian of the Most holy Two Terabytes of Gaming Goodness™
Davros is offline   Reply With Quote
Old 19-Jan-2009, 03:23   #7
Zengar
Member
 
Join Date: Dec 2003
Posts: 288
Default

Quote:
Originally Posted by Davros View Post
That will teach me for learning pascal instead of c
It is not so different in pascal...
Zengar is offline   Reply With Quote
Old 19-Jan-2009, 04:19   #8
Davros
Darlek ******
 
Join Date: Jun 2004
Posts: 9,663
Default

ok whats the bloody answer then
__________________
Guardian of the Most holy Two Terabytes of Gaming Goodness™
Davros is offline   Reply With Quote
Old 19-Jan-2009, 06:20   #9
Zengar
Member
 
Join Date: Dec 2003
Posts: 288
Default

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.
Zengar is offline   Reply With Quote
Old 19-Jan-2009, 06:33   #10
Sc4freak
Member
 
Join Date: Dec 2004
Location: Melbourne, Australia
Posts: 233
Default

I love C and C++, they're so deliciously perverse.
Sc4freak is offline   Reply With Quote
Old 19-Jan-2009, 08:36   #11
Davros
Darlek ******
 
Join Date: Jun 2004
Posts: 9,663
Default

i know that zengar
but how do you call it ?
__________________
Guardian of the Most holy Two Terabytes of Gaming Goodness™
Davros is offline   Reply With Quote
Old 19-Jan-2009, 11:25   #12
AndreasL
Registered
 
Join Date: May 2008
Posts: 6
Default

As usual:
xt();
AndreasL is offline   Reply With Quote
Old 19-Jan-2009, 11:47   #13
Simon F
Tea maker
 
Join Date: Feb 2002
Location: In the Island of Sodor, where the steam trains lie
Posts: 4,396
Default

Quote:
Originally Posted by Sc4freak View Post
I love C and C++, they're so deliciously perverse.
Au contraire, this and these are perverse.
__________________
"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
Simon F is offline   Reply With Quote
Old 19-Jan-2009, 16:04   #14
Davros
Darlek ******
 
Join Date: Jun 2004
Posts: 9,663
Default

Doh!!!!!
__________________
Guardian of the Most holy Two Terabytes of Gaming Goodness™
Davros is offline   Reply With Quote
Old 19-Jan-2009, 16:16   #15
rpg.314
Senior Member
 
Join Date: Jul 2008
Location: /
Posts: 4,079
Send a message via Skype™ to rpg.314
Icon Smile

Quote:
Originally Posted by Simon F View Post
Au contraire, this and these are perverse.
I liked it. Thanks for that.
__________________
The views presented here are my own and not my employer's.
Quote:
Originally Posted by Alexko View Post
So in a nutshell, model [BLANK] will have [BLANK], up to [BLANK], and even [BLANK] for a power consumption of just [BLANK]. Impressive.
rpg.314 is offline   Reply With Quote
Old 19-Jan-2009, 17:18   #16
Simon F
Tea maker
 
Join Date: Feb 2002
Location: In the Island of Sodor, where the steam trains lie
Posts: 4,396
Default

Quote:
Originally Posted by rpg.314 View Post
I liked it. Thanks for that.
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
Simon F is offline   Reply With Quote
Old 20-Jan-2009, 01:25   #17
Sc4freak
Member
 
Join Date: Dec 2004
Location: Melbourne, Australia
Posts: 233
Default

Quote:
Originally Posted by Simon F View Post
Au contraire, this and these are perverse.
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.
Sc4freak is offline   Reply With Quote
Old 20-Jan-2009, 14:33   #18
nv_dv
Junior Member
 
Join Date: Feb 2006
Location: galaxy
Posts: 25
Default

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
nv_dv is offline   Reply With Quote
Old 20-Jan-2009, 15:33   #19
Simon F
Tea maker
 
Join Date: Feb 2002
Location: In the Island of Sodor, where the steam trains lie
Posts: 4,396
Default

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
Simon F is offline   Reply With Quote
Old 21-Jan-2009, 19:55   #20
codelogic
Junior Member
 
Join Date: Oct 2007
Location: Albuquerque, NM
Posts: 54
Default

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() );
}
__________________
PS3 Theme Extractor
ps3.blackvoltage.org
codelogic is offline   Reply With Quote
Old 21-Jan-2009, 23:05   #21
Davros
Darlek ******
 
Join Date: Jun 2004
Posts: 9,663
Default

why not a dynamic array?
__________________
Guardian of the Most holy Two Terabytes of Gaming Goodness™
Davros is offline   Reply With Quote
Old 22-Jan-2009, 00:02   #22
Sc4freak
Member
 
Join Date: Dec 2004
Location: Melbourne, Australia
Posts: 233
Default

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.
Sc4freak is offline   Reply With Quote
Old 22-Jan-2009, 10:23   #23
Mate Kovacs
Member
 
Join Date: Dec 2004
Location: Debrecen, Hungary
Posts: 163
Send a message via ICQ to Mate Kovacs
Default

Well, C99 has dynamically-sized arrays.
linky
Mate Kovacs is offline   Reply With Quote

Reply

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


All times are GMT +1. The time now is 14:07.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.