ReadCode-001: memcpy memset bzero

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student{
	int age;
	char name[15];
};

typedef struct student student_t;

int main(void) {
	student_t stu1, *stu1_ptr;
	stu1.age = 12;
	strcpy(stu1.name, "amao");
	stu1_ptr = &stu1;

	student_t *stu2_ptr = (student_t *) malloc(sizeof(student_t));
	memset(stu2_ptr, 0, sizeof(student_t));
	printf("%d %s\n", stu1_ptr->age, stu1_ptr->name);
	printf("%d %s\n", stu2_ptr->age, stu2_ptr->name);

	memcpy(stu2_ptr, stu1_ptr, sizeof(student_t));
	printf("%d %s\n", stu2_ptr->age, stu2_ptr->name);
	return EXIT_SUCCESS;
}


/* static variables */
#ifdef WITH_PTHREADS
  static pthread_mutex_t  log_lock = PTHREAD_MUTEX_INITIALIZER;
#else
  static int              log_lock;
#endif /* WITH_PTHREADS */
//-----------------------------------
#  define slurm_mutex_lock(mutex)
#  define slurm_mutex_unlock(mutex)
//-----------------------------------
/* return the FILE * of the current logfile (stderr if logging to stderr)
 */
FILE *log_fp(void)
{
	FILE *fp;
	slurm_mutex_lock(&log_lock);
	if (log->logfp)
		fp = log->logfp;
	else
		fp = stderr;
	slurm_mutex_unlock(&log_lock);
	return fp;
}
//-----------------------------------
/*
 * "Safe" version of malloc().
 *   size (IN)	number of bytes to malloc
 *   RETURN	pointer to allocate heap space
 */
void *slurm_xmalloc(size_t size, const char *file, int line, const char *func)
{
	void *new;
	int *p;


	xmalloc_assert(size >= 0 && size <= INT_MAX);
	MALLOC_LOCK();
	p = (int *)malloc(size + 2*sizeof(int));
	MALLOC_UNLOCK();
	if (!p) {
		/* don't call log functions here, we're probably OOM
		 */
		fprintf(log_fp(), "%s:%d: %s: xmalloc(%d) failed\n",
				file, line, func, (int)size);
		exit(1);
	}
	p[0] = XMALLOC_MAGIC;	/* add "secret" magic cookie */
	p[1] = (int)size;	/* store size in buffer */

	new = &p[2];
	memset(new, 0, size);
	return new;
}
//-----------------------------------
#define xmalloc(__sz) \
	slurm_xmalloc (__sz, __FILE__, __LINE__, __CURRENT_FUNC__)
//-----------------------------------
extern int jobacct_common_init_struct(struct jobacctinfo *jobacct,
				      jobacct_id_t *jobacct_id)
{
	if (!jobacct_id) {
		jobacct_id_t temp_id;
		temp_id.taskid = (uint16_t)NO_VAL;
		temp_id.nodeid = (uint32_t)NO_VAL;
		jobacct_id = &temp_id;
	}
	memset(jobacct, 0, sizeof(struct jobacctinfo));
	jobacct->sys_cpu_sec = 0;
	jobacct->sys_cpu_usec = 0;
	jobacct->user_cpu_sec = 0;
	jobacct->user_cpu_usec = 0;

	jobacct->max_vsize = 0;
	memcpy(&jobacct->max_vsize_id, jobacct_id, sizeof(jobacct_id_t));
	jobacct->tot_vsize = 0;
	jobacct->max_rss = 0;
	memcpy(&jobacct->max_rss_id, jobacct_id, sizeof(jobacct_id_t));
	jobacct->tot_rss = 0;
	jobacct->max_pages = 0;
	memcpy(&jobacct->max_pages_id, jobacct_id, sizeof(jobacct_id_t));
	jobacct->tot_pages = 0;
	jobacct->min_cpu = (uint32_t)NO_VAL;
	memcpy(&jobacct->min_cpu_id, jobacct_id, sizeof(jobacct_id_t));
	jobacct->tot_cpu = 0;

	return SLURM_SUCCESS;
}

extern struct jobacctinfo *jobacct_common_alloc_jobacct(
	jobacct_id_t *jobacct_id)
{
	struct jobacctinfo *jobacct = xmalloc(sizeof(struct jobacctinfo));
	jobacct_common_init_struct(jobacct, jobacct_id);
	return jobacct;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值