473,388 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Implicit Declaration of Function "int malloc(...)"

Rabbit
12,516 Expert Mod 8TB
I've #included <stdlib.h> at the beginning of the file. Weird thing is this compiled fine before, then I changed the code of one of the functions, which should, as far as I know, have nothing to do with this piece:
Expand|Select|Wrap|Line Numbers
  1. buffer = (char*)malloc(100*sizeof(char));
And now I'm getting that error.

So I figured I must have missed something in the tutorial so when I couldn't find anything by sight, I copied all the source files from the tutorial and tried to compile that but I still get the error.
Apr 6 '07 #1
5 10847
Rabbit
12,516 Expert Mod 8TB
Sorry, I solved it. I was only assuming he #include <stdlib.h> but he didn't. I misread the <stdio.h> as <stdlib.h>. Should read more carefully...
Apr 6 '07 #2
Ganon11
3,652 Expert 2GB
LOL: Welcome to the wide world of C++/C, where the biggest frustration can be caused by the smallest error.

Speaking of C++/C, which are you learning? malloc() is generally used in C, while new is used in C++.
Apr 6 '07 #3
Rabbit
12,516 Expert Mod 8TB
I'm using C++, the tutorial used malloc so I went with it.
Apr 6 '07 #4
Banfa
9,065 Expert Mod 8TB
If you are allocating data for simple (built-in) types (int, float , double, char etc) malloc is fine.

If you are allocating data for structures or classes then it is rather important to use new. The reason for this is that when you use new the constructor for the data type is called where as when you call malloc it is not. Since built in types do not have constructors it is not a problem for them but for classes and structures it can be a serious problem if the constructor is not called.

Couple the fact that newbie programmers forget that a struct and class differ only in the default access specifier for members (public for struct and private for class) and that a class/struct with a default constructor that does nothing can actually be malloc'd without a problem and string is not actually a built-in type but a defined type and a class the following error has occured several times on the board

Expand|Select|Wrap|Line Numbers
  1. typedef struct mystruct {
  2.    int someVariable;
  3.    int someOtherVariable;
  4.    string str;
  5. } MyStruct;
  6.  
  7. MyStruct *ms = (MyStruct *)malloc(sizeof(MyStruct));
  8.  
  9. ms->str = "Hello World";  // <-- everything falls over
  10.  
string is a class and needs it's constructor called, mallocing MyStruct fails to call the (compiler provided) default constructor of MyStruct so the constructor of str is not called. When the str member is accessed it's internal members have not been initialised correctly and everything falls apart.

Many new C++ programmers seem to thing that string is a built in type but it is in fact extremely import to remember that it isn't.

Also use of typedef and MyStruct in this example is superfluous, C++ automatically defines the structures name mystruct. (I recently went through most of project code at work removing such definitions).
Apr 8 '07 #5
Rabbit
12,516 Expert Mod 8TB
Wow, didn't know all that although I kinda figured string was a defined type.
Apr 8 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jason | last post by:
Hi, Im running windows xp pro and compiling using dev c++ 4. I have the following situation: #include <iostream> #include <string> using namespace std; int main() {
2
by: nick | last post by:
the following is my programming code and compile message why the warning message arise, have i done somethings wrong? #include<stdio.h> typedef struct card{ int abc; }card;
7
by: Paminu | last post by:
On a gentoo linux system i don't get any warnings when I comlpile this code: #include <stdio.h> typedef struct test { void *content; struct test_ *bob; } test_;
5
by: Christian Christmann | last post by:
Hi, in one file I've a function that is used by another file containing the main function: file1.c: .... void test( int a ) { ... } ....
14
by: Chen Shusheng | last post by:
CSS white here: Simply strange, I found "define" can not work with "malloc". Together my complier will say "parse error". Could anyone tell me why? ------------------------- #define MAX 10000...
6
by: yeah | last post by:
hi I got this error "implicit declaration of function" what it means???
14
by: UNCManiac37 | last post by:
Hi, I'm very new to coding, so this is a newbie question. I'm trying to print out a banner using functions instead of main: int main () { banner1 (); } void banner1 (void) { cout...
5
by: DanielJohnson | last post by:
I call a function which is named as func_name in file /source/folderA/ fileA.c. The actual function definition is in /source/folderB/fileB.c. And I get this error. warning: implicit...
3
by: samdomville | last post by:
hello...upon compilation, i get a warning: "warning: implicit declaration of funciton funciton_name" what, techicallly, is an implicit declaration? how/where would i look to track down the error?...
1
by: samdomville | last post by:
Hello - I have a .c file that gives me a warning "implicit declaration of function function_name". However, I have #included the header file that #defines that funciton, so how is it possible to...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.