Question

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

struct myStruct
{
    int number;
};

void
allocateMem(struct myStruct *struct1)
{
    struct1 = malloc(sizeof(struct myStruct));
    struct1->number = 500;
    printf("struct1->number: %d\n", struct1->number);
}

int
main(int argc, char *argv[])
{
    struct myStruct *struct1 = NULL;
    allocateMem(struct1);
    printf("number: %d\n", struct1->number);
    return 0;
}

Hello citizens of StackOverflow, I seek your assistance.

What I am trying to do is allocate a struct using heap space rather than stack space; and I am trying to do the allocation inside of a function. No matter how much I mess with this I always get either a segmentation fault or several compiler warnings.

I'm doing something here incorrectly, but I can't figure out what, I've been all over StackOverflow and the general internet trying to find an example or post that relates to my issue but I have not found anything.

If you could tell me what I'm doing wrong, or just point me in the right direction I would be extremely grateful.


Thank you everyone for your quick and accurate replies. You were correct, I needed a pointer to a pointer, my code works now thanks to your help.

Était-ce utile?

La solution

In this function,

void
allocateMem(struct myStruct *struct1)
{
    struct1 = malloc(sizeof(struct myStruct));
    struct1->number = 500;
    printf("struct1->number: %d\n", struct1->number);
}

struct1 is passed by value. Any changes you make to it in the function are not visible from the calling function.

A better alternative:

struct myStruct* allocateMem()
{
    struct myStruct *struct1 = malloc(sizeof(struct myStruct));
    struct1->number = 500;
    printf("struct1->number: %d\n", struct1->number);
    return struct1;
}

Change the calling function to:

int
main(int argc, char *argv[])
{
    struct myStruct *struct1 = allocateMem();
    printf("number: %d\n", struct1->number);

    // Make sure to free the memory.
    free(struct1);

    return 0;
}

Autres conseils

A pointer is just an integer in reality, when you pass in struct1 to your function since struct1 is null you are just passing in a 0 to your function in reality. that value gets passed on the stack and you do a heap allocation and update the stack value with the new address. but when you return from the function, that value just gets popped of the stack and you have leaked that memory. the value of struct1 in main (which is also on the stack still keeps its value 0(NULL). so you have to pass in a pointer to your pointer if you want to update that value, or what might be easier is just to return the malloc'd pointer from your function.

This is one way you could modify your function to work:

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

struct myStruct
{
    int number;
};

struct myStruct*
allocateMem()
{
    struct myStruct* struct1 = malloc(sizeof(struct myStruct));
    struct1->number = 500;
    printf("struct1->number: %d\n", struct1->number);
    return struct1;
}

int
main(int argc, char *argv[])
{
    struct myStruct *struct1 = allocateMem();
    printf("number: %d\n", struct1->number);
    return 0;
}

Because you are changing the struct1 pointer in the method, you need to either return the pointer to the caller, or pass it as a double pointer (address of the caller's pointer), e.g.

Return:

struct myStruct* allocateMem()
{
    struct myStruct* struct1 = malloc(sizeof(struct myStruct));
    struct1->number = 500;
    printf("struct1->number: %d\n", struct1->number);
    return struct1;
}

Double pointer:

void allocateMem(struct myStruct **struct1) 
{

With the allocation and usage as per @Grijesh's comments below.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top