Strange malloc problem

c

    Sponsored Links

    Next

  • 1. loop in a single linked list
    Hi All, here i have a daught how can i find wheather a linked list contains a loop or not.
  • 2. design question
    I have defined as structure to expose some functionality: typedef struct { /* methos */ irc_session_t* (*get_session) (const char* irc_server); void (*add_session) (irc_session_t* session); int (*contains_session) (const char* irc_server); } IRCConnectionManager; After proprely initializing an IRCConnectioManager, the above functionality should be implemented. My question is, how can I maintain the underlining data structure for an IRCConnectionManager? I was thinking that I could use a GHashTable which would map a irc_server string to a irc_session_t structure. Could someone assist me in defining a .h and .c file for this to work? I would like the implementation of an IRCConnectionManager to be hidden from anyone using it. e.g. I do not want to expose the GHashTable. I basically want to add some hidden members to the IRCConnectionManager class. One of these would be the GHashTable. Here is what I am trying to accomplish: IRCConnectionManager* conn_manager = init_conn_manager(); client->create_connection(conn_manager, "irc.server.com"); The create_connection function would call get_session on the conn_manager and essentially add the string "irc.server.com" to the hashtable. Thanks, Matt
  • 3. arrays - where they finish?
    I want to know how many integers has 'array'. (int *array) Or chars has 'array' (char *array) Etc... So... while (array) { ... } ???? while (array != EOF) {...} ???? while (array != NULL) {...} ??? (I want to use the minimun possible variables)
  • 4. Problem in char " ?"
    Hi, The charecter/symbol " ?" writen in a file able to get only in that system. When the file opened in other system, not able to see that charecter, insted its viewd as " ? ". So, the special char sets are different system to system? How can over come this problem , that to view this symbol " ?" using the same kind of editor in any system. Its very urgent, Sooner reply will be very greatfull. Thanks, Sweety
  • 5. A volunteer for any C project with Linux
    Hi folks, I'm a new 'C' student and just in the first steps...I know a few thing 'bout 'C' and have huge amount of will to learn more and more. I have a good experience in programming logic, I'm PHP programmer with 4 years of exp. Some syntax is almost the same thing in those 2. I want to learn as much as I can, and I'm askin' 4 anyone if can give me a chance to learn and give some time in it. Even if I begin translating some docs (Brazillian Portuguese), dont care, just to learn :) Tnx for the attention, sorry boring u all

Strange malloc problem

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

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. 

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.

PS
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






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!


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 
(lcdata==NULL)

Thanks for the help people !

Re: Strange malloc problem

Postby Quas.co.ua » 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)
	{
	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://www.**--****.com/ 

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



Re: Strange malloc problem

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

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!






  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.


  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,
perhaps?


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

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.

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

-- 

Regards,

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.


2.free'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 
first?

Thanks.
-cjl 


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?

   free(data[0]);
   free(data[1]);
   free(data[2]);
        ...
        ...

I appreciate your input.

Peter

4.to 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
strategies:

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

*or*

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.

TIA!

kynn

-- 

5.porting non-malloc code to malloc

Hi,
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]

-madan

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 41 guest