| | |
dynamic memory alloc with "new" freezes OS9.2
Please support our C++ advertiser: Intel Parallel Studio Home
| View Poll Results: What do you think is the best OS for developers? | |||
| Windowes 2000/XP/2003 Server | | 3 | 42.86% |
| Linux 2.x.x | | 3 | 42.86% |
| OS 10.x.x | | 1 | 14.29% |
| OS 9.2.x | | 0 | 0% |
| Voters: 7. You may not vote on this poll | |||
![]() |
•
•
Join Date: Sep 2003
Posts: 81
Reputation:
Solved Threads: 0
Hello,
I really don't know if this is more of a Mac of C++ question but.. I am doing a loading of a file with
"
short CSVload (char * filename) //called from main
{
//openning a file and checking if it exists
FILE * datafile = NULL;
if ((datafile=fopen (filename, "r"))==NULL)
{
printf ("\nError - no file found");
return -1;
}
//finding size of the file
fseek (datafile,0,SEEK_END);
int filesize = ftell (datafile);
rewind (datafile);
//allocating a string of a size of a file
char * data;
data = new char (filesize+2); //+2 just to be safe
//reading file (I am using getc but I simplified for this post)
fscanf (datafile,"%s", data);
//closing file
fclose (datafile);
//printing the file
printf ("\nThe data is:\n%s",data);
//deleting data
delete [] data;
//returning success
return 0;
}
"
and the program seems to be freezing every time I call this function.. I have asked somebody why this code doesn't work on OS 9.2 on G3 and the answer was that maybe I am allocating onto the system memory since OS9.2 doesn't protect it's memory like win2k or redhat 9.0 linux (my other systems). My compiler is a very old Code Warrior 7.0 Gold Edition (I usually don't use Mac's, nor this version of CW (I am only doing mac now because of my internship)).
If you can, please help,
Thank you in advance,
Ilya
I really don't know if this is more of a Mac of C++ question but.. I am doing a loading of a file with
"
short CSVload (char * filename) //called from main
{
//openning a file and checking if it exists
FILE * datafile = NULL;
if ((datafile=fopen (filename, "r"))==NULL)
{
printf ("\nError - no file found");
return -1;
}
//finding size of the file
fseek (datafile,0,SEEK_END);
int filesize = ftell (datafile);
rewind (datafile);
//allocating a string of a size of a file
char * data;
data = new char (filesize+2); //+2 just to be safe
//reading file (I am using getc but I simplified for this post)
fscanf (datafile,"%s", data);
//closing file
fclose (datafile);
//printing the file
printf ("\nThe data is:\n%s",data);
//deleting data
delete [] data;
//returning success
return 0;
}
"
and the program seems to be freezing every time I call this function.. I have asked somebody why this code doesn't work on OS 9.2 on G3 and the answer was that maybe I am allocating onto the system memory since OS9.2 doesn't protect it's memory like win2k or redhat 9.0 linux (my other systems). My compiler is a very old Code Warrior 7.0 Gold Edition (I usually don't use Mac's, nor this version of CW (I am only doing mac now because of my internship)).
If you can, please help,
Thank you in advance,
Ilya
•
•
•
•
Originally Posted by Valmian
Hello,
I really don't know if this is more of a Mac of C++ question but.. I am doing a loading of a file with
"
short CSVload (char * filename) //called from main
{
//openning a file and checking if it exists
FILE * datafile = NULL;
if ((datafile=fopen (filename, "r"))==NULL)
{
printf ("\nError - no file found");
return -1;
}
//finding size of the file
fseek (datafile,0,SEEK_END);
int filesize = ftell (datafile);
rewind (datafile);
//allocating a string of a size of a file
char * data;
data = new char (filesize+2); //+2 just to be safe
//reading file (I am using getc but I simplified for this post)
fscanf (datafile,"%s", data);
//closing file
fclose (datafile);
//printing the file
printf ("\nThe data is:\n%s",data);
//deleting data
delete [] data;
//returning success
return 0;
}
"
and the program seems to be freezing every time I call this function.. I have asked somebody why this code doesn't work on OS 9.2 on G3 and the answer was that maybe I am allocating onto the system memory since OS9.2 doesn't protect it's memory like win2k or redhat 9.0 linux (my other systems). My compiler is a very old Code Warrior 7.0 Gold Edition (I usually don't use Mac's, nor this version of CW (I am only doing mac now because of my internship)).
If you can, please help,
Thank you in advance,
Ilya
Alright, first of all... man you've really mixed C with C++
If you're going to use C you ought to use malloc() and free() and not new and elete. Anyway, try: char *data = (char *)malloc(sizeof(char) * (filesize + 2)); fscanf("%s",&data) (that's off the top of my head). Seriously though, use malloc and free from now on!!!!!Sorry I really hate C++ and when it's mixed in with C is makes my heart frown
![]() |
Similar Threads
- Dynamic memory for an array of objects (C++)
- Static and Dynamic Memory Allocations...Advan's & Disadvan's??? (Computer Science)
- regarding dynamic memory allocation (C)
- Dynamic memory allocation homework (C++)
Other Threads in the C++ Forum
- Previous Thread: Canvas and shape problem
- Next Thread: Dia, .PDB files
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





