1

I have created a struct which stores an integer, and then in a method I set the integer to a value. However, when I try to access the value in any other method, I'm given a large value that isn't the same.

typedef struct thing {
    int size;
    arraylist** list; // defined elsewhere
} thing;

// Creates a thing
void create (thing* table) {
    table = (thing *)malloc(sizeof(thing) * 8);
    table->size = 1;
    printf("%d", table->size); // prints 1
}

void another (thing* table) {
    printf("%d", table->size); // prints a large number
}

void main() {
    struct thing a;
    create(&a);
    printf("test: %d", (&a)->size); // prints a random large number, ex. 1667722352
    another(&a);
}
7
  • 1
    Why are you mallocing anything? Commented Apr 19, 2014 at 19:06
  • I don't think size is defined in table = (thing *)malloc(sizeof(thing) * size); Commented Apr 19, 2014 at 19:06
  • 1
    You allocate memory for what? thing a; (no need for struct) is on the stack, and you're trying to allocate memory for it in create(). Remove malloc line, and it may work. Commented Apr 19, 2014 at 19:08
  • And the a in create(&a) is not used. Commented Apr 19, 2014 at 19:09
  • I'm going to edit my original code version. size was a variable I was using but removed for the sake of the example, and I am malloc-ing because it also includes an arraylist buffer. Commented Apr 19, 2014 at 19:11

2 Answers 2

1

You're overwriting a variable on the stack which giving you unexpected values later on.

In you're main function, you declare a struct thing on the stack.

void main() {
    struct thing a;  // Stack allocated variable
    create(&a);
    printf("test: %d", (&a)->size); // prints a random large number, ex. 1667722352
    another(&a);
}

All of the memory is already there, however you then take the address of the variable and pass that along to other functions. Now, you pass that address (by value) to create, call malloc and replace that reference. The address you've just assigned is not the address from the object on the stack. And, because you haven't actually initialized the object on the stack, you end up printing out garbage values.

I'm not sure what you're trying to do, but one way to fix this exact instance is by not mallocing new memory and just using what you have on the stack.

// Creates a thing
void create (thing* table) {
    // table already has memory
    table->size = 1;
    printf("%d", table->size); // prints 1
}    
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This was the result of ollowing an example online that doesn't match what I'm trying to do.
1

You probably want another level of indirection:

typedef struct thing {
    int size;
} thing;

// Creates a thing
void create(thing **table) {
    *table = malloc(sizeof(thing) * size); // don't know what size is. 1?
    (*table)->size = 1;
    printf("%d", (*table)->size); // prints 1
}

void another(const thing *table) {
    printf("%d", table->size); // prints a large number
}

void main() {
    struct thing *a;
    create(&a);
    printf("test: %d", a->size); // prints a random large number, ex. 1667722352
    another(a);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.