Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7896309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:51:47+00:00 2026-06-03T07:51:47+00:00

I am having problems with an array which i want to re-use in my

  • 0

I am having problems with an array which i want to re-use in my program. I need to change the size dynamically and clear it. But unfortunately the resizing does not work.

uint8_t * readBuffer; // Create array pointer

readBuffer = (uint8_t *) malloc(4); // Mem. alloc. 4bytes
memset(readBuffer, 0, sizeof(readBuffer); // Reset array
// Do stuff
free(readBuffer) // Release mem. block
....
readBuffer = (uint8_t *) malloc(1) // Mem. alloc. 1byte
memset(readBuffer, 0, sizeof(readBuffer); // Reset array
// Do stuff
free(readBuffer) // Release mem. block

At the the resizing step the length of my array is still the former (4).

Am i using free all wrong?

Further more is there much more efficient alternatives to memset for clearing?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:51:49+00:00Added an answer on June 3, 2026 at 7:51 am

    Size of a pointer (e.g. readBuffer) is always same (here 4 bytes) for any data type. You need to rather store the size given inside the malloc into a temporary and then use memset():

    uint size = 4;
    readBuffer = (uint8_t *) malloc(size); // Mem. alloc. 4bytes
    memset(readBuffer, 0, size); // Reset array
    

    Moreover argument to malloc() is in bytes, so malloc(1) means ideally 1 byte. If you are using C++ then use new:

    readBuffer = new uint8_t[N];  // 'N' is number of elements of "uint8_t"
    

    Edit:

    In C++, std::vector provides this facility with much ease:

    std::vector<uint8_n> readBuffer(N, 0);
    

    Here you are allocating ‘N’ elements for readBuffer and initializing them to 0.
    Whenever you want to add elements, you can use push_back() method. For bigger chunks you may also explore resize() or reserve() methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a C program but I keep having problems with my array of
I'm having problems trying to use an array of structs which doesn't have an
We want to use NHibernate behind our WCF service but we are having problems
im having problems on how to save the content of an array in an
Hi I am having a problems displaying my array via JSON object. I passed
I'm having problems to iterate twice on the same array: <? $indice=0 ?> <?php
I'm having problems getting values in my multidimensional arrays Array ( [0] => Array
I'm having a lot of problems initializing an array of bools. When using the
I have a number of Typed TLists which I am having problems getting to
Having few issues with my copy program which creates a copy of a file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.