Solved

Allocation memory

Posted on 1998-06-11
5
241 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

Industry Leaders: 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

696 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