?
Solved

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

Posted on 2008-11-11
4
Medium Priority
?
3,276 Views
Last Modified: 2013-11-18
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

0
Comment
Question by:vwps
  • 2
4 Comments
 
LVL 8

Expert Comment

by:97WideGlide
ID: 22936466
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
 
LVL 8

Accepted Solution

by:
97WideGlide earned 2000 total points
ID: 22936505
As an aside, if you're programming in C++ new/delete is far preferable to malloc/free.
0
 

Author Comment

by:vwps
ID: 22936514
97WideGlide - Thanks a lot!
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22937910
For malloc, you'll need to :

        #include <cstdlib>
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question