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?
 
97WideGlideConnect With a Mentor Commented:
As an aside, if you're programming in C++ new/delete is far preferable to malloc/free.
0
 
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
 
vwpsAuthor Commented:
97WideGlide - Thanks a lot!
0
 
Infinity08Commented:
For malloc, you'll need to :

        #include <cstdlib>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.