The Memset function that has been misunderstood

Source: Internet
Author: User

1. Consequences of "Take it for granted"

Today made a very low-level error in initializing an integer array with memset all element values are set to 1. But the result is very big, very unexpected! Next, I did the code test.

#include <iostream>using namespacestd;intarray1[10001];intarray2[Ten];intarray3[Ten];intMain () {memset (array1,-1,Ten); memset (Array2,0,Ten); memset (Array3,1,Ten);  for(inti =0; I <Ten; i++) cout<< Array1[i] <<' '; cout<<Endl;  for(inti =0; I <Ten; i++) cout<< Array2[i] <<' '; cout<<Endl;  for(inti =0; I <Ten; i++) cout<< Array3[i] <<' '; cout<<Endl; return 0;}

The output results are as follows:

What's the situation?

2, know it, but do not know the reason why

With this question to check the relevant information, only to understand that they really made a very low error Ah! Just know that the Memset function can initialize the data, but I don't know how it works. This is the study of the taboo Ah!

The explanations and applications of memset are given below.

Function prototype: extern void* memset (void *buffer,int c,int count), buffer is a pointer or array name, C is the value assigned to buffer, count is the length of buffer.

The function is used in the socket to empty the array. For example, the prototype is memset (buffer, 0, sizeof), and the other memset is used to set all of the memory space to a character, which is generally used to initialize the defined string to ' ' or '/0 ';

For example: Char A[100];memset (A, '/0 ', sizeof (a));

Memset can easily empty a variable or array of a struct type.

struct Student
{
Char csname[16];
int iSeq;
int iType;
};

For variable Student Stu, the method of emptying Stu in general:

Stu.csname[0]= '/0 '; stu.iseq=0; stu.itype=0;

It is very convenient to use memset:
memset (&sttest,0,sizeof (struct sample_struct));

If it is an array: struct sample_struct test[10];

memset (test,0,sizeof (struct sample_struct) *10);

Expand and introduce strcpy and memcpy.

strcpy

Prototype: extern char *strcpy (char *dest,char *src);

Usage: #i nclude

function: Copy the null-terminated string from SRC to the array referred to by Dest.

Description: The memory areas referred to by SRC and dest cannot overlap and dest must have sufficient space to accommodate the SRC string.

Returns a pointer to the dest.

memcpy

Prototype: extern void *memcpy (void *dest, void *src, unsigned int count);

Usage: #i nclude

Function: the memory area referred to by SRC is copied from count bytes to the memory area of Dest.

Description: The memory area referred to by SRC and dest cannot overlap, and the function returns a pointer to Dest.

The Memset function that has been misunderstood

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.