0

I'd like to use a few for loops in C and use common index for these. I want to use pointer, because I will be able to free it later.
I thought about something like:

uint8_t *p = malloc(sizeof(uint8_t));

for (*p = 0; *p < 255; *p++)
{
  // Instructions...
}

// A few loops...

free(p);

But this code doesn't work as I want. How can I fix it?

PS I'm a beginner in C's pointers.

7
  • In the code as shown, you're not accomplishing anything you couldn't have done by declaring an ordinary variable uint8_t i;. There are uses of dynamic memory allocation, but this isn't it! Commented Dec 1, 2019 at 13:15
  • Also, using an 8-bit type as an index variable is usually a bad idea, and is a rather frequent source of subtle bugs. Is there a reason you're trying to use uint8_t here, instead of plain int? Commented Dec 1, 2019 at 13:17
  • Just change *p++ to (*p)++, it should work fine. Commented Dec 1, 2019 at 13:19
  • I used uint8_t because I wanted to save memory. I will check what will happen if I use int, wait a second Commented Dec 1, 2019 at 13:19
  • 1
    In general, for individual variables like that, using a type smaller than int uses more memory, runs more slowly, introduces bugs, and wastes your time. It's a loss in almost every way. My advice to you is: just use int! Commented Dec 1, 2019 at 13:49

3 Answers 3

2

By this line:

uint8_t *p = malloc(sizeof(uint8_t));

You're allocating memory that is suitable to hold ONE uint8_t. You shouldn't touch any other addresses after p because you didn't allocate them. You may need to do the following:

uint8_t *p = malloc(255 * sizeof(uint8_t));

The previous line allocates memory that can hold 255 uint8_t instead of just one. That way, you can access addresses from p to p + 254.

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

2 Comments

It works, but is too hard to understand for me. I will go back to normal index variable. Thanks!
malloc simply takes the number of bytes you need from the RAM. You code says to the operating system: "Hey Windows (or linux or mac), give me a memory that can hold uint8_t value". But you actually want 255 values. that's why I multiplied the sizeof(uint8_t) by 255. This way, I'm saying: "Hey Windows, give me memory that can hold 255 uint8_t values.".
0

OK, I solved the problem. My code:

uint8_t *p = malloc(sizeof(uint8_t));

for (*p = 0; *p < 255; (*p)++)
{
  // Instructions...
}

// A few loops...

free(p);

Thank you all for your answers!

2 Comments

I'm glad to know it works, but it would be easier to read, easier to write, faster, use less code, and use less data if you were to simply write uint8_t i; and use i everywhere instead of *p. You are also skating on thin ice here: it is very easy to write a loop that runs forever, when you try to use an 8-bit variable to count to 256. (You're saved by the fact that you're only counting to 255.)
I said it would "use less code and use less data if you were to simply write uint8_t i;", but you would likely save even more if you changed it to plain int i;.
-1

Syntax of malloc is :

int * p = (*int )malloc(sizeof(int);

You have to first convert the data type into pointer

1 Comment

No, casts on malloc's return value are not required, and are disrecommended by most C experts.

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.