How do I respond to a "[Linker error] undefined reference to 'malloc(int)' " when trying to use malloc?

I am writing a program to split lines of text from a text file into tokens. After adding the line

word = (char*) malloc (tokenLength + 1);

in a .cpp file and

void *malloc(int n);

in its corresponding .h file, I received an error that said: "[Linker error] undefined reference to 'malloc(int)' Id returned 1 exit status".

// Edited to reflect changes I made to malloc function prototype
#include <iostream>
#include <fstream>
#include <string.h> 
#include "system_utilities.h"
#include "definitions.h"
 
using namespace std;
 
ifstream file; // create input stream object
 
 
char linetext[256]; // array of characters to hold 255 characters + terminating null char
int length; // variable to hold length of current line of input (num chars)
int pos = 0; // variable to hold position of last character of current input line read by getNextToken
char* word;
 
int errorcode;
 
void printError(int errorcode)
{
   switch ( errorcode )
   {
      case END_OF_FILE:
         cout << "Error: End of file has been reached." << endl;
         break;
      case FILE_NOT_OPENED:
         cout << "Error: File could not be opened." << endl;
         break;
      case TOKEN_NOT_FOUND:
         cout << "Error: Token not found." << endl;
         break;
      default:
         cout << "There is an error." << endl;
   }
}
 
int openInputFile (char fname[])
{
   file.open(fname, ios::in); // what is ios::in for?
   if (file.good())
   {
      cout << fname << " was read successfully" << endl;
      return 0; 
   }
   else
   {
      cout << "file not read successfully" << endl;
      return FILE_NOT_OPENED; 
   }
}
 
int getNextToken(char **token) 
{
   
   int tokenFound = 0; // whether or not a token has been found
   int tokenLength = 0; // length of token
  
   while(tokenFound == 0) // when no token has been found
   {
      if(pos == 0) // if you're at the beginning of a line
      {
         int j;
         for (j = 0; j <256; j++) // set all character array values to NULL
         {
            linetext[j] = '\0';
         }
         file.getline(linetext, 256); // read in a line of text from the file
         length = strlen(linetext); // get the length of the line
      }
      
      // Recognizing single characters and symbols (not quotes)
      int i;
      for(i = pos; i < length && (tokenFound == 0); i++) // loop through the whole line
      {
         if(((linetext[i] != ' ') && (linetext[i] != '\0'))) // if the character is not a space or NULL, or is a symbol
         {
            word = (char*) malloc (tokenLength + 1);
            tokenLength = i - pos; // length of token
            pos = i + tokenLength; // new pos = next character after i (1 = length of these tokens; not i++ for all tokens)
            tokenFound = 1;
         }
      }
      
      if(pos == length) // if you've reached the end of the line
      {
         pos = 0; // reset pos to 0
         if(file.eof()) // if you've reached the end of the file
         {
            return END_OF_FILE;
         }
      }
   }
   return 0;
}
  
int symbol(char c)
{
   if ((c == '>') || (c == '<') || (c == '/') || (c == '='))
      {
      return 1;
      }
   else
      {
      return 0;
      }
}

Open in new window

vwpsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

97WideGlideCommented:
You don't need void * malloc(int); in the header file unless it is your intention to write your own malloc().  

The linker is explaining that you created a reference to a function called malloc and you didn't define it elsewhere (like in the .cpp file).

I think ...
0
97WideGlideCommented:
As an aside, if you're programming in C++ new/delete is far preferable to malloc/free.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vwpsAuthor Commented:
97WideGlide - Thanks a lot!
0
Infinity08Commented:
For malloc, you'll need to :

        #include <cstdlib>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.