Hi,
I ran into a malloc problem but I can't find the solution.
I try to read a file into a variable with malloc like this:
BYTE *lcdata;
lcdata = malloc(fsize*sizeof(BYTE));
<do stuff>
free(lcdata);
lcdata = NULL;
fsize is the size of the file I'm reading. When I read a file with a size
around 2KB everything works ok. When I read a file with a size of around
1.5MB malloc doesn't allocate the memory. I'm using lcc-win32 as the
compiler. The program in a running state is using around 7MB in memory
before malloc.
Hope anyone can help. 9 1922
Me wrote: Hi,
I ran into a malloc problem but I can't find the solution.
I try to read a file into a variable with malloc like this:
BYTE *lcdata;
lcdata = malloc(fsize*sizeof(BYTE));
Using `sizeof *lcdata` may be better (easier to maintain).
<do stuff>
free(lcdata); lcdata = NULL;
fsize is the size of the file I'm reading. When I read a file with a size around 2KB everything works ok. When I read a file with a size of around 1.5MB malloc doesn't allocate the memory. I'm using lcc-win32 as the compiler. The program in a running state is using around 7MB in memory before malloc.
Have you considered the possibility that you've run out of memory? What
does "does not allocate the memory mean"? Does it return NULL? You
don't seem to test for that. There may be OS/compiler specific ways of
checking how much memory you have left.
PS
Do you /really/ have to read /all/ of a 1.5MB file into memory at once?
Me wrote: Hi,
I ran into a malloc problem but I can't find the solution.
I try to read a file into a variable with malloc like this:
BYTE *lcdata;
lcdata = malloc(fsize*sizeof(BYTE));
<do stuff>
free(lcdata); lcdata = NULL;
fsize is the size of the file I'm reading. When I read a file with a size around 2KB everything works ok. When I read a file with a size of around 1.5MB malloc doesn't allocate the memory. I'm using lcc-win32 as the compiler. The program in a running state is using around 7MB in memory before malloc.
Hope anyone can help.
What's fsize's *type* - unlikely it's overflowing, but worth knowing.
--
==============
Not a pedant
==============
"Vladimir Oka" <no****@btopenworld.com> wrote in
news:11**********************@i40g2000cwc.googlegr oups.com: Me wrote: Hi,
I ran into a malloc problem but I can't find the solution.
I try to read a file into a variable with malloc like this:
BYTE *lcdata;
lcdata = malloc(fsize*sizeof(BYTE)); Using `sizeof *lcdata` may be better (easier to maintain).
<do stuff>
free(lcdata); lcdata = NULL;
fsize is the size of the file I'm reading. When I read a file with a size around 2KB everything works ok. When I read a file with a size of around 1.5MB malloc doesn't allocate the memory. I'm using lcc-win32 as the compiler. The program in a running state is using around 7MB in memory before malloc.
Have you considered the possibility that you've run out of memory? What does "does not allocate the memory mean"? Does it return NULL? You don't seem to test for that. There may be OS/compiler specific ways of checking how much memory you have left.
I made a check for that:
if (!*lcdata)
{
<allocate error message>
return 0;
} PS Do you /really/ have to read /all/ of a 1.5MB file into memory at once?
Well it would be possible to read smaller parts, but that would make the
program very complex. I also have to mention that this malloc is inside a
function (1MB stack ?) Can't imagine it has anything to do with the stack
because declaration, malloc and free is all inside the function.
"pemo" <us***********@gmail.com> wrote in news:e7**********@news.ox.ac.uk: Me wrote: Hi,
I ran into a malloc problem but I can't find the solution.
I try to read a file into a variable with malloc like this:
BYTE *lcdata;
lcdata = malloc(fsize*sizeof(BYTE));
<do stuff>
free(lcdata); lcdata = NULL;
fsize is the size of the file I'm reading. When I read a file with a size around 2KB everything works ok. When I read a file with a size of around 1.5MB malloc doesn't allocate the memory. I'm using lcc-win32 as the compiler. The program in a running state is using around 7MB in memory before malloc.
Hope anyone can help.
What's fsize's *type* - unlikely it's overflowing, but worth knowing.
fsize is of type long and the actual test value for fsize is 1612288.
Me wrote: "Vladimir Oka" <no****@btopenworld.com> wrote in news:11**********************@i40g2000cwc.googlegr oups.com:
Have you considered the possibility that you've run out of memory? What does "does not allocate the memory mean"? Does it return NULL? You don't seem to test for that. There may be OS/compiler specific ways of checking how much memory you have left.
I made a check for that:
if (!*lcdata)
Did you /really/ write `!*lcdata`? Not `!lcdata` (I prefer `lcdata == 0`)?
If malloc returns null, lcdata is null, and *lcdata is undefined, BOOM. PS Do you /really/ have to read /all/ of a 1.5MB file into memory at once?
Well it would be possible to read smaller parts, but that would make the program very complex.
Would it? Are you sure? It depends on the problem.
I also have to mention that this malloc is inside a function (1MB stack ?) Can't imagine it has anything to do with the stack because declaration, malloc and free is all inside the function.
It will depend, for example, on how much store programs are allowed
to allocate. You don't necessarily get everything that's available.
--
Chris "pseudo-Ivanova" Dollin
"No-one here is exactly what he appears." G'kar, /Babylon 5/
Me wrote: "Vladimir Oka" <no****@btopenworld.com> wrote in news:11**********************@i40g2000cwc.googlegr oups.com:
Me wrote: Hi,
I ran into a malloc problem but I can't find the solution.
I try to read a file into a variable with malloc like this:
BYTE *lcdata;
lcdata = malloc(fsize*sizeof(BYTE)); Using `sizeof *lcdata` may be better (easier to maintain).
<do stuff>
free(lcdata); lcdata = NULL;
fsize is the size of the file I'm reading. When I read a file with a size around 2KB everything works ok. When I read a file with a size of around 1.5MB malloc doesn't allocate the memory. I'm using lcc-win32 as the compiler. The program in a running state is using around 7MB in memory before malloc.
Have you considered the possibility that you've run out of memory? What does "does not allocate the memory mean"? Does it return NULL? You don't seem to test for that. There may be OS/compiler specific ways of checking how much memory you have left.
I made a check for that:
if (!*lcdata)
ITYM
if (!lcdata)
If not, you may find that where `lcdata` points to in case of a large
file (by coincidence) is a NULL pointer!
{ <allocate error message> return 0;
Returning 0 usually means success. To be sure, return EXIT_FAILURE.
} PS Do you /really/ have to read /all/ of a 1.5MB file into memory at once?
Well it would be possible to read smaller parts, but that would make the program very complex.
Maybe, but it would certainly be easier on resources, and friendlier in
multi-tasking environments. However, only you can be the judge of where
the best line of compromise lies (or is it your users?).
I also have to mention that this malloc is inside a function (1MB stack ?) Can't imagine it has anything to do with the stack because declaration, malloc and free is all inside the function.
SHould be irrelevant. Memory allocated in a function is not local to
it. Memory allocation (using `malloc()`) comes out of a "global" memory
pool available to C program (details are implementation defined).
Me <ju****@here.com> wrote in
news:Xn***************************@194.109.133.242 : Hi,
I ran into a malloc problem but I can't find the solution.
I try to read a file into a variable with malloc like this:
BYTE *lcdata;
lcdata = malloc(fsize*sizeof(BYTE));
<do stuff>
free(lcdata); lcdata = NULL;
fsize is the size of the file I'm reading. When I read a file with a size around 2KB everything works ok. When I read a file with a size of around 1.5MB malloc doesn't allocate the memory. I'm using lcc-win32 as the compiler. The program in a running state is using around 7MB in memory before malloc.
Hope anyone can help.
Problem solved. It was the check that did it I now changed it to if
(lcdata==NULL)
Thanks for the help people !
Chris Dollin wrote: Me wrote:
"Vladimir Oka" <no****@btopenworld.com> wrote in news:11**********************@i40g2000cwc.googlegr oups.com: Have you considered the possibility that you've run out of memory? What does "does not allocate the memory mean"? Does it return NULL? You don't seem to test for that. There may be OS/compiler specific ways of checking how much memory you have left.
I made a check for that:
if (!*lcdata)
Did you /really/ write `!*lcdata`? Not `!lcdata` (I prefer `lcdata == 0`)?
If malloc returns null, lcdata is null, and *lcdata is undefined, BOOM.
I was also possible to do such
.....
unsigned char * lcdata;
.....
lcdata=(unsigned char *)calloc(fsize,sizeof(unsigned char))
lcdata[0]=lcdata | lcdata >> 8 | lcdata >> 16 | lcdata >> 24;
if (*lcdata)
{
error_message;
exit(error_code);//because begine of the stack in main() is already corrupted
:)))
}
if you wanna see interpreter of strange language wellcom to http://users.svitonline.com/quas/tec
Zis is ze let' tou censoers.
--by ManoNegra PS Do you /really/ have to read /all/ of a 1.5MB file into memory at once?
Well it would be possible to read smaller parts, but that would make the program very complex.
Would it? Are you sure? It depends on the problem.
I also have to mention that this malloc is inside a function (1MB stack ?) Can't imagine it has anything to do with the stack because declaration, malloc and free is all inside the function.
It will depend, for example, on how much store programs are allowed to allocate. You don't necessarily get everything that's available.
-- Chris "pseudo-Ivanova" Dollin "No-one here is exactly what he appears." G'kar, /Babylon 5/
Groovy hepcat Quas.co.ua was jivin' on Tue, 20 Jun 2006 22:40:02 +0300
in comp.lang.c.
Re: Strange malloc problem's a cool scene! Dig it! Chris Dollin wrote: Me wrote:
> "Vladimir Oka" <no****@btopenworld.com> wrote in > news:11**********************@i40g2000cwc.googlegr oups.com: >> Have you considered the possibility that you've run out of memory? >> What does "does not allocate the memory mean"? Does it return NULL? >> You don't seem to test for that. There may be OS/compiler specific >> ways of checking how much memory you have left. > > I made a check for that: > > if (!*lcdata)
Did you /really/ write `!*lcdata`? Not `!lcdata` (I prefer `lcdata == 0`)?
If malloc returns null, lcdata is null, and *lcdata is undefined, BOOM.
I was also possible to do such
.... unsigned char * lcdata; .... lcdata=(unsigned char *)calloc(fsize,sizeof(unsigned char)) lcdata[0]=lcdata | lcdata >> 8 | lcdata >> 16 | lcdata >> 24;
But, since the allocation has failed and calloc() has returned a
null pointer, and you are therefore dereferencing a null pointer,
BLAMMO! The nasal demons take over your linen closet and demand that
you give them fabric softener, threatening to shred your sheets if you
don't.
if (*lcdata)
And just for bad measure, you reopen the door to Nasal Hell by
dereferencing a null pointer again, allowing the nasal demons to take
over your bathroom so they can point and laugh at you every time you
take a bath.
{ error_message;
And what is that, exactly; a macro expanding to something useful,
perhaps?
exit(error_code);//because begine of the stack in main() is already corrupted :))) }
if you wanna see interpreter of strange language wellcom to http://users.svitonline.com/quas/tec
If it contains examples of the kind you've displayed above, I'm not
surprised it is for a strange language. That language is Ubish, no
doubt, the native tongue of the nasal demons.
--
Dig the even newer still, yet more improved, sig! http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Neil Zanella |
last post by:
Hello,
I wonder whether anyone has ever come across
the following g++ compiler error message. I
don't recall ever seeing it before. I solved
my problem but I am still not sure about
what this...
|
by: Edd Dawson |
last post by:
Hi. I have a strange problem involving the passing of command line
arguments to a C program I'm writing. I tried posting this in
comp.programming yesterday but someone kindly suggested that I'd...
|
by: Marlene Stebbins |
last post by:
Something very strange is going on here. I don't know if it's a C
problem or an implementation problem. The program reads data from
a file and loads it into two arrays. When xy, x, y, *xlist and...
|
by: Ronny Mandal |
last post by:
Hi!
I am writing a program that'll read binary integers from a file, put them
into a dynamic array. THe array is first declared
int* a = malloc( sizeof( int ) * N ) <-- N is the number of...
|
by: bear |
last post by:
hi all,
I have a program whose speed is so strange to me. It is maily used
to
calculate a output image so from four images s0,s1,s2,s3 where
so=(s0-s2)^2+ (s1-s3)^2. I compile it with gcc (no...
|
by: sylsau |
last post by:
Hi,
I am doing a little program who calculates the permutation of a set of
vertex.
I use the recursivity for this calcul.
My function who calculate the permutations :
void permutation(set...
|
by: Andy Carlson |
last post by:
I have a strange problem. I am trying to use a global variable, but I
can't get the second routine to recognize changes to the globabl. I
finally realize, that I was forking, so the global was...
|
by: angryRotarian |
last post by:
Im trying to write a MPI C program for a debian cluster that multiplies matricies. Ive written almost all of it, but cant seem to shake a strange problem. Here is the code:
#include <stdio.h>...
|
by: MN |
last post by:
Hi,
I was writing a program that must dynamically allocate memory to a 2-
dimensions array, initialize it to 0, then place the result of
multiplication of two 1-dimension arrays as described in...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |