Solved

Allocation memory

Posted on 1998-06-11
5
238 Views
Last Modified: 2010-04-01
What does the (char *) do in this sample program?

#include<stdlib.h>

main()
{
char *str;
if ((str= (char *) malloc(100)) ==NULL)
 {
   printf("not enough memory to allocate buffer\n");
   exit(1);
 }
printf("String was allocated");
return 0;
}
0
Comment
Question by:program
  • 3
  • 2
5 Comments
 

Author Comment

by:program
ID: 1165701
Edited text of question
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1165702
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165703
The return value of malloc() is a pointer of type "void *".  This means it doesn't point to any defined type.  This is because malloc doesn't know what you want to use the memory for, so it returns this, sort of, generic pointer.    you must convert this pointer to something useful.  You do this using a type cast, which is to put a type name inside of parenthesis before the thing to be changed.  For example.

int i;
char *CPtr1  = &i; // error &i is an int* pointer, not a char * pointer.
char *CPtr2 =  (char *) &i; // Okay converted from int * to char *.

you can also use this with non-pointers, like

double D = 5;
char c = (char) D; // convert D to a char.

0
 

Author Comment

by:program
ID: 1165704
Thanks for the answer.  This question stumped many people at my
office who have C knowledge.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165705
One thing I should have said and didn't is that the new operator is type safe and malloc is not.   That is new creates memory of a particular type and returns a pointer to that type.  This is much better than malloc.  A C++ program should never need to use malloc()  (except in the case of implimenting operator new).
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now