Some good questions
(一)
#include <stdio.h>
#include <stdlib.h>
void getmemory(char *p)
{
p=(char *) malloc(100);
strcpy(p,"hello world");
}
int main( )
{
char *str=NULL;
getmemory(str);
printf("%s/n",str);
free(str);
return 0;
}
程序崩溃,getmemory中的malloc 不能返回动态内存, free()对str操作很危险。
上面代码传str的值进去带不出来,如果对指针进行赋值一定要用更高一级的指针,否则就要有返回值malloc()分配了新的内存给p,但是原来函数中的p是复制了str的值进行操作,函数执行完之后p就找不到了,str并没有得到p的值,同时把p丢了,也没有办法回收分配给p的内存。
#include <stdio.h>
#include <string.h>
#include <stdlib.h> void getmemory(char **p) //双重指针
{
*p=(char *) malloc(100);
strcpy(*p,"hello world");
}
int main( )
{
char *str=NULL;
getmemory(&str); //传地址
printf("%s\n",str);
free(str);
return 0;
} 或者:
#include <stdio.h>
#include <string.h>
#include <stdlib.h> char* getmemory(char *p) //返回char*
{
p=(char *) malloc(100);
strcpy(p,"hello world");
return p; //返回值
}
int main( )
{
char *str=NULL;
str=getmemory(str);
printf("%s\n",str);
free(str);
return 0;
} (二)
char szstr[10];
strcpy(szstr,"0123456789");
产生什么结果?为什么?
答案:长度不一样,会造成非法的OS
源码实现:
int strlen(const char* str)
{
int i=0;
while(*str++ !=’’) i++;
return i;
}
char* strcpy(char * dst, const char * src)
{
char * cp = dst;
while( *cp++ = *src++ )
;
return( dst );
}
或者
char *strcpy(char *dest, const char *src)
{
unsigned i;
for (i=0; src[i] != '\0'; ++i)
dest[i] = src[i];
dest[i] = '\0';
return dest;
}
(三)
struct name1{ //字节对齐问题
char str; //sizeof(char)为1
short x; //sizeof(short)为2
int num; //sizeof(int)为4
}; //以4字节对齐,前面2个在第一个4字节里面,所以就是4+4了
struct name2{
char str;
int num;
short x;
}; //同样以4字节对齐,这里的char和short分开了,4+4+4了
为什么sizeof(name1)=8;sizeof(name2)=12
问题补充:
类似的
struct s1{
int i:8; //因为int占了4字节,
int i:4; //和上面的一起在了4个字节
int a:3; //4字节
double b; //sizeof(double)为8,以8字节对齐,前面的2个合一起为8,所以8+8
};//16
struct s2{
int i:8;
int i:4; //2个合一起4字节。
double b; //以8字节对齐
int a:3;
};//24 //8+8+8
(*) int i:8 ;和int i;有区别,int i:8这个是意思是占32位(4字节)中的8位,int i这个要占满4个字节。
Some good questions的更多相关文章
- WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】
http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...
- [译]Node.js Interview Questions and Answers (2017 Edition)
原文 Node.js Interview Questions for 2017 什么是error-first callback? 如何避免无止境的callback? 什么是Promises? 用什么工 ...
- [面试] Design Questions
Uber总是考一些系统设计的题目,而且重复率很高,汇总了一下地里的所有design的题目,希望可以跟小伙伴们讨论下. Uber Design Questions 1. 让design uber ...
- Front End Developer Questions 前端开发人员问题(二)CSS 后续
问题来源:http://markyun.github.io/2015/Front-end-Developer-Questions/ 31.视差滚动效果,如何给每页做不同的动画?(回到顶部,向下滑动要再 ...
- WCF学习系列三--【WCF Interview Questions – Part 3 翻译系列】
http://www.topwcftutorials.net/2012/10/wcf-faqs-part3.html WCF Interview Questions – Part 3 This WCF ...
- WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】
WCF Interview Questions – Part 4 This WCF service tutorial is part-4 in series of WCF Interview Qu ...
- [转]Design Pattern Interview Questions - Part 4
Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...
- [转]Design Pattern Interview Questions - Part 2
Interpeter , Iterator , Mediator , Memento and Observer design patterns. (I) what is Interpreter pat ...
- [转]Design Pattern Interview Questions - Part 3
State, Stratergy, Visitor Adapter and fly weight design pattern from interview perspective. (I) Can ...
- [转]Design Pattern Interview Questions - Part 1
Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...
随机推荐
- static 和 extern
外部函数:定义的函数能被本文件和其他文件访问,默认所有的情况都是外部函数,不允许有同名的外部函数 >>extern定义和声明一个外部函数(可以省略) 内部函数:定义的函数只能被本文件访问, ...
- C#中in,out,ref,params的作用和区别
ref和out的区别在C# 中,既可以通过值也可以通过引用传递参数.通过引用传递参数允许函数成员更改参数的值,并保持该更改.若要通过引用传递参数, 可使用ref或out关键字.ref和out这两个关键 ...
- CentOS 7 上搭建LNMP环境
(转自美团云知识库Chris) 简介 LNMP是Linux.Nginx.MySQL(MariaDB)和PHP的缩写,这个组合是最常见的WEB服务器的运行环境之一.本文将带领大家在CentOS 7操作系 ...
- Hibernate摘记
原理: 1.通过Configuration().configure();读取并解析hibernate.cfg.xml配置文件2.由hibernate.cfg.xml中的<mapping reso ...
- [WPF]解决ListView在没有Items时,水平滚动条不出现的问题
转载地址:http://www.cnblogs.com/nankezhishi/archive/2010/03/19/FixListViewNotScrollHeaderBug.html 在上一篇Bl ...
- C语言的本质(5)——类型转换的本质与处理
数据类型转换的方式 C 语言中的数据类型转换可分为隐式转换和显式转换两种. 隐式转换 隐式转换也可称作为自动转换,它经常以以下几种形式发生: 1.在计算一个同时出现多种数据类型的表达式时,将所有数据类 ...
- RHEL Channel Bonding
1. 添加 kernel 模块 RHEL5上编辑 /etc/modprobe.conf 加入 alias bond0 bonding options bond0 miimon=100 mode=1 ...
- poj3100---求根问题
题意:a的n方=b,a这个整数与b开n方的值相近,分别向上取整和向下取整,同时n方,b一定介于这两个整数之间,然后比较这两个数与b的距离,取最近的 收获:c++的cei和floor函数在c中的向上取整 ...
- poj1004
Financial Management Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 125635 Accepted: ...
- Day1_算法分析方法
课堂笔记: 程序执行效率影响因子: 输入:评估时使用最坏情况输入 输入大小:考虑n渐进∞ 机器执行效率: 忽略 两种排序算法比较 插入排序n2 归并排序nlgn 递归 技能1:评估多项式的时间级数θ ...