1

I'm able to assign values with strdup and print the values with this: (*test + index)->email but I have no idea how to free the memory allocated to the variable email. I thought about freeing test+index but I guess this would cause a memory leak, right? Taking into account that the struct has allocated memory and each of the pointer inside it have memory allocated with strdup.

Edit:

The code is roughly like this:

struct random {
    char *email;
} Random;

void function(Random **struct) {
    char *temp = calloc(100, sizeof(char));
    *struct = calloc(5, sizeof(Random));
    for (int i = 0; i < 5; i++) {
        scanf("%s", temp);
        (*struct + i)->email = strdup(temp); //This works
    }
    free((*struct + 3)->email); //Gives segmentation fault
}

int main() {
    Random *struct;

    function(&struct)
}
5
  • 1
    Did you omit any code here? Because scanf into uninitialized temp is going to be undefined behavior. temp needs to be malloced, or made into an array, not just a char*. Also, you use *struct + i here, which makes no sense, given there is no i variable, nor a randomSize variable. Commented Sep 19, 2019 at 2:56
  • @ShadowRanger Yes, I tried to simplify the example as much as possible. I forgot the malloc part. temp is initialized with calloc. Commented Sep 19, 2019 at 2:58
  • why are you adding i ? you are going to have to put in enough code to repro the issue Commented Sep 19, 2019 at 2:59
  • 1
    You're going to need to provide an actual minimal reproducible example (make sure it compiles, runs, and has the same error as your full code), we can't just guess at what other mistakes you're making when you chop up the code this badly. Commented Sep 19, 2019 at 2:59
  • 1
    C11 Standard - 6.4.1 Keywords, your use of struct as a variable name is not allowed. Commented Sep 19, 2019 at 3:30

3 Answers 3

1

The posted code does not compile:

  • you cannot use struct as the name of a variable. struct is a keyword.
  • Random is a global variable, not a type.

It is idiomatic and much simpler in C to return the result instead of passing its address as an argument.

Following these remarks, and adding basic checks, the code should be simplified as:

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

typedef struct Random {
    char *email;
} Random;

Random *function(void) {
    char *temp = calloc(100, sizeof(char));
    if (temp == NULL)
        return NULL;
    Random *s = calloc(5, sizeof(Random));
    if (s != NULL) {
        for (int i = 0; i < 5; i++) {
            if (scanf("%99s", temp) != 1)
                *temp = '\0';
            (s + i)->email = strdup(temp); //This works
        }
        free((s + 3)->email); //Gives segmentation fault
    }
    free(temp);
    return s;
}

int main() {
    Random *s = function();
    // ...
}

This code, semantically equivalent to your posted fragment, does not have undefined behavior where you indicate, your actual code must be doing something else.

Sign up to request clarification or add additional context in comments.

Comments

1

If I'm not mistaken, wouldn't it this?

free(*(test+index)->email);
free(text+index);

2 Comments

I get a segumentation fault when I try that. Maybe the edit might clarify things? I'm not sure.
It would be free((*(test + index))->email), I think. Or, better, free(test[index]->email); and free(text[index]);`.
0

please read/understand: precedence of C operators

Then note that the de-reference operator * has a lower precedence than the + operator,

Therefore, The posted code needs to be modified to use something like:

(*mystruct)+i = ...

etc. Otherwise, the + will be executed before the *

1 Comment

unary * evaluates before binary +, which is usually referred to as having higher precedence, not lower. *s + i is equivalent to (*s) + i, the parentheses do not change anything.

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.