Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic memory allocation problem! 2

Status
Not open for further replies.

sachindn

Programmer
Jun 27, 2000
10
US
Dear Friends,
I want to allocate a large chunk of memeory of about 200K dynamically.
When I call malloc its not able to allocate that much memory and neither its returning a null pointer.
Is there any other way to make sure that it allocates that much memory?
my code is

char *p;
p=(char *)malloc(int+int);
Can anybody plz tell me what is the input for malloc
wether its int or long?
Regards [sig][/sig]
 
Hi,

A couple of comments:

Make sure you include <stdlib.h> when using malloc().

Casting the return value of malloc() isn't necessary. malloc() returns a pointer to void and is guaranteed to be in the proper alignment for whatever data type you're using.

malloc() takes a size_t data type, which (I think) is guaranteed to hold the size of a long integer. Of course, the size of a long integer is implementation defined.

Here's how you could make your call to malloc() for a 200K buffer.

char *p=malloc(200000 * sizeof(char));

/* Also, make sure you check the return value: */

if (p==NULL) {
perror(&quot;malloc failed\n&quot;);
exit(1); /* or whatever's appropriate */
}

...

/* And when you're done with it, to free it: */

free(p);

Regards, [sig]<p>Russ<br><a href=mailto:bobbitts@hotmail.com>bobbitts@hotmail.com</a><br><a href= is in</a><br>[/sig]
 
oops...meant:

char *p=malloc(200001*sizeof(char *));

of course, it's better to use a define rather than a magic number:

#define BUFFERSIZE 200000

...

char *p=malloc((BUFFERSIZE+1)*sizeof(char *));

Regards, [sig]<p>Russ<br><a href=mailto:bobbitts@hotmail.com>bobbitts@hotmail.com</a><br><a href= is in</a><br>[/sig]
 
What operating system and compiler are you using?

If you are on a DOS/Windows system, you may be using a 16-bit development tool which has 64K segmentation issues.

Try allocating a little less than 64K and then try allocating a little more than 64K. If the first works and the second doesnt, this is your problem.

You will need to get a 32-bit compiler. DJGPP is a pretty good C/C++ compiler based on the *nix compilers, and is freely downloadable (although I dont have the URL right now. i will try to look for it later if you need it)


Hope this helps,
Gerald
[sig][/sig]
 
Hi ,
rbobbitt you were right for the first time .
The actual usage of malloc for char* p is

p = (char*) malloc(sizeof(char)); and not sizeof(char*).

It is required when you want to create memory for 2 dimension array of POINTERS.
For eg, char **p;

p = (char**) malloc(sizeof(char*) * 10); // suppose 10 rows are required .
Then say for each row 20 columns are required , then
for(int i = 0; i< 10; i++)
p = (char*) malloc(sizeof(char) * 20);
This will do the job.

Regarding sachindn's problem . Just check what is the size of your memory. And also close all the program . Because I checked it for 200K bytes . It worked properly.

Does this answer your question?
Thanks
Siddhartha Singh
[sig]<p>Siddhartha Singh<br><a href=mailto:siddhu_singh@hotmail.com>siddhu_singh@hotmail.com</a><br><a href=siddhu.freehosting.net> </a><br> [/sig]
 
Hi,

Yes, my mistake on using sizeof(char *) instead of sizeof(char) ... However, it turns out that sizeof(char) is basically pointless as it will always evaluate to 1.

so:

p=malloc(SIZE_OF_BUFFER+1);

is actually sufficient.

Again, casting the return value of malloc() isn't necessary and is even discouraged nowadays, although it used to be required before the introduction of the void pointer.

Also, I'm not sure I understand what you're doing in your little sample program. First you allocate 10 bytes of memory to p. then you allocate 20 bytes of memory to p 10 times (in the loop). However p points to the same region of memory for the duration of the program.

Regards, [sig]<p>Russ<br><a href=mailto:bobbitts@hotmail.com>bobbitts@hotmail.com</a><br><a href= is in</a><br>[/sig]
 
I think that the reason why you couldn't see the meaning rbobbitt, is that tek-tips uses the same code for italic text-formatting as the index for i.

So I would suggest not to use i inside [] here :)

With counter instead it'll look better:

for(int counter = 0; counter < 10; counter++ )
p[counter] = (char*) malloc(sizeof(char) * 20);


 
Ok, that makes a more sense. :)

Although, again, making casts from malloc() is unnecessary and generally a bad idea.

In any case, I'm afraid we've strayed from the OP's original question which doesn't have anything to do with multi-dimensional arrays -- he just wanted to obtain a pointer to 200K of allocated memory.

Russ
bobbitts@hotmail.com
 
dear rbobbit,
most compilers would give u an error if u do not cast malloc()
anindyakar
 
Any compiler that does is not ANSI-compliant, and most popular modern compilers are (or claim they are) C89 compliant -- gcc, djgpp (gcc port to Windows), MSVC++ etc. Which compilers and versions are you talking about?

Russ
bobbitts@hotmail.com
 
It should not give you an error but it will give you a warning:

warning: assignment makes pointer from integer without a cast

this on the latest version of pgcc and whatever version of gcc that comes from redhat 6.2 on va linux machines.

just annoying to me. i would be happier if my compiler didnt give me any lip about my malloc statements. heh
 
you're probably forgetting to #include <stdlib.h>

If you don't do this, the compiler will generate a diagnostic because you haven't provided it with the proper declaration for malloc().

I get no warnings with the same compiler you're using when including <stdlib.h>.

Regards,

Russ
bobbitts@hotmail.com
 
Ahhhhhh...
Well I never claimed to be the sharpest knife in the drawer... heh... more like a spoon... :(



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top