Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

malloc inside a function 1

Status
Not open for further replies.

indieboy

Programmer
Nov 21, 2003
2
BR
could anyone tell me what's wrong about the use of pointers and malloc. =)
in function alloc_memory the memory is allocated just fine, but when it returns, the pointers test and test->test are 0x00 =(((
Code:
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    char *value1;
    char *value2;
}TEST2;
    

typedef struct
{
    char *value;
    TEST2 *test;
} TEST;

TEST *meu_test;

void alloc_memory(TEST *test)
{
    test = (TEST *) calloc(1, sizeof(TEST));
    test->value = (char *)calloc(100, 1);
    test->test = (TEST2 *) calloc(1, sizeof(TEST2));
    test->test->value1 = (char *)calloc(100,1);
    test->test->value2 = (char *)calloc(100,1);
}
void set_values(TEST *test)
{
    test->value = &quot;this is value&quot;;
    test->test->value1 = &quot;this is value 1&quot;;    
    test->test->value2 = &quot;this is value 2&quot;;        
}

void show_values(TEST *test)
{
    printf(&quot;TEST->value: %s\n&quot;, test->value);
    printf(&quot;TEST->test->value1: %s\n&quot;, test->test->value1);
    printf(&quot;TEST->test->value2: %s\n&quot;, test->test->value2);
}

int main(int argc, char *argv[])
{
  alloc_memory(meu_test);
  set_values(meu_test);
  show_values(meu_test);
  system(&quot;PAUSE&quot;);	
  return 0;
}
 
The basic problem is parameter passing copies values, so alloc_memory updates its parameter value, not the value in main()
In addition, your string assignments should have been strcpy() calls

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
    char *value1;
    char *value2;
}TEST2;
    

typedef struct
{
    char *value;
    TEST2 *test;
} TEST;

TEST* alloc_memory(void)
{
    TEST *test = calloc(1, sizeof(TEST));   /* casts of malloc functions are not */
    test->value = calloc(100, 1);           /* needed in ANSI-C */
    test->test = calloc(1, sizeof(TEST2));  /* at worst, they hide errors if you */
    test->test->value1 = calloc(100,1);     /* fail to include stdlib.h */
    test->test->value2 = calloc(100,1);
    return test;
}
void set_values(TEST *test)
{
    strcpy(test->value, &quot;this is value&quot; );
    strcpy(test->test->value1, &quot;this is value 1&quot; );
    strcpy(test->test->value2, &quot;this is value 2&quot; );
}

void show_values(TEST *test)
{
    printf(&quot;TEST->value: %s\n&quot;, test->value);
    printf(&quot;TEST->test->value1: %s\n&quot;, test->test->value1);
    printf(&quot;TEST->test->value2: %s\n&quot;, test->test->value2);
}

int main(int argc, char *argv[])
{
    TEST *meu_test;
    meu_test = alloc_memory();
    set_values(meu_test);
    show_values(meu_test);
    return 0;
}

--
 
thanks a lot, but then i have another question, what if i would like to allocate memory for 2 different structs in the same function? is it impossible?
do i have to return it?
i thought that passing the structs trough pointers, i would be allocating memory for the original variable.

i just want to understand all this 'pass by reference-value' stuff

btw, i forgot to put strcpy as you advise me hehe but i'm using it in the original code (this one was just a test to post it here) my mistake
 
Here's what you need to do, This can be a difficult concept to grasp, but you need to pass the address (pass by reference) of your pointer not just the value (pass by value) so that the actual value (memory location) the pointer holds is modified.

This is the same as passing any variable by reference.

In this instance, you need to pass a pointer by reference, or a pointer to a pointer, like this:

Your alloc function becomes:

void alloc_memory(TEST **test)
{
*test = calloc(1, sizeof(TEST));
*test->value = calloc(100, 1);
*test->test = calloc(1, sizeof(TEST2));
*test->test->value1 = calloc(100,1);
*test->test->value2 = calloc(100,1);
}

Then in main, your function call is like this:

alloc_memory(&meu_test);

Hope this makes sense. I don't have a lot of time right now to discuss this further, but I'd be happy to go into more detail when I do have time.

LOL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top