-5

I have been trying to call this function, but I keep getting the error "identifier not found" (yes, I know my variable names aren't the most practical).

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

typedef struct _POOL
{
    int size;
    void* memory;

} Pool;

int main()
{
    printf("Enter the number of bytes you want to allocate\n");
    int x = getchar();
    allocatePool(x);

    return 0;
}

Pool* allocatePool(int x)
{
    Pool* p = (Pool*)malloc(x);
    return p;
}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
  • 3
    "i keep getting the error identifier not found". Please show the *exact* error. That is, which identifier? And please clarify whether what is shown is in fact your *complete* code. – kaylum Mar 20 '16 at 10:15
  • 1
    And please fix up your question title. "trying to my allocate memory function" does not make grammatical sense so it is not clear what it means. – kaylum Mar 20 '16 at 10:18
  • You need to declare allocatePool before using it. If you don't want to move up its definition you can add the line `Pool* allocatePool(int x);` before the `main()`. But there are many other errors in this program. – jdarthenay Mar 20 '16 at 10:28
  • Given an instance of `Pool`, presumably you need to allocate its `memory` member to point at something. `getchar()` reads a character. The character `'1'` does not have a numeric value of `1`. – Peter Mar 20 '16 at 10:29
  • [and do not cast the return value of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Antti Haapala -- Слава Україні Mar 20 '16 at 10:31

2 Answers2

2

Maybe what you want to do should look like this:

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

typedef struct pool
{
    int size;
    void* memory;
} pool;

pool allocatePool(int x);

int main (int argc, char *argv[])
{
    int x = 0;
    pool *p = NULL;
    printf("enter the number of bytes you want to allocate//>\n");
    if (scanf("%d", &x) == 1 && x > 0 && (p = allocatePool(x)) != NULL)
    {
        // Do something using p
        // ...
        // Treatment is done, now freeing memory
        free(p->memory);

        free(p);
        p = NULL; // Not useful right before exiting, but most often good practice.
        return 0;
    }
    else
    {
        // Show a message error or do an alternative treatment
        return 1;
    }
}

pool allocatePool(int x)
{
    pool *p = malloc(sizeof(pool));
    if (p != NULL)
    {
        p->memory = malloc(x);
        p->size = (p->memory == NULL) ? 0 : x;
    }
    return p;
}
jdarthenay
  • 3,062
  • 1
  • 15
  • 20
0

I think your program should look like this:

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

typedef struct _Pool
{
    int size;
    void* memory;
} Pool;

Pool* allocatePool(int x)
{
    static Pool p;//no pointer here you also need memory for your integer or you have to allocate memory for the integer too
    p.size=x;
    p.memory=malloc(x);//just allocate memory for your void pointer
    return &p;//return the adress of the Pool
}

int main()
{
    printf("enter the number of bytes you want to allocate//>\n");
    int x;
    scanf("%d", &x);//if you use getchar() for reading you pass the ascii value of the number not the normal number
    allocatePool(x);
    return 0;
}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
phathe
  • 56
  • 5
  • 3
    This is very wrong to return and use a pointer to a variable in the stack! You should for instance declare `static Pool p;` in the `allocatePool()` function. – jdarthenay Mar 20 '16 at 10:56
  • Ok, now it's better. Maybe you could also free memory when `allocatePool()`'s argument is non positive and p.size was positive. – jdarthenay Mar 20 '16 at 11:17