473,385 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

malloc and calloc reg

Hai,
This situation will never arise in computers.But still to understand the difference between malloc and calloc i just visualised the situation.The situation is"consider the RAM in computer which has only bytes 10000 to 10007 and 10028 and 10029 left free.All other portion of memory are used for other purposes."Now what will happen if i give malloc(10) and calloc(5,2)?
One of my friends said that malloc will not allocate memory as there is no continuous group of 10 bytes.Yet calloc will allocate memory.I have two doubts arising from that?They are
1)Is that correct?
2)If so, what will happen if the ram has memory locations 10000 to 10006 and 10027 to 10029 left free,
all other portion of memory are used for other purposes and i give the command calloc(5,2)?Will it allocate memory or not?
Thanks for your reply in advance.
Sep 6 '12 #1
9 2083
weaknessforcats
9,208 Expert Mod 8TB
malloc allocates in contiguous bytes. That is, each byte is adjacent to the next.

It must do this or you couldn't do pointer arithmetic on the allocation which would mean you couldn't use malloc for arrays.

calloc just calls malloc but sets each allocated byte to binary zero.

calloc(5.2) is senseless. The 5.2 would get converted to an int of 5 which would prompt a compiler warning about potential loss of data.

It is common for memory allocations to leave bytes here and there in a non-allocatable state.
Sep 7 '12 #2
hai,
do you mean calloc too cant allocate memory when there is no continuous group of memory which is left free.
Sep 9 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
Yes.

All memory alocators need continuous memory locations. Otherwise you have no idea how to use pointers inside that allocation.

Always check the return value of malloc and calloc. A return of zero means no allocation possible. A one means continuous bytes were found.
Sep 9 '12 #4
Hi there,

I think have 2 things of different from malloc and calloc as below:

1.
- malloc allocates memory to basic type as int, float...
- calloc is familiar with malloc but it can allocate memory to user define type as structure type, union type...

2.
- malloc allocates a array of bytes of memory
- calloc allocates a block of size bytes of memory
Sep 10 '12 #5
weaknessforcats
9,208 Expert Mod 8TB
Allocations are made in bytes. Not types.

Look at how an allocation is made. For 100 ints you code
100 * sizeof(int). For a struct you code
100 * sizeof(MyStruct). Every allocation using a type uses sizeof to get the correct number of bytes for that type.

There is no special formatting for various types. You use a type to determine how to use those bytes. If you say 4 bytes are an int then there is an assumed sign bit, which if you never initialize, will have the same it had when the allocation was made.

There's no difference between a "block of bytes" and an "array of bytes". That's just two names for the same thing.
Sep 10 '12 #6
Hi weaknessforcats,

Thank for your illustration but I didn't said "Allocations are made in bytes", ok?

I said:
"malloc allocates a array of bytes of memory" // actually, I mean that malloc will allocate in contiguous bytes // as when you declare a array
Sep 11 '12 #7
weaknessforcats
9,208 Expert Mod 8TB
True. But I was referring to allocation of bytes for a struct type. There may be no array in this case but the bytes are still contiguous.
Sep 11 '12 #8
donbock
2,426 Expert 2GB
@nextstep: your first post suggested there is a difference in what a program can do with the memory allocated by malloc or calloc. This is emphatically not the case. Memory allocated by either function can be used to hold any data type(s).

The one and only difference between these functions is that the memory provided by malloc is uninitialized and could contain any random value; while the memory provided by calloc is initialized to all zeroes.

It is entirely possible that neither function provides a legitimate initial value. For example, suppose you ask these functions to allocate memory for an enumerated type that takes the values 1, 2, and 3. malloc is unlikely to provide one of those values and calloc is guaranteed not to.
Sep 12 '12 #9
@donbock

You are right as manpage (netbsd) explains as below.

Expand|Select|Wrap|Line Numbers
  1.      The malloc() function allocates size bytes of uninitialized memory.  The
  2.      allocated space is suitably aligned (after possible pointer coercion) for
  3.      storage of any type of object.
  4.  
  5.      The calloc() function allocates space for number objects, each size bytes
  6.      in length.  The result is identical to calling malloc() with an argument
  7.      of ``number * size'', with the exception that the allocated memory is
  8.      explicitly initialized to zero bytes.
  9.  
Sep 12 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Koster | last post by:
Sorry for the re-post, but my previous question was left unanswered. I have a question about the appropriateness of calloc. Consider an array of pointers to structs which need to be allocated...
26
by: dagger | last post by:
Hi there. I'm using C under FreeBSD with the gcc compiler and am having a bit of trouble using the calloc and realloc calls. As an example the code snippet: #include <stdio.h> int main() {...
16
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
27
by: MK | last post by:
I am a newbie. Please help. The following warning is issued by gcc-3.2.2 compiler (pc Linux): ================================================================== read_raw_data.c:51: warning:...
8
by: EvilRix | last post by:
I've always casted the result of a call to c/malloc viz.: int * ptr = (int *) calloc(10, sizeof(int)); However, I know this is not strictly necessary as there is always an implied cast on the...
6
by: Ramasubramanian XR (AS/EAB) | last post by:
What is diff b/w malloc,calloc,realloc could any one explain
7
by: mef526 | last post by:
I have had this problem for months now and it has been nagging me. I have a large project that has several C++ DLL's, one of them uses malloc / calloc / free for the other DLL's. I process very...
14
by: Roka100 | last post by:
Hi all, I tried 2 programs : #include <stdio.h> #include <string.h> 1, int main(void){ char *str = NULL;
47
by: Raman | last post by:
Hi All, As per my understanding, malloc and calloc differs in the way they allocate memory( malloc give a contigous block, But calloc may give Non-contigous block as well). Now consider this...
5
by: reachanil | last post by:
Hi, We've interposed malloc/calloc/realloc/memalign/valloc in our application. While all of our application calls our own implementation of these functions, there seems to be some issue with the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.