malloc problem

T

Thierry

Hi,

Here is my source code :

void getData(char *theData)
{
theData = malloc(500);
strcpy(theData,"hello world...");
printf("\n%s\n",theData); // OK
}

void main()
{
char *data;
getData(&data);
printf("\n%s\n",data); // = NULL ???
}


malloc is ok, into the function I get what I want, but data is equal
to NULL in main().

What is the problem ?

thanks for your help.

thierry
 
E

Ed Morton

Hi,

Here is my source code :

void getData(char *theData)
{
theData = malloc(500);
strcpy(theData,"hello world...");
printf("\n%s\n",theData); // OK
}

void main()
{
char *data;
getData(&data);
printf("\n%s\n",data); // = NULL ???
}


malloc is ok, into the function I get what I want, but data is equal
to NULL in main().

What is the problem ?

I suspect your compiler is already telling you with a warning. Your parameter
for getData should be of type char **, not char *, and dereferenced accordingly.

Ed.
 
J

Joona I Palaste

Thierry said:
Here is my source code :
void getData(char *theData)
{
theData = malloc(500);
strcpy(theData,"hello world...");
printf("\n%s\n",theData); // OK
}
void main()
{
char *data;
getData(&data);
printf("\n%s\n",data); // = NULL ???
}

malloc is ok, into the function I get what I want, but data is equal
to NULL in main().
What is the problem ?

It is not enough to supply the address of data in main(). The getData()
function must use it correctly. Like so:
void getData(char **theData) {
*theData = malloc(500);
if (*theData) {
strcpy(*theData, "Hello world");
printf("%s\n", *theData);
}
}

I think a message somewhat like "Assigning new values to function
parameters is a sign of a design problem" every 5 minutes to
comp.lang.c might help a little to cure this very common newbies'
problem.

PS. You also should replace void main() by int main().
 
C

Christopher Benson-Manica

Thierry said:
void getData(char *theData)
{
theData = malloc(500);
strcpy(theData,"hello world...");
printf("\n%s\n",theData); // OK
}
void main()
{
char *data;
getData(&data);
printf("\n%s\n",data); // = NULL ???
}

What is &data? It's a pointer to a char*, right? So &data is a char**, which
is not what getData takes as a parameter.

void getData(char **theData) {
*theData=malloc(500);
if( !*theData ) return; /* check for NULL */
strcpy( *theData, "Hello, world!" );
printf( "\n%s\n", *theData );
}

Also, main() *always* returns an int. void main() is wrong.
 
A

Al Bowers

Thierry said:
Hi,

Here is my source code :

void getData(char *theData) char *getData(char **theData)
{
theData = malloc(500);
*theData = malloc(500);
/* make sure the allocations were successful */
if(*theData != NULL)
{
strcpy(theData,"hello world...");
printf("\n%s\n",theData); // OK
}
return *theData;
}
void main() int main(void)
{
char *data;
/* assign it NULL for safety */
char *data = NULL;
getData(&data);
if(getData(&data)) /* if success then print */
printf("\n%s\n",data); // = NULL ???

free(data);
return 0;
}


malloc is ok, into the function I get what I want, but data is equal
to NULL in main().

What is the problem ?

The primary error is the incorrect parameter in function getData.
It should be char **theData, not char *theData.
 
I

Irrwahn Grausewitz

Hi,

Here is my source code :
malloc is ok, into the function I get what I want, but data is equal
to NULL in main().

What is the problem ?

Joona has already answered your question.
However, here is another approach:

#include <stdio.h>
#include <stdlib.h>

#define DATA_SIZE 500

char *getData( void )
{
char *theData = malloc( DATA_SIZE );
if ( theData == NULL ) /* [1] */
exit( EXIT_FAILURE );
strcpy( theData, "hello world..." );
return theData;
}

int main( void ) /* [2] */
{
char *data;

data = getData();
printf("\n%s\n",data);

return EXIT_SUCCESS; /* [2] */
}

[1] *Always* check the return value of malloc().
[2] main() *always* returns an int (on hosted implementations).

HTH

Regards
 
C

CBFalconer

Joona said:
.... snip ...

It is not enough to supply the address of data in main(). The
getData() function must use it correctly. Like so:

void getData(char **theData) {
*theData = malloc(500);
if (*theData) {
strcpy(*theData, "Hello world");
printf("%s\n", *theData);
}
}

I think a message somewhat like "Assigning new values to
function parameters is a sign of a design problem" every 5
minutes to comp.lang.c might help a little to cure this very
common newbies' problem.

On the contrary, parameters are pre-initialized local variable,
and should be so used. What needs to be dunned into newbies is
that they don't return values. Therefore a better form of the OPs
routine would be:

char *getData(void) {
char *theData

theData = malloc(500);
if (theData) {
strcpy(theData, "Hello world");
printf("%s\n", theData);
}
return theData;
}

and the call in main should be:

theData = getdata()

with no taking of addresses nor confusion. The "theData" visible
within getData() is purely local, and has no connection with the
"theData" visible in main.

Functions primarily return values. Let them do so.
 
T

Thierry

I think a message somewhat like "Assigning new values to function
parameters is a sign of a design problem" every 5 minutes to
comp.lang.c might help a little to cure this very common newbies'
problem.

PS. You also should replace void main() by int main().

Is it not recommended to assign new value to function parameter ?

How can I do If I have 2 variables to change at one time (in a function)

For example void changeValues(char *value1, char *value2);

I think it's the right solution to do this without using global variables.

What do you think ?

Thanks for you help.

thierry
 
J

Joona I Palaste

Is it not recommended to assign new value to function parameter ?
How can I do If I have 2 variables to change at one time (in a function)
For example void changeValues(char *value1, char *value2);
I think it's the right solution to do this without using global variables.
What do you think ?

Did you have in mind this kind of solution?
void changeValues(char *value1, char *value2)
{
char tmp;
tmp = *value1;
*value1 = *value2;
*value2 = tmp;
}
If so, then please note that the above does NOT assign new values to
function parameters. It assigns new values to what the parameters
POINT TO. This is very much OK design.
 
I

Irrwahn Grausewitz

Is it not recommended to assign new value to function parameter ?
How can I do If I have 2 variables to change at one time (in a function)

For example void changeValues(char *value1, char *value2);

I think it's the right solution to do this without using global variables.
What do you think ?

Alternatively, you may pass|return a structure to|from the function.

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,085
Messages
2,570,597
Members
47,219
Latest member
Geraldine7

Latest Threads

Top