×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Dynamic memory allocation problem!
2

Dynamic memory allocation problem!

Dynamic memory allocation problem!

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

RE: Dynamic memory allocation problem!

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("malloc failed\n");
    exit(1);   /* or whatever's appropriate */
}

...

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

free(p);

Regards,

Russ
bobbitts@hotmail.com
Bobbitt is in

RE: Dynamic memory allocation problem!

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,

Russ
bobbitts@hotmail.com
Bobbitt is in

RE: Dynamic memory allocation problem!

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

RE: Dynamic memory allocation problem!

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

Siddhartha Singh
siddhu_singh@hotmail.com

RE: Dynamic memory allocation problem!

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,

Russ
bobbitts@hotmail.com
Bobbitt is in

RE: Dynamic memory allocation problem!

HEY
THE PROBREM...........

RE: Dynamic memory allocation problem!

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


RE: Dynamic memory allocation problem!

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
http://home.earthlink.net/~bobbitts

RE: Dynamic memory allocation problem!

dear rbobbit,
             most compilers would give u an error if u do not cast malloc()
                  anindyakar

RE: Dynamic memory allocation problem!

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
http://home.earthlink.net/~bobbitts

RE: Dynamic memory allocation problem!

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

RE: Dynamic memory allocation problem!

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
http://home.earthlink.net/~bobbitts

RE: Dynamic memory allocation problem!

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



Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close