473,546 Members | 2,644 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*si zeof(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 1935

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*si zeof(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*si zeof(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****@btopenw orld.com> wrote in
news:11******** **************@ i40g2000cwc.goo glegroups.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*si zeof(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.u k:
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*si zeof(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****@btopenw orld.com> wrote in
news:11******** **************@ i40g2000cwc.goo glegroups.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****@btopenw orld.com> wrote in
news:11******** **************@ i40g2000cwc.goo glegroups.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*si zeof(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.co m> wrote in
news:Xn******** *************** ****@194.109.13 3.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*si zeof(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****@btopenw orld.com> wrote in
news:11******** **************@ i40g2000cwc.goo glegroups.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=(unsigne d 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****@btopenw orld.com> wrote in
> news:11******** **************@ i40g2000cwc.goo glegroups.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=(unsign ed 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 "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Jun 23 '06 #10

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

Similar topics

13
1848
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 message is all about. Any ideas? error: invalid initialization of non-const reference of
6
2922
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 have better luck here. So here goes! My program ignores any command line arguments, or at least it's supposed to. However, when I pass any command...
11
1655
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 *ylist are ints or floats there is no apparent problem. If these variables are doubles, the program crashes. Furthermore, the crashes occur only when...
16
1959
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 slots. When N is odd, the program will work just fine, however when N is even, it will not work.
10
2566
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 optimization). the codec between /***********/ is the initialization code. What supprise me a lot is the code with initialization(io==1) is much...
9
1449
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 *e, int *current, int nbre) {
2
1683
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 probably getting copied. So, I changed it to a pointer, and malloc an int so that it can be changed. But, it still doesn't appear to be working. I...
2
2894
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> #include <stdlib.h> #include <mpi.h> #define MASTER 0 // Make code more readable with names #define OTW 1 //"..."...
5
1560
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 the next code example. My code is: #include<stdio.h> #include<stdlib.h>
0
7504
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7435
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7694
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7792
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5360
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.