1

Describe Issue: I'm able to access buffer variable inside of malloc function and can retrieve and set data with no issues

any attempt to access *(buffer+ insert some index here)->data outside of malloc function results in following error

mem* demo = malloc(2);
if(*(demo+1)->data == 0x00) {
... do some stuff here 
}

following error is produced by gcc cross compiler

kernel.c:96:21: error: invalid type argument of '->' (have 'int')
   96 |         if(*(demo+1)->data == 0x00) {

Code:

//Licensed under public domain
//also please note there is no standart library this is on a embedded system

typedef struct{
    _Bool allocated;
    unsigned char data;
} mem;
mem memory[1000];

mem* malloc(size_t size){
    mem* buffer[size];
    unsigned int successfulCounts = 0;
    unsigned int bufferCounter = 0;
    for(unsigned int i = 0; i < sizeof(memory); i++){
        //Hey that's available memory for us
        if(memory[i].allocated == 0){
            //because buffer is 16 4 items in memory (16*4)-15*4 can be found like this
            if(successfulCounts < sizeof(buffer)-sizeof(buffer-1)){
                *(buffer+successfulCounts) = &memory[i];
                successfulCounts++;
                memory[i].allocated = 1;
            }else{
                break;
            }
        }
    }
    return buffer;    
}


//... some more code that implements stuff like free() and calloc()

Odd Findings:

when mem * in function changed to unsigned char and returned *(buffer+1) i can access the data for some odd reason and i can get the exact same data i have pushed nothing is corrupted as i expect for some odd reason

2
  • Please fix a minimal reproducible example Commented Jul 15, 2022 at 22:21
  • @klutt example has been fixed now gcc in.c -ffreestanding -nostdlib -o test.elf needs a int main() though Commented Jul 15, 2022 at 22:27

2 Answers 2

0

This if statement

if(*(demo+1)->data == 0x00) {

is equivalent to

if( *( ( demo + 1 )->data ) == 0x00) {

but data is not a pointer. It has the type unsigned char

typedef struct{
    _Bool allocated;
    unsigned char data;
} mem;

It seems you mean

if( (demo+1)->data == 0x00) {

Pay attention to that the function in any case is invalid

mem* malloc(size_t size){
    mem* buffer[size];
    //...
    return buffer;    
}

For starters the return type of the function is mem * while the type of the returned expression is mem **. And moreover the function returns a pointer to a local object (array). So the returned pointer will not be invalid because the array will not be alive after exiting the function.

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

5 Comments

still the same issue kernel.c:96:20: error: invalid type argument of '->' (have 'int') 96 | if((demo+1)->data == 0x00) {
@lplplplp The expression demo + 1 does not have the type int. it is a pointer provided that it is defined like mem* demo = malloc(2);
No it is not -- -> is higher precedence than *, so it is equivalent to if (*((demo+1)->data) == 0x00)
From the error message provided, demo has type int (as does demo + 1), so the demo in the statement with the error is not the demo in the earlier declaration. That's why an MRE is so important.
@ChrisDodd You are right. I was not attentive. But now I can not remove the answer because it is marked as the best.:)
0

Your problem is you are de-referencing the pointer before using the arrow operator (->).

The arrow operator(->) is used when you want to access a member of a struct using a pointer.

struct new_t
{
    char *str;
    int num;
};

struct new_t p1 = {"Dog", 5};
struct *ptr = &p;

char *s = ptr->str; //is valid

char *s1 = ptr.str; // is not valid

char *s2 = *(ptr)->str; // is not valid

Whereas the dot(.) operator is used to access a member of a struct

int n = p1.num; //is valid

n = p1->num; //is not valid

To solved your problem used this

if(*(demo+1).data == 0x00) {
... do some stuff here 
}

or

if((demo+1)->data == 0x00) {
... do some stuff here 
}

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.