12 Replies - 434 Views - Last Post: 29 January 2014 - 10:52 AM Rate Topic: -----

#1 xcool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 14-December 13

I don't understand malloc( )

Posted 28 January 2014 - 10:47 AM

I understand what malloc() function does but I don't seem to grasp is this line
books[ctr] = (struct bookInfo*)malloc(sizeof(struct bookInfo));


I am okay with everything but why to typecast malloc() ?
2nd if we use malloc(sizeof(struct bookInfo))we have to tell the function how many bytes like malloc(10 * sizeof(int)) which means ten bytes size of an integer. What appears in my mind when I say 10 bytes like ten boxes and these boxes large enough to hold an integer, right ? But in this code
 malloc(sizeof(struct bookInfo));
it is not mentioned how many bytes ? what does that mean ? I hope it's clear

This post has been edited by xcool: 28 January 2014 - 10:49 AM


Is This A Good Question/Topic? 0
  • +

Replies To: I don't understand malloc( )

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,590
  • Joined: 08-August 08

Re: I don't understand malloc( )

Posted 28 January 2014 - 11:05 AM

sizeof returns the number of bytes needed, in this case by the struct bookInfo.

http://en.wikipedia.org/wiki/Sizeof
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4141
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: I don't understand malloc( )

Posted 28 January 2014 - 11:08 AM

Quote

I am okay with everything but why to typecast malloc() ?

In a C program you shouldn't typecast the return value from malloc(), this typecast could be hiding the fact that you forgot to include the stdlib.h header file.


Jim

This post has been edited by jimblumberg: 28 January 2014 - 11:09 AM

Was This Post Helpful? 1
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: I don't understand malloc( )

Posted 28 January 2014 - 12:44 PM

View Postxcool, on 28 January 2014 - 12:47 PM, said:

it is not mentioned how many bytes ?


The sizeof actually returns the number of bytes. As a quick exploration, try:
#include <stdio.h>

#define T(x) printf("%s = %d\n", #x, sizeof(x))

int main( ) {
    int a[3];

    T(char);
    T(int);
    T(double);
    T(a);

    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,289
  • Joined: 05-May 12

Re: I don't understand malloc( )

Posted 28 January 2014 - 01:09 PM

View Postjimblumberg, on 28 January 2014 - 01:08 PM, said:

Quote

I am okay with everything but why to typecast malloc() ?

In a C program you shouldn't typecast the return value from malloc(), this typecast could be hiding the fact that you forgot to include the stdlib.h header file.


Jim


Or that you are calling malloc() in a C++ program rather than a C program.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: I don't understand malloc( )

Posted 28 January 2014 - 01:14 PM

Which is C++'s gentle way of telling you that you shouldn't be using malloc in the first place. ;)
Was This Post Helpful? 4
  • +
  • -

#7 xcool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 14-December 13

Re: I don't understand malloc( )

Posted 29 January 2014 - 12:38 AM

View Postjimblumberg, on 28 January 2014 - 11:08 AM, said:

Quote

I am okay with everything but why to typecast malloc() ?

In a C program you shouldn't typecast the return value from malloc(), this typecast could be hiding the fact that you forgot to include the stdlib.h header file.


Jim


I've heard malloc() always returns a character so my pointer will not work, because if I have an integer pointer it will only point to an integer variable. I'm confused what a horrible function, KILL IT.
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: I don't understand malloc( )

Posted 29 January 2014 - 04:13 AM

View Postxcool, on 29 January 2014 - 02:38 AM, said:

I've heard malloc() always returns a character so my pointer will not work


Don't go by hearsay, look it up: http://www.cplusplus...cstdlib/malloc/

The malloc function has ALWAYS returned a pointer. The standard is a void *.

The granularity of malloc is bytes. Indeed, in some more esoteric machines, a char may actually be more than one byte; but that's not usually the case.

Because malloc returns a void *, in C you can simply have AnyType *x = malloc(sizeof(AnyType));. The void * will become the AnyType * without complaint. Unless it's C++. C++ complains a lot. ;)

To be clear, in C++ you should NEVER use malloc; use new and delete. In C you MUST use malloc; it's what you've got. If you're uncomfortable with the idea that a programming language will allow you to corrupt memory and shoot yourself in the foot if you're not careful, then C is not for you.
Was This Post Helpful? 1
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4141
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: I don't understand malloc( )

Posted 29 January 2014 - 06:31 AM

In many ways this freedom to shoot yourself in the foot is what makes both C and C++ such powerful languages.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2133
  • View blog
  • Posts: 3,267
  • Joined: 21-June 11

Re: I don't understand malloc( )

Posted 29 January 2014 - 07:16 AM

View Postbaavgai, on 29 January 2014 - 12:13 PM, said:

Indeed, in some more esoteric machines, a char may actually be more than one byte


No, it may not. sizeof(char) is 1 by definition. However a byte may be more than 8 bits.
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,590
  • Joined: 08-August 08

Re: I don't understand malloc( )

Posted 29 January 2014 - 08:41 AM

I've assumed that since unicode characters are two bytes there might be some version of C or C++ where char was also two bytes. Maybe there isn't, but it seems clear that there should be.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,289
  • Joined: 05-May 12

Re: I don't understand malloc( )

Posted 29 January 2014 - 09:34 AM

To make that distinction between the character that traditional C programmers first learned and the storage for Unicode characters there is char and wchar_t.
Was This Post Helpful? 1
  • +
  • -

#13 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2133
  • View blog
  • Posts: 3,267
  • Joined: 21-June 11

Re: I don't understand malloc( )

Posted 29 January 2014 - 10:52 AM

View PostCTphpnwb, on 29 January 2014 - 04:41 PM, said:

I've assumed that since unicode characters are two bytes there might be some version of C or C++ where char was also two bytes. Maybe there isn't, but it seems clear that there should be.


It seems like there should be if you think of chars as characters, which doesn't seem very strange given the name. But you have to keep in mind that C does not have a "byte" type. So if chars aren't bytes, you have no way to represent bytes. So having chars not be bytes simply would not work - you would no longer be able to iterate over the bytes of something for example because you'd usually use char pointers for that, but that would no longer work (keeping in mind that you can't do arithmetic on void pointers, so you'd need to cast them to something to iterate with them).

So in summary, you need some type to represent bytes for the language to work properly and in C that type is char, which is required by the standard to be exactly one byte long.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1