"Warning: Call to function 'random' with no prototype in function main"? [C]

Elledan

Banned
Jul 24, 2000
8,880
0
0
What does this error mean? I'm getting a whole list of them.

I'm attempting to compile a source in C using the Borland C++ 5.5 compiler.

[edit]: I forgot to mention that after the above described warnings, the compiler quits with the message: Error: Unresolved external '_random' referenced from F:\DOWNLOADS\SKIP_LISTS\UMD_SOURCE\SKIPLISTS.OBJ

Anyone?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Where is your prototype header.?

The warning message is stating that the compiler does not know how to handle the procedure random.

The error message indicates that there is no reference for random in any of the libraries.

Assumption is that there may be a spelling error.
 

Elledan

Banned
Jul 24, 2000
8,880
0
0


<< Care to post the source? >>


Sure, it's a bit a mess, though (mixing C and C++ here!):

----------- source: --------------

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


#define false 0
#define true 1
typedef char boolean;
#define BitsInRandom 31

// #define allowDuplicates // duplicates are allowed if defined.

#define MaxNumberOfLevels 16
#define MaxLevel (MaxNumberOfLevels-1)
#define newNodeOfLevel(l) (node)malloc(sizeof(struct nodeStructure)+(l)*sizeof(node *))

typedef int keyType;
typedef int valueType;

#ifdef allowDuplicates
boolean delete(), search();
void insert();
#else
boolean insert(), delete(), search();
#endif

typedef struct nodeStructure *node;

typedef struct nodeStructure{
keyType key;
valueType value;
node forward[1]; /* variable sized array of forward pointers */
};

typedef struct listStructure{
int level; /* Maximum level of the list
(1 more than the number of levels in the list) */
struct nodeStructure * header; /* pointer to header */
} * list;

node NIL;

int randomsLeft;
int randomBits;

void init() {
srand(time(NULL));
NIL = newNodeOfLevel(0);
NIL->key = 0x7fffffff;
randomBits = rand() % 1;
//randomBits = random();
randomsLeft = BitsInRandom/2;
};

list newList() {
list l;
int i;

l = (list)malloc(sizeof(struct listStructure));
l->level = 0;
l->header = newNodeOfLevel(MaxNumberOfLevels);
for(i=0;i<MaxNumberOfLevels;i++) l->header->forward[ I ] = NIL;
return(l);
};

void freeList(l)
list l;
{
register node p,q;
p = l->header;
do {
q = p->forward[0];
free(p);
p = q; }
while (p!=NIL);
free(l);
};

int randomLevel()
{register int level = 0;
register int b;
do {
b = randomBits&3;
if (!b) level++;
randomBits>>=2;
if (--randomsLeft == 0) {
randomBits = rand() % 1;
//randomBits = random();
randomsLeft = BitsInRandom/2;
};
} while (!b);
return(level>MaxLevel ? MaxLevel : level);
};

#ifdef allowDuplicates
void insert(l,key,value)
#else
boolean insert(l,key,value)
#endif

register list l;
register keyType key;
register valueType value;
{
register int k;
node update[MaxNumberOfLevels];
register node p,q;

p = l->header;
k = l->level;
do {
while (q = p->forward[k], q->key < key) p = q;
update[k] = p;
} while(--k>=0);

#ifndef allowDuplicates
if (q->key == key) {
q->value = value;
return(false);
};
#endif

k = randomLevel();
if (k>l->level) {
k = ++l->level;
update[k] = l->header;
};
q = newNodeOfLevel(k);
q->key = key;
q->value = value;
do {
p = update[k];
q->forward[k] = p->forward[k];
p->forward[k] = q;
} while(--k>=0);
#ifndef allowDuplicates
return(true);
#endif
}

boolean delete(l,key)
register list l;
register keyType key;
{
register int k,m;
node update[MaxNumberOfLevels];
register node p,q;

p = l->header;
k = m = l->level;
do {
while (q = p->forward[k], q->key < key) p = q;
update[k] = p;
} while(--k>=0);

if (q->key == key) {
for(k=0; k<=m && (p=update[k])->forward[k] == q; k++)
p->forward[k] = q->forward[k];
free(q);
while( l->header->forward[m] == NIL && m > 0 )
m--;
l->level = m;
return(true);
}
else return(false);
}

boolean search(l,key,valuePointer)
register list l;
register keyType key;
valueType * valuePointer;
{
register int k;
register node p,q;
p = l->header;
k = l->level;
do while (q = p->forward[k], q->key < key) p = q;
while (--k>=0);
if (q->key != key) return(false);
*valuePointer = q->value;
return(true);
};

// -----------------------
// ---- Test Code ----

#define sampleSize 65536
main() {
list l;
register int i,k;
keyType keys[sampleSize];
valueType v;

init();

l= newList();

for(k=0;k<sampleSize;k++) {
//keys[k]=random();
keys[k]=rand() % 1;
insert(l,keys[k],keys[k]);
};


for(i=0;i<4;i++) {
for(k=0;k<sampleSize;k++) {
if (!search(l,keys[k],&v)) printf("error in search #%d,#%d\n",i,k);
if (v != keys[k]) printf("search returned wrong value\n");
};
for(k=0;k<sampleSize;k++) {
if (! delete(l,keys[k])) printf("error in delete\n");
//keys[k] = random();
keys[k] = rand() % 1;
insert(l,keys[k],keys[k]);
};
};

freeList(l);

};

--------------------------

By the way, as you can above, I've replaced random() with srand() and rand(), because a random number between 0 and 1 was needed yet I could not find a definition of the function random() anywhere in the source.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Source compiles just fine after a minor correction:

This line: for(i=0;i<MaxNumberOfLevels;i++) l->header->forward[ I ] = NIL;

should be

for(i=0;i<MaxNumberOfLevels;i++) l->header->forward[ i ] = NIL; (Capital I should be a lowercase i).

No errors/warnings using Microsoft Visual C++ 6.0 (compiled as a C source file).
 

Elledan

Banned
Jul 24, 2000
8,880
0
0


<< Source compiles just fine after a minor correction:

This line: for(i=0;i<MaxNumberOfLevels;i++) l->header->forward[ I ] = NIL;

should be

for(i=0;i<MaxNumberOfLevels;i++) l->header->forward[ i ] = NIL; (Capital I should be a lowercase i).
>>

Yeah, that's because FuseTalk screwed up again. The sourcefile is fine.



<< No errors/warnings using Microsoft Visual C++ 6.0 (compiled as a C source file). >>

I get these warnings:

---------- Borland C++ ----------
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
F:\Downloads\skip_lists\UMD_source\skipLists.c:
Warning W8065 F:\Downloads\skip_lists\UMD_source\skipLists.c 178: Call to function 'randomLevel' with no prototype in function insert
Warning W8065 F:\Downloads\skip_lists\UMD_source\skipLists.c 249: Call to function 'init' with no prototype in function main
Warning W8065 F:\Downloads\skip_lists\UMD_source\skipLists.c 251: Call to function 'newList' with no prototype in function main
Warning W8065 F:\Downloads\skip_lists\UMD_source\skipLists.c 256: Call to function 'insert' with no prototype in function main
Warning W8065 F:\Downloads\skip_lists\UMD_source\skipLists.c 262: Call to function 'search' with no prototype in function main
Warning W8065 F:\Downloads\skip_lists\UMD_source\skipLists.c 266: Call to function 'delete' with no prototype in function main
Warning W8065 F:\Downloads\skip_lists\UMD_source\skipLists.c 269: Call to function 'insert' with no prototype in function main
Warning W8065 F:\Downloads\skip_lists\UMD_source\skipLists.c 273: Call to function 'freeList' with no prototype in function main
Warning W8070 F:\Downloads\skip_lists\UMD_source\skipLists.c 275: Function should return a value in function main
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
Normal Termination
Output completed (3 sec consumed).

-------

Did you try running the program?
 

singh

Golden Member
Jul 5, 2001
1,449
0
0


<< Did you try running the program? >>



I ran the program, waited for about 4 minutes before killing it. No Output. It consumed about 800K of RAM.

I'm leaving because I have classes, so I will not see any comments/responses till I get back.
 

Elledan

Banned
Jul 24, 2000
8,880
0
0


<<

<< Did you try running the program? >>



I ran the program, waited for about 4 minutes before killing it. No Output. It consumed about 800K of RAM.
>>

Yeah, it has something to do with the warnings I'm receiving, I guess.



<< I'm leaving because I have classes, so I will not see any comments/responses till I get back. >>

That's alright :) I'm going to bed anyway =)

Thanks for your assistance so far!
 

thornc

Golden Member
Nov 29, 2000
1,011
0
0
Elladan did you read the read.me after you installed the Borland compiler....

you have to set-up a few .cfg files so that the compiler knows where to look for the includes and the libs...
 

Elledan

Banned
Jul 24, 2000
8,880
0
0


<< Elladan did you read the read.me after you installed the Borland compiler....

you have to set-up a few .cfg files so that the compiler knows where to look for the includes and the libs...
>>


Yes, I followed the instructions. I've successfully compiled dozens of projects already.
 

CSoup

Senior member
Jan 9, 2002
565
0
0
have you tried using the GCC compiler? It is more standard since it comes on all linux computers and many other unix systems. They you can easily transfer the files to different platforms and make them using one makefile. Also, why such extensive use of the register keyword in your program. It does not really make a difference since compilers will most likely ignore them. I can understand the use of volatile but not register in modern day compilers with highly optimized graph-coloring based register allocation.
 

Elledan

Banned
Jul 24, 2000
8,880
0
0
I can try compiling it on one of my Linux systems.

The use of 'register' is because this source is rather old (and hasn't been written by me).

[edit]: same for malloc() and free(). I'll probably turn it into C++ code. C just doesn't cut it =)
 

CSoup

Senior member
Jan 9, 2002
565
0
0
It is complaining about funciton prototypes because you don't have any for those functions. C was designed for one-pass compilation in the days when memory was scarce. Because of this they relied on you giving a prototype to all your functions at the top of the source file, so that the compiler would know that it existed before seeing the definition. The code will compile without function prototypes in most cases when functions are only used after they are defined, but it is standard practice to list all prototypes.

you need to include lines like

int RandomLever() at the top of your file after the includes. Seems like the prototypes for insert, delete, and search are there, so not sure why it is complaining about those.
 

m0ti

Senior member
Jul 6, 2001
975
0
0
The function doesn't exist.

"A Book on C" has it in it, but it's pseudo-random number generator they wrote themselves as an example of external static variables. Because they include all of the standard C functions, and the index only points to their example function, it doesn't exist in standard C.

rand() returns integer values, btw.

To scale it you should do (double)rand/RAND_MAX;