Teen Programmers Unite  
 

 

Return to forum top

(C++) Start-Off a Bit Shaky

Posted by webdesign11 [send private reply] at April 16, 2002, 07:28:52 PM

I've restarted learning C++. I am writing an address book program and right now I'm writing a file function. I am getting errors on lines 13 and 18. The errors are very cryptic so I will not disclose them. Any help is greatly appreciated. Code is below.

#include <stdio.h>

char * readfile(const char * filename) {
char * buffer;
long pifSize;
FILE * pif=fopen(filename,"r");
if(pif==NULL) buffer=(char *)"Cannot open file!";

fseek(pif,0,SEEK_END);
pifSize=ftell(pif);
rewind(pif);

buffer=(char *)malloc(pifSize);
fread(buffer,1,pifSize,pif);

fclose(pif);
return buffer;
free(buffer);
}
int main() {
char * pifDat=readfile("myfile.txt");
printf("Data:\n\n%s",pifDat);

return 0;
}

Posted by taubz [send private reply] at April 16, 2002, 07:47:56 PM

I don't see a problem on those lines just from looking at it. Those error messages would be *very* helpful.

- taubz

Posted by webdesign11 [send private reply] at April 16, 2002, 08:27:50 PM

Alright, here are the error messages:

Line 13: implicit declaration of 'int malloc(...)'
Line 18: implicit declaration of 'int free(...)'

Posted by CodeRed [send private reply] at April 16, 2002, 08:37:09 PM

if(pif==NULL) buffer=(char *)"Cannot open file!";

I don't think you need to cast that string literal. Thats not the problem but I just thought I'd say something

Posted by CodeRed [send private reply] at April 16, 2002, 08:37:59 PM

I don't know anything about malloc or free, I've never found a need for them.

Posted by vikram_1982 [send private reply] at April 17, 2002, 12:42:42 AM

Add the header file : alloc.h

Posted by vikram_1982 [send private reply] at April 17, 2002, 12:44:32 AM

malloc is defined in the alloc.h file. If u use it without including the header, the malloc and the free fns wont work. Hence the errors.


By the way, this program is in C. Not C++.

Posted by buzgub [send private reply] at April 17, 2002, 01:22:56 AM

The convential include for those libraries is stdlib.h, so you probably just want the line

#include <stdlib.h>

at the top of your program.

BTW, malloc's parameter should be the amount of memory you want in bytes. Bear in mind that your code for allocation will work because sizeof(char) == 1, but that doesn't hold true for other types. Also note that after your call to return, your function stops executing. You need to put your free statement elsewhere if you want it to do anything.

I probably missed something there.
Posted by webdesign11 [send private reply] at April 17, 2002, 07:15:55 AM

Thank you for the help, everyone. I have it working, but it adds a superfluous "ov" and then a down arrow (have no idea how to write that - I've forgotten what its ASCii keycode is). Is there any way to get rid of that?

Posted by buzgub [send private reply] at April 17, 2002, 07:29:46 AM

It sounds to me like there's some gibberish at the end of the array, probably just because you didn't set it to anything. Is there a difference between pifSize and the actual size of the file?

It's also possible that there's some newline funniness going on. If myfile.txt happens to have three lines, I'll try to explain further. Otherwise, I can't be bothered (It's second hand knowledge that I half understand, so I won't inflict it on you without need).

Posted by CViper [send private reply] at April 17, 2002, 07:55:07 AM

actually theres no problem with the size / types, since ftell() returns the offset in *bytes* from the beginning of the file.

to get rid of some funny characters, try
malloc( pifSize+1 ); // one more byte for the '\0'
and after
fread( buffer, 1, pifSize, pif );
add
buffer[pifSize] = '\0';

btw: you'll get some memory leeks, since "buffer" is never free()'d (as someone already mentioned)
btw II: the cast at line... uhm.. 7 (i think) isn't required.

also if pif is NULL, you'll get a nasty error, since your going to try to free non-heap memory

Posted by taubz [send private reply] at April 17, 2002, 09:04:31 AM

I want to explain the error message for webdesign11 so that he understands what to do when he sees that error in the future.

"implicit declaration of 'int malloc(...)' "

Implict and explicit are very cool words. Explicit means that it's actually written or said somewhere. Implicit means that it's not written anywhere, but it can be assumed.

So the error message means "the declaration of 'malloc' isn't written anywhere that the compiler knows of, so it's assuming that the definition is 'int malloc (...)'."

Now, you don't want a computer making its own assumptions in a computer program, everything should be explicit. :)

Hopefully you know what a function declaration is (if not, we should go find the answer to a previous post for you). So, what the compiler is asking for is an explicit (written somwhere) definition of malloc. Where do you get definitions for "standard" functions, in stdlib.h, which you #include.

I hope that explains that.

- taubz

Posted by CodeRed [send private reply] at April 17, 2002, 01:32:43 PM

save the whales, feed the children, free the mallocs

Posted by webdesign11 [send private reply] at April 17, 2002, 03:31:59 PM

I understand that you need to include the header files that contain the functions. I just thought that malloc() and free() were within the stdio.h header file; I guess that assumptions are stupid in C++ programming. Thanks for your help.

Posted by Psion [send private reply] at April 17, 2002, 05:00:33 PM

Also, in C++, you should use new and delete instead of malloc and free. It will save you the trouble of wondering whether something has a constructor that must be called behind the scenes, thus preventing malloc from doing everything required.

Posted by Sord [send private reply] at April 17, 2002, 07:39:05 PM

First of all, you should include both alloc.h and stdlib.h
Secondly, do NOT use malloc and free in C++, use new and delete. Use malloc and free only in C and in a case where you MUST use it in C++.

Posted by Psion [send private reply] at April 17, 2002, 08:31:38 PM

alloc.h isn't standard, so I don't think it's a good idea to include it.

Posted by vikram_1982 [send private reply] at April 18, 2002, 05:59:49 AM

"alloc.h isn't standard, so I don't think it's a good idea to include it."

I dont understand what u mean by "it is not standard". Do u mean to say all compilers dont support it? In that case in C, if I am using dynamic allocation , I will always get this error.

Posted by Psion [send private reply] at April 18, 2002, 08:55:20 AM

I mean that the international standard says that malloc and free are prototyped in stdlib.h, and says nothing about any file alloc.h.

Posted by vikram_1982 [send private reply] at April 18, 2002, 11:05:16 AM

oic. Thanks. I didnt know that.

Posted by RedX [send private reply] at April 18, 2002, 01:32:50 PM

If you want to learn C++, why don't U use new and delete?
No includes, simple interface, what more do you need?

RedX

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.