Strange malloc problem


    Sponsored Links


  • 1. moving data in a file without using system memory
    Let's assume I have following file: 2938929384902491233..... 923949919199191919112.... File contains INTs only. What is more they are huge. For example first row in file may contain integer which size is 50MB and the second 30MB. Now we come to my problem. Is there possibility to swap this rows without using system memory (preferably in Unix/Linux)? Is there any function in C to do this? Thanks for help, John
  • 2. OT: Need a queue in C
    ern wrote: > I need a FIFO queue of size 20, to keep track of a running average. > Once the queue is full with 20 values, I want to take the sum/20 = > AVERAGE. Then when a new value comes in, I want: > > (Old Sum - oldest value + newest value)/20 = New Average > > Anybody know an efficient way to implement that? Is a queue even the > best way? > > Thanks, This is not really a c question other than that happens to be the language you are using but Iwould probably just use an array of 20 elements and maintain the index to the oldest element: //pseudocode //some initialization stuff comes first sum = sum - array[oldest element] + new value average = sum / array size array[oldest element] = new value if oldest element is not last element oldest element++ else oldest element = 0 Keep in mind this is off the top of my head and is not c code. The c code to do this should be straight forward and not much different than my psuedocode with some additional setup and storing the right variables in the right places but the best way for you to do that for maximum performance really depends on your system, i.e. maybe you have some HW registers you can use or something like that.
  • 3. Debug a C DLL
    Hello, I have an exe that calls a dll. The dll is dynamically loaded and I use getprocaddress to get the address of the funciton I want to call. Everything seems to work fine in debug mode but in release mode it is blowing up at the first call to the dll. Does anybody have any advice on how to narrow down the problem. I put popups into the exe code to narrow down the issue to the first dll call but I am at a loss as to what would cause debug dll to work and the release version not to work. I can generate a map but I am terrible at reading it and I function a lot better when the error occurs in debug mode because then I can use the debug tool. Would the map be the best approach or is there a better method. If I generate a map could someone help me find the correct location in the dll? Thanks Jake
  • 4. How do the compiler knows about the change that is made in the static variable?
    Hello, can anyone tell me how the c compiler remembers the changes made to the static variable? Regards, ramu
  • 5. how to allocate memory for this
    hello all, i want to know about dynamically allocating for char **ptr and in problem #include<stdio.h> main() { char **p={"hello","world"}; printf("%s",*ptr); printf("%c",**ptr); printf("%s",*(ptr+1)); } i wann to print "hello" , "h", "world" is this correct code , is ther any problem with this code; well i know while initializing tha 2d char array char *p[]={"hello","world"}; but i want to know to how i can initialize using pointer to ponter;;

Strange malloc problem

Postby Me » Wed, 21 Jun 2006 19:56:22 GMT


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>

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. 

Re: Strange malloc problem

Postby Vladimir Oka » Wed, 21 Jun 2006 20:52:31 GMT

Using `sizeof *lcdata` may be better  (easier to maintain).

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.

Do you /really/ have to read /all/ of a 1.5MB file into memory at once?

Re: Strange malloc problem

Postby pemo » Wed, 21 Jun 2006 21:23:02 GMT

What's fsize's *type* - unlikely it's overflowing, but worth knowing.

Not a pedant

Re: Strange malloc problem

Postby Me » Wed, 21 Jun 2006 22:55:35 GMT

"Vladimir Oka" < XXXX@XXXXX.COM > wrote in

I made a check for that:

if (!*lcdata)
    	<allocate error message>
    	return 0;

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.   

Re: Strange malloc problem

Postby Chris Dollin » Wed, 21 Jun 2006 22:58:04 GMT

Did you /really/ write `!*lcdata`? Not `!lcdata` (I prefer `lcdata == 0`)?

If malloc returns null, lcdata is null, and *lcdata is undefined, BOOM.

Would it? Are you sure? It depends on the problem.

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/

Re: Strange malloc problem

Postby Me » Wed, 21 Jun 2006 22:58:45 GMT

fsize is of type long and the actual test value for fsize is 1612288. 

Re: Strange malloc problem

Postby Vladimir Oka » Wed, 21 Jun 2006 23:05:40 GMT


  if (!lcdata)

If not, you may find that where `lcdata` points to in case of a large
file (by coincidence) is a NULL pointer!

Returning 0 usually means success. To be sure, return EXIT_FAILURE.

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

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

Re: Strange malloc problem

Postby Me » Thu, 22 Jun 2006 00:12:13 GMT

Me < XXXX@XXXXX.COM > wrote in

Problem solved. It was the check that did it I now changed it to if 

Thanks for the help people !

Re: Strange malloc problem

Postby » Thu, 22 Jun 2006 04:40:02 GMT

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)
	exit(error_code);//because begine of the stack in main() is already corrupted

if you wanna see interpreter of strange language wellcom to

Zis is ze let' tou censoers.
--by ManoNegra

Re: Strange malloc problem

Postby phaywood » Sat, 24 Jun 2006 13:07:07 GMT

Groovy hepcat 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!

  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

  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.

  And what is that, exactly; a macro expanding to something useful,

  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://www.**--****.com/ ~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"?

Similar Threads:

1.Strange thing about malloc()


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.

Is it just a Win32-issue, or a bug?



Ronny Mandal

This e-mail and any attachment are confidential and may be privileged or 
otherwise protected from disclosure. It is solely intended for the person(s) 
named above. If you are not the intended recipient, any reading, use, 
disclosure, copying or distribution of all or parts of this e-mail or 
associated attachments is strictly prohibited. If you are not an intended 
recipient, please notify the sender immediately by replying to this message 
or by telephone and delete this e-mail and any attachments permanently from 
your system.'ing malloc'd structure with malloc'd members

In the course of an assignment, I learned the hard way that I shouldn't try 
to free a malloc'd member of a malloc'd structure after having freed that 
structure  (i.e., free( structure );  free( structure->bufferspace ) ).

My question is, if I free just the structure, will the (e.g.) bufferspace be 
freed implicitly, or do I have to (as I currently am) free the members 


3.Malloc/Free - freeing memory allocated by malloc

Is it really necessary to call free()? Can I make the assumption that
the OS will do the clean up once the program terminates?

For a linked list, I can step through the list and free one node at a
time, but what about a dynamic array created by malloc?

   int *data = malloc(100 * sizeof(int));

Does free(data) frees all 100 elements or only the first one? Do I
have to do the following in a loop?


I appreciate your input.

Peter malloc or not to malloc?

I used to program C all the time, but that was years ago.  Since
then, continued use of languages like Java that take care of memory
management have made me soft and flabby.  Now I have to code
something in C, and I get the feeling that I'm courting memory
leaks and segmentation faults at every turn...

I'm writing a (Flex/Bison) parser that reads some serialized
representation of an arbitrarily complex C data structure, and
cranks out the "revived" data structure at the other end.  So this
code must generate a ton of C data elements (integers, floats,
strings, etc.), and compose them in arbitrarily convoluted ways.

Suppose I have this structure

typedef struct {
  int bar; 
  baz *frobozz;
} foo;

to represent one of these data elements.  Now, I want to define a
constructor that will produce these things.

Here's where I feel particularly rusty: to malloc or not to malloc.

More specifically, what would be better of these two general

foo new_foo(int bar, baz *frobozz) {
  foo ret; = bar;
  ret.baz = copy_a_baz(frobozz);
  return ret;


foo *new_foo(int bar, baz *frobozz) {
  foo *ret;
  ret = (foo *)malloc(sizeof *ret);
  if (!ret) kick_up_a_fuss();
  ret->bar = bar;
  ret->bar = copy_a_baz(frobozz);
  return ret;

My gut feeling is that the second one is the way to go, but I
confess that I could not provide a very solid defense for this
preference.  At best I'd mumble something indistinct about stacks
and heaps, and change the subject...

What really throws me off is to consider the simpler case of
implementing a constructor for, e.g., integers.  In this case, the
gut response I described above feels all wrong.  For example, it
feels weird to implement new_int like this:

int *new_int(char *int_as_string) {
  int *ret;
  ret = (int *)malloc(sizeof *ret);
  if (!ret) kick_up_a_fuss();
  *ret = (int)strtol(int_as_string, NULL, 10);
  return ret;

Instead, in this case my instinct would be to simply write

int new_int(char *int_as_string) {
  int ret = (int)strtol(int_as_string, NULL, 10);
  return ret;

or better yet

int new_int(char *int_as_string) {
  return (int)strtol(int_as_string, NULL, 10);

Okay, my state of confusion should be evident by now.  Any words
of wisdom would be much appreciated.




5.porting non-malloc code to malloc

I want to post some code which has defn like

struct AVPicture {
     int *data[4];
     int linesize[4];
the is noe recognised by matlab, so i need to port to some form like
struct AVPicture {
     int **data;
     int *linesize;

Now whatz the best way to go about this process

1. write a wrapper header, but how to initialize the constants ?? like
.. **data to *data[]
2. rewrites the source .. [impossible]


6. to malloc or not to malloc??

7. malloc rare error (malloc bug??)

8. [Clax86list] HLA malloc problem

Return to c


Who is online

Users browsing this forum: No registered users and 80 guest