Mihai Adrian
Mihai Adrian

Reputation: 3

dynamic allocation in code blocks

I am trying to make a linked list in C. I've got the program up and running in Turbo C++ which we use at school. When I try putting my code in Code Blocks I cannot get it running. I stripped a big part of the code so you can see the part that stops working when I debug. When it compiles it says nothing about an error but simply stops working. I am thinking it might me because of how I dynamically allocate memory.

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

struct data
{
    int a;
    int b;
};

struct node
{
    struct data info;
    struct node *urm;
};

struct lista
{
    int lungime;
    struct node *curent, *prim, *ultim;
};

struct lista *listax;

int creare(struct lista *LP)
{
    LP->prim = (struct node*)malloc(sizeof(struct node));
    LP->ultim = (struct node*)malloc(sizeof(struct node));
    LP->prim->urm = LP->ultim;
    LP->ultim->urm = NULL;
    LP->curent=LP->prim;
    LP->lungime = 0;
    return 1;
}

int main()
{
    creare(listax);
    return 0;
}

I have to use this type of declaration because this how our teacher wants us to present the list (with a start and end node). Any help is appreciated.

*edit:
prim is first
ultim is last
lungime is length
urm is next

Upvotes: 0

Views: 2319

Answers (1)

Toby
Toby

Reputation: 10144

Your memory allocation is indeed incorrect. Before attempting to assign to each of the members of LP you must assign space for the struct pointed to by LP itself, for example:

LP = malloc(sizeof(*LP));

Additional advice

  1. When you allocate space you should normally check the result of the allocation function (malloc()/calloc()/realloc()) as the function may return null if it was not able to assign the memory requested. You can do this with a simple if statement after the malloc(), such as: if (!LP) return -1;
  2. Do not cast the return value of malloc() .
  3. It's usually better to use sizeof on a variable name rather than a type, eg LP->prim = malloc(sizeof(LP->prim));
  4. Anytime you allocate memory you should remember to free the allocated memory when finished with it! Use free() to do this. If you do not free memory a program leaks memory, that is it keeps allocating more and more each time and eventually all of the memory on your computer is allocated, even though your program only actually needs to use a little bit of what it has allocated. See this and this.
  5. As pointed out by @StoryTeller, use white space to make your code readable - having it all bunched up together in some effort to make it appear shorter, or something, does not make it look professional, it just looks unreadable (and thus hard for other people to help you with it).

    Using white space to divide words or group information may not be the norm in some writing systems (some middle eastern languages place less or no importance on it for example), but a lot of code is written in American English, which does. As such, writing professional code that co-workers, teachers or contributors can easily read will involve using white space in this manner and will be something you need to learn, just the same learning a language's syntax.

    This question may be a good point to start with code style, but you can also check out some of the named C style guides, such as GNU, Linux Kernel, BSD, etc

Upvotes: 1

Related Questions