Skip to content
Life is short Play more
  • Computer
    • Cloud
    • DataBase
      • Mysql
      • Oracle
    • Operation system
      • Android
      • Linux
      • Windows
    • Programming
      • C , C++
      • Java
      • Javascript
      • Linux shell
      • Php
      • python
      • Web Programing
    • Security
    • Virtulization
    • Web hosting
      • Ad
  • Life Testing
  • My work
    • wrapEasyDevice
  • Network
    • HTTP
  • Note
  • Other
    • Embedded
    • Hardware
  • Share
    • Articles
    • Music
    • Tools
    • Video
  • Software Testing
    • Automation Testing
    • Cloud Testing
    • Functional Testing
    • Performance Testing
Life is short Play more
Life is short play more

cs50 library for c

Posted On 2012年2月23日

cs50的c语言库, 主要为6个函数, 并定义string 数据类型. 这6个函数是基本输入函数,可以方便根据用户的输入获取到不同类型的数据。
1. char GetChar(void);
2. double GetDouble(void);
3. float GetFloat(void);
4. int GetInt(void);
5. long long GetLongLong(void);
6. string GetString(void);
获取cs50库访问: https://manual.cs50.net/CS50_Library
其他1-5 函数都依赖GetString函数。 GetString函数中用到了一个reallocate函数,用户重新分配内存。

关于reallocate函数, 主要是, 重新调整动态内存大小, 如果调大,则根据实际情况可能分配一块新的内存空间(也可能部分新的内存空间,这时仍返回旧指针,所以我们要对返回的指针进行判断),拷贝旧数据到新内存空间,释放旧内存空间,并返回新内存空的起始指针。 如果调小,将会意味着减少的那部分空间数据。
下面文章做了更清的阐述。
Sometimes you’re not sure at first how much memory you’ll need. For example, if you need to store a series of items you read from the user, and if the only way to know how many there are is to read them until the user types some “end” signal, you’ll have no way of knowing, as you begin reading and storing the first few, how many you’ll have seen by the time you do see that “end” marker. You might want to allocate room for, say, 100 items, and if the user enters a 101st item before entering the “end” marker, you might wish for a way to say “uh, malloc, remember those 100 items I asked for? Could I change my mind and have 200 instead?”

In fact, you can do exactly this, with the realloc function. You hand realloc an old pointer (such as you received from an initial call to malloc) and a new size, and realloc does what it can to give you a chunk of memory big enough to hold the new size. For example, if we wanted the ip variable from an earlier example to point at 200 ints instead of 100, we could try calling

ip = realloc(ip, 200 * sizeof(int));
Since you always want each block of dynamically-allocated memory to be contiguous (so that you can treat it as if it were an array), you and realloc have to worry about the case where realloc can’t make the old block of memory bigger “in place,” but rather has to relocate it elsewhere in order to find enough contiguous space for the new requested size. realloc does this by returning a new pointer. If realloc was able to make the old block of memory bigger, it returns the same pointer. If realloc has to go elsewhere to get enough contiguous memory, it returns a pointer to the new memory, after copying your old data there. (In this case, after it makes the copy, it frees the old block.) Finally, if realloc can’t find enough memory to satisfy the new request at all, it returns a null pointer. Therefore, you usually don’t want to overwrite your old pointer with realloc’s return value until you’ve tested it to make sure it’s not a null pointer. You might use code like this:
int *newp;
newp = realloc(ip, 200 * sizeof(int));
if(newp != NULL)
ip = newp;
else {
printf(“out of memory\n”);
/* exit or return */
/* but ip still points at 100 ints */
}
If realloc returns something other than a null pointer, it succeeded, and we set ip to what it returned. (We’ve either set ip to what it used to be or to a new pointer, but in either case, it points to where our data is now.) If realloc returns a null pointer, however, we hang on to our old pointer in ip which still points at our original 100 values.
Putting this all together, here is a piece of code which reads lines of text from the user, treats each line as an integer by calling atoi, and stores each integer in a dynamically-allocated “array”:

#define MAXLINE 100

char line[MAXLINE];
int *ip;
int nalloc, nitems;

nalloc = 100;
ip = malloc(nalloc * sizeof(int)); /* initial allocation */
if(ip == NULL)
{
printf(“out of memory\n”);
exit(1);
}

nitems = 0;

while(getline(line, MAXLINE) != EOF)
{
if(nitems >= nalloc)
{ /* increase allocation */
int *newp;
nalloc += 100;
newp = realloc(ip, nalloc * sizeof(int));
if(newp == NULL)
{
printf(“out of memory\n”);
exit(1);
}
ip = newp;
}

ip[nitems++] = atoi(line);
}
We use two different variables to keep track of the “array” pointed to by ip. nalloc is how many elements we’ve allocated, and nitems is how many of them are in use. Whenever we’re about to store another item in the “array,” if nitems >= nalloc, the old “array” is full, and it’s time to call realloc to make it bigger.
Finally, we might ask what the return type of malloc and realloc is, if they are able to return pointers to char or pointers to int or (though we haven’t seen it yet) pointers to any other type. The answer is that both of these functions are declared (in ) as returning a type we haven’t seen, void * (that is, pointer to void). We haven’t really seen type void, either, but what’s going on here is that void * is specially defined as a “generic” pointer type, which may be used (strictly speaking, assigned to or from) any pointer type.

上段英文文章摘自: http://www.eskimo.com/~scs/cclass/notes/sx11c.html

此篇文章已被阅读2559 次

相关文章

  1. 不一致同步及静态变量延迟初始化的错误
  2. open source fast key-value storage library
  3. 如何单元测试一个android library
  4. adb logcat 如何针对应用包名进行过滤日志?
  5. android 中如何获取电池的电流值

Related Posts

Dalvik VM 架构
xen,xenserver,xcp 有什么区别么?
关于国外空间备案的10个问题

About The Author

The Tester

技术交流,生活学习

Add a Comment

取消回复

邮箱地址不会被公开。 必填项已用*标注

最热文章

  • No results available

近期文章

  • vue2 动态import导入vue提示报错
  • batch udpate sql批量更新数据库中所有表的特定字段值
  • Springboot application启动missing EmbeddedServletContainerFactory错误解决
  • Dubbo使用java api 配置开发禁止使用config center
  • 使用jacoco实时生成后端服务集成测试代码覆盖率报告

近期评论

  • ghj发表在《科学上网trojan的安装和使用》
  • The Tester发表在《cloudflare Rocket Loader建议中国使用者关闭》
  • zhoulujun发表在《cloudflare Rocket Loader建议中国使用者关闭》
  • The Tester发表在《vue+iviewui menu+tabs联动及tabs切换总结》
  • xiaodong.xuexd发表在《sonarqube 7.0 新功能及安装》
  • Josephnex发表在《使用github pages作图床》

标签云

adsense ajax android aws crawler css eclipse findbugs git goagent htmlunit iptables jacoco java javascript jmeter joolma joomla junit jvm lcd linux moco mongodb monkeyrunner mysql oled openvpn oracle pdp php puppet python rabbitmq sonar sonarqube spring springboot ubuntu vpn vue wordpress wrapeasymonkey yii yum

分类目录

© 2025 Life is short Play more | blog | Powered by WordPress & Customizable Blogily