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 ...
随机推荐
- SQL Server AlwaysOn 故障转移
目的: a) AlwaysOn 可用性组功能是一个提供替代数据库镜像的企业级方案的高可用性和灾难恢复解决方案. b) 当数据库服务器SQL1出现故障宕机时,可以通过AlwaysOn可用性组,自动故障转 ...
- js学习日记 (1)createDocumentFragment() ES6 => 箭头
只能说是会用和记载,深入理解还需时间. 有关性能优化: 使用createdocumentfragment()方法可以创建某个具有节点该有的所有属性的节点. 使用情况: 提取文档中的某个小部分,修改文 ...
- a 标签的四个伪类
link 有链接属性时visited 链接地址已被访问过active 被用户激活(在鼠标点击与释放之间发生的事件)hover 其鼠标悬停 <!DOCTYPE ...
- 关于const限定符
1. 指向非常量数据的非常量指针 具有最高的数据访问极限 .不包含const关键字. 2. 指向常量数据的非常量指针 指针可以被改写,使其指向相应类型的任何数据项, 但是它所指向的数据项的值是不能被 ...
- jQuery validate (转载)
转自:http://blog.sina.com.cn/s/blog_608475eb0100h3h1.html jQuery校验 官网地址:http://bassistance.de/jquery-p ...
- 解决svn: Cannot negotiate authentication mechanism错误问题
解决svn: Cannot negotiate authentication mechanism错误问题 作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/ ...
- docker 运行挂载磁盘
docker:/data# mkdir /awp docker:/data# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAM ...
- C# 图片压缩 开源库
http://www.rasteredge.com/how-to/csharp-imaging/image-compressing/ http://www.rasteredge.com/dotnet- ...
- Why stackedit
马克飞象 这个小工具很不错.初初看上了他能够很好写Latex,然后能同步到Evernode.但有个问题,在ipad上面用evernote的App时,latex不能显示.可能是权限的问题. Stacke ...
- 自定义和扩展 SharePoint 2010 Server 功能区
了解构成 SharePoint 2010 服务器功能区的组件以及如何通过演练两个功能区自定义项方案来自定义功能区. 适用范围: Microsoft SharePoint Foundation 2010 ...