473,386 Members | 1,795 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,386 software developers and data experts.

Strange malloc problem

Me
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.
Jun 20 '06 #1
9 1914

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?

Jun 20 '06 #2
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
==============
Jun 20 '06 #3
Me
"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.
Jun 20 '06 #4
Me
"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.
Jun 20 '06 #5
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/

Jun 20 '06 #6

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).

Jun 20 '06 #7
Me
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 !
Jun 20 '06 #8
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/


Jun 20 '06 #9
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"?
Jun 23 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
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...
6
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...
11
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...
16
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...
10
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...
9
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...
2
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...
2
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>...
5
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
0
jinu1996
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 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.