extern function

nv_dv

Newcomer
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:oops:


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 ^^
 
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.
 
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();
}
 
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.
 
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.
 
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 ;)
 
There's no need to cheat. Simply allocate an array of pointers of sufficient length.
 
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() );
}
 
Back
Top