Solved

Allocation memory

Posted on 1998-06-11
5
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

749 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