1

I'm learning about structs and memory allocation.

One of the things i found out by studying was that there is not a way to see if a block of memory was allocated correctly.

I think it's working properly, but i was getting ready to read about linked lists and got all confused again.

I ask this, because on linked lists, i have seen:

typedef LIST* Lint;

int main(){
    Lint *lint;
}

and by defining the type of class like this on my code:

typedef CLASS* t;

int main(){
    t class; // WITHOUT THE * BEFORE class
}

it worked! I thought it would be ok, to give the definition like this. To me makes sense, the class is now a pointer to a type (type ie. CLASS);

So, am i on the right track or this working was pure luck?

The code structure sucks, but i'm still learning :)

typedef struct student {
    char    name[50];
    int     grade;
} TURMA;

typedef CLASS* t;

void showS(struct student i){
    printf("Name of Student: %s \n Grade of Student: %d\n", i.name, i.grade);
}

void insert(struct student *i,int k){
    char n[100]; int q=0;
    printf("Define name of student %d\n", k+1); scanf("%s", n);
    printf("Define the grade of %s\n", n); scanf("%d", &q);
    strcpy(i->name,n);
    i->grade = q;
}


int main()
{
    t class;
    int total,plus,i;
    printf("Define number of total students: \n"); scanf("%d", &total);
    class = (struct student*) malloc(total*(sizeof(CLASS)));

    for(i=0; i<total; i++){
        insert(&class[i], i);
    }
    printf("\n\nThis is the complete class: \n");
    for(i=0; i<total; i++){
        showS(class[i]);
    }

    printf("(Total size after malloc: %lu) Add more students (Int)\n",sizeof(*turma)); scanf("%d", &plus);
    class = realloc(class, plus*sizeof(CLASS));
    free(class);

    printf("(Total size after malloc: %lu)", sizeof(*class));
    return 0;
}
6
  • if malloc fails, it returns a null pointer Commented May 9, 2015 at 0:23
  • Don't cast malloc: class = malloc(total*(sizeof(CLASS))); is all that is needed. (presuming CLASS is correct.). Also parenthesis are only needed when using sizeof with a data type (e.g. sizeof (int)), not when taking the size of a variable. sizeof(*class) can be simply sizeof *class Commented May 9, 2015 at 0:36
  • Why do they use Lint *lint instead of Lint lint ? Notice my t class, should it be t *class? This last option would make the pointer class a pointer to a struct student right? Commented May 9, 2015 at 0:39
  • No because typedef CLASS* t makes t an alias for class* (i.e. a pointer to class) Commented May 9, 2015 at 0:51
  • @skills Well, you have issues, but the typedef Lint allows creating a pointer of type Lint to point to the next node in the linked list. (e.g. Lint *lint;). However, you will never get to that point unless you define what CLASS is. You are trying to create a typedef for t from an uknown type. We need more of your code. See: How to create a Minimal, Complete, and Verifiable example." Commented May 9, 2015 at 0:53

1 Answer 1

2

It is really hard to tell exactly what you are trying to accomplish. I looks like you are trying to create a singly-linked-list out of struct student with a typedef of struct student to TURMA. That's about where the useful information ends. Attempting to declare typedef CLASS* t; makes no sense. The compiler doesn't know what CLASS is. Guessing what you were attempting, the following is what flows from your code.

note: I added one function flush_stdin to empty stdin so you wouldn't have pesky newline characters left in the buffer. I also changed the scanf format string to prevent leaving newlines following your reading of character strings. Take a look below, and I'll look at your latest additions.

Also note in your code, you cannot free (class); then expect to take sizeof(*class)) -- that is undefined behavior.

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

#define MAXN 100

typedef struct student {
    char    name[50];
    int     grade;
} TURMA;

typedef TURMA* t;

void flush_stdin () 
{
    int c = 0;
    while ((c = getchar()) != '\n' && c != EOF);
}

void showS (TURMA i)
{
    printf("Name of Student: %s \n Grade of Student: %d\n", i.name, i.grade);
}

void insert (t i, int k)
{
    char n[MAXN] = {0}; int q=0;

    printf("Define name of student %d\n", k+1); scanf ("%[^\n]%*c", n);
    printf("Define the grade of %s\n", n); scanf("%d", &q); flush_stdin();

    strcpy(i->name,n);
    i->grade = q;
}


int main()
{
    t class;
    int total,plus,i;

    printf("Define number of total students: \n"); scanf("%d", &total); flush_stdin();
    class = malloc (total * sizeof *class);
    if (!class) {
        fprintf (stderr, "error: virtual memory exhausted.\n");
        exit (EXIT_FAILURE);
    }

    for (i=0; i<total; i++){
        insert (&class[i], i);
    }

    printf("\n\nThis is the complete class: \n");
    for (i=0; i<total; i++){
        showS (class[i]);
    }

    printf("(Total size after malloc: %lu) Add more students (Int)\n", sizeof *class * total); 
    scanf("%d", &plus); flush_stdin();

    TURMA *turma = realloc (class, plus * sizeof *turma);
    if (!turma) {
        fprintf (stderr, "error: virtual memory exhausted.\n");
        exit (EXIT_FAILURE);
    }

    free (class);

    printf("(Total size after realloc: %lu)\n", sizeof *turma * (total + plus));

    free (turma);

    return 0;
}

Output

$ ./bin/llclass
Define number of total students:
2
Define name of student 1
John James
Define the grade of John James
8
Define name of student 2
Jill James
Define the grade of Jill James
9


This is the complete class:
Name of Student: John James
 Grade of Student: 8
Name of Student: Jill James
 Grade of Student: 9
(Total size after malloc: 112) Add more students (Int)
2
(Total size after realloc: 224)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your insights David, the code was already working, the reason on why it didn't compile is because i write my code in portuguese, and i've tried to translate all the terms to english before publishing here. But it was nice to see the changes you've made, makes much more sense now. Thank you very much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.