Understanding Output for Malloc and Calloc Pointers?

  • Thread starter transgalactic
  • Start date
  • Tags
    Code
In summary, the programmer is trying to use malloc and calloc to allocate memory, but is getting incorrect results.
  • #1
transgalactic
1,395
0
Code:
#include <stdio.h>
#include <stdlib.h> 
/* required for the malloc, calloc and free functions */

int main() {
  float *calloc1, *calloc2, *malloc1, *malloc2;
  int i;

  calloc1 = calloc(3, sizeof(float)); /* might need to cast */ 
  calloc2 = calloc(3, sizeof(float));
  malloc1 = malloc(3 * sizeof(float));
  malloc2 = malloc(3 * sizeof(float));

if(calloc1!=NULL && calloc2!=NULL && malloc1!=NULL && malloc2!=NULL) {

    for(i=0 ; i<3 ; i++) {
      printf("calloc1[%d] holds %05.5f, ", i, calloc1[i]);
      printf("malloc1[%d] holds %05.5f\n", i, malloc1[i]);
      printf("calloc2[%d] holds %05.5f, ", i, *(calloc2+i));
      printf("malloc2[%d] holds %05.5f\n", i, *(malloc2+i));
    }

    free(calloc1);
    free(calloc2);
    free(malloc1);
    free(malloc2);

    return 0;
  }
  else {
    printf("Not enough memory\n");
    return 1;
  }
}

Output:
calloc1[0] holds 0.00000, malloc1[0] holds -431602080.00000
calloc2[0] holds 0.00000, malloc2[0] holds -431602080.00000
calloc1[1] holds 0.00000, malloc1[1] holds -431602080.00000
calloc2[1] holds 0.00000, malloc2[1] holds -431602080.00000
calloc1[2] holds 0.00000, malloc1[2] holds -431602080.00000
calloc2[2] holds 0.00000, malloc2[2] holds -431602080.00000


at first they build 4 pointers
each one of them points to the start of a sector whose size is 3 float variables.

then the print some how
calloc1 should return only the address

*(calloc2+i) should return the data inside the address

but they get similar output

i can't understand these printf lines

??
 
Technology news on Phys.org
  • #2
transgalactic said:
calloc1 should return only the address

*(calloc2+i) should return the data inside the address


Err, no. Both of those commands are equivalent, and return the data at the i-th element of the array calloc1.

If you want to return the address, use &calloc1 or calloc1+i.
 
  • #3
the calloc needs to return an address to which every cell is empty
but when we print calloc1 (which is the content of cell "i")
we get the value -431602080.00000 instead of empty or some thing like that

why is that?
 
  • #4
malloc and calloc are equivalent except that calloc also does a memset() on the allocated memory. In fact on most unix implementations they are identical - malloc will also return memory that is zeroed.
You are printing a float representation of some random memory contents (possibly all 1s?) only all bits zero is guaranteed to produce a float output of zero.
 
  • #5
mgb_phys said:
malloc and calloc are equivalent except that calloc also does a memset() on the allocated memory. In fact on most unix implementations they are identical - malloc will also return memory that is zeroed.
You are printing a float representation of some random memory contents (possibly all 1s?) only all bits zero is guaranteed to produce a float output of zero.

That was also my understanding mgb. I wonder why it is that transg was getting calloc1 = -431602080.00000 in this case? (assuming some valid "i").
 
  • #6
He isn't - all the calloc values are printing 0.0. That's part of IEEE float standard, all zero bits gives 0.0, the malloc are printing some garbage bcause the memory is initialised to something else.
Win32 inits malloc memory to 0xCE (or similair) so you know where it came from, some IBM machines used to use 0xDEADBEEF
 
  • #7
mgb_phys said:
malloc and calloc are equivalent except that calloc also does a memset() on the allocated memory. In fact on most unix implementations they are identical - malloc will also return memory that is zeroed.

Which causes all kinds of havoc when porting between Unix compilers and, say, MSVC, as Unix developers tend to get into the habit of using malloc for everything. But then, I suppose that's what purify is for...
 
  • #8
transgalactic said:
then the print some how
calloc1 should return only the address

*(calloc2+i) should return the data inside the address


There is absolutely no difference between *(calloc1+i) and calloc1 -- or i[calloc1], for that matter. The square brackets in C are just syntactic sugar1, unfortunately. The compiler immediate translates foo[bar] to *(foo+bar).


1 Except when they aren't syntactic sugar, which is even more unfortunate. The square brackets mean something quite different for multidimensional arrays such as double matrix[3][3] and ragged arrays such as double **ragged. With these declarations, matrix[2][2] is exactly the same as *(matrix+2+2*3) while ragged[2][2] is exactly identical to *(*(ragged+2)+2) .
 
  • #9
mgb_phys said:
He isn't - all the calloc values are printing 0.0.

Ok but he did specifically say that this was not working for him though I agree that it should be printing zeros. Anyway I guess we can put this down to misinformation from a noob user in transgalactic?
 

1. What is the difference between malloc and calloc?

Malloc and calloc are both functions in the C programming language used for dynamic memory allocation. The main difference is that malloc allocates a single block of memory of a given size, while calloc allocates multiple blocks of memory of a given size and initializes them to 0.

2. Why do I sometimes get a segmentation fault when using malloc or calloc?

A segmentation fault occurs when a program tries to access memory that it does not have permission to access. This can happen with malloc or calloc if the allocated memory is accessed incorrectly, such as trying to write to a read-only memory block or accessing memory that has already been freed.

3. Is it necessary to check the return value of malloc or calloc?

Yes, it is important to check the return value of malloc or calloc to ensure that the memory allocation was successful. If the allocation fails, the function will return a null pointer, and attempting to use this pointer can lead to unexpected errors or crashes.

4. How do I free memory allocated with malloc or calloc?

To free memory allocated with malloc or calloc, the function free() should be used. This returns the allocated memory back to the system, making it available for other processes to use. It is important to only free memory that has been allocated with malloc or calloc, as attempting to free static or stack memory can lead to errors.

5. Can I use malloc or calloc in C++?

Yes, both malloc and calloc can be used in C++, but they are not the preferred methods for dynamic memory allocation in this language. C++ provides its own memory allocation operators, such as new and delete, which are more convenient and provide additional features such as exception handling.

Similar threads

  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
1
Views
944
  • Programming and Computer Science
Replies
2
Views
933
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
670
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Programming and Computer Science
4
Replies
119
Views
15K
Back
Top