Using memset

Thread Starter

ElectricSpidey

Joined Dec 2, 2017
2,786
First time using an array and memset, the array is pretty simple but I’m not confident using memset, so I’m cheating and using some code off of the web.

The array:

int F[26] = {0}; //26 element array initialized to all zeros

Then at some point I need to reset the array back to all zeros, so I’m using this:

memset(F, 0, sizeof(F)); //The code in question

So two questions:

Is the memset code correct?
Do I need to “include” any files for memset to work?

The IDE isn’t complaining about any of the code, but I can’t compile and test for a few weeks and I need to get a jump on the coding.

I know I could use a loop to do this or simply change the elements to zero one at a time but I want to try this first.

EDIT: Oh yea...it's C++
 
Last edited:

WBahn

Joined Mar 31, 2012
30,077
You need to get in the habit of using references to answer these kinds of questions because they will come up often and you will need to have an answer in a minute or two (preferably a lot less) and not have to wait for responses from a forum. Usually, just Googling something like "C memset" is more than sufficient.

So go do that and see if you can figure out what is wrong with your use of memset() and also what library you need to include. The latter can be a bit harder to find as many sites don't emphasize that information -- so just keep looking until you find one that does. It won't take long before you will have a few favorite sites that you will pick out from the Google results first and almost always be able to answer your own question in well under a minute.
 

WBahn

Joined Mar 31, 2012
30,077
Nevermind...

It's not like I haven't spent a very long time just getting this far.
And we are trying to help you learn a skill that will help you to not have to spend similar very long times in the future. If all you are interested in is someone to answer your questions without so that you don't have to learn how to answer them yourself, fine. Here you go:

So two questions:

Is the memset code correct?
Do I need to “include” any files for memset to work?
No, the memset code is not correct.

Yes, you need to include a file for memset to work properly.
 

Thread Starter

ElectricSpidey

Joined Dec 2, 2017
2,786
So I was able to set up a test before getting to the actual project I will use this function for.

So...the function will set a type "int" array back to all zeros.

No included library needed.
 

WBahn

Joined Mar 31, 2012
30,077
So I was able to set up a test before getting to the actual project I will use this function for.

So...the function will set a type "int" array back to all zeros.

No included library needed.
Not with the code you provided earlier; that will only set the first element of the array to zero.

Your compiler might be able to get by without including the correct include files (some compilers include a core set of header files by default), but that is asking for trouble.

Why not just do it right and include the proper header file?
 

Sleve Hope

Joined Apr 21, 2020
11
Memset is pretty simple.
Here is the code of how to use memset:

#include <cstring>
#include <iostream>

using namespace std;

int main()
{
char str[] = "ElectricSpidey";
memset(str, 't', sizeof(str));
cout << str;
return 0;
}

How ever this is an array of character but logic is same.
Also use "cstring" library.
 

WBahn

Joined Mar 31, 2012
30,077
So first you lecture me, and now you are calling me a liar.
I was not calling you a liar and you might consider taking that humongous chip off your shoulder.

But I do need to correct what I was saying and you are correct in one part (but I wonder if you understand why you are correct).

using memset(F, 0, sizeof(F)) will work in this instance since F is a static array. I'm used to working with dynamic arrays and so I tend to forget that the sizeof() macro behaves differently with static arrays.

Is your compiler giving you any warnings when you don't include any header files? If it isn't, then you should look into getting a different compiler. If it is and you are ignoring them, then you are setting yourself up for disaster at some point.

But trying to convince you of that would be lecturing you, and we've already established that you don't want to actually learn anything that might help you beyond the immediate code snippet you are focused on, so we can wait till you come back wondering why your program keeps doing things you can't understand because you didn't feel there was any value in doing things correctly.
 

Thread Starter

ElectricSpidey

Joined Dec 2, 2017
2,786
My compiler always warns when an .h file is missing, usually with the "explicit declaration" warning, but I have seen others as well.

As far as learning the string stuff, first of all I haven't gotten that far in my studies, second all I wanted was to find out if this particular function would do what I wanted, without skipping forward in my studies, as to make one project work.

When I get to strings, then I will study strings but I may not get to those for months from now, and I'm sure there are things I will need to know before getting to strings, so I can understand the instruction at that time.

You made the assumption that asking a simple yes or no question, was a sign that I was unwilling to do any learning.
 

BobaMosfet

Joined Jul 1, 2009
2,113
First time using an array and memset, the array is pretty simple but I’m not confident using memset, so I’m cheating and using some code off of the web.

The array:

int F[26] = {0}; //26 element array initialized to all zeros

Then at some point I need to reset the array back to all zeros, so I’m using this:

memset(F, 0, sizeof(F)); //The code in question

So two questions:

Is the memset code correct?
Do I need to “include” any files for memset to work?

The IDE isn’t complaining about any of the code, but I can’t compile and test for a few weeks and I need to get a jump on the coding.

I know I could use a loop to do this or simply change the elements to zero one at a time but I want to try this first.

EDIT: Oh yea...it's C++
Title: Standard C [Quick Ref]
Author(s): P.J.Plauger, Jim Brodie
ISBN: 1-55615-158-6
 

WBahn

Joined Mar 31, 2012
30,077
You made the assumption that asking a simple yes or no question, was a sign that I was unwilling to do any learning.
Not at all. It was when you got all bent out of shape when people tried to help you learn something that I concluded that you didn't want to learn anything. That left only the possibility that you only wanted exact answers to the exact questions you asked. But you got bent out of shape at that, too. Thus the only available conclusion: Some people just can't be pleased and are going to gripe no matter what.
 
Top