某公司C语言笔试题

呵-呵呵 2012-02-19 06:13:41
用C或者C++写一套通用函数。要求实现以下功能。
在一段连续内存(不大于10MB)中存储数据,要求FIFO。
至少有两个函数:
存入数据,将存入的数据放在缓冲区数据的尾部。第一参数为将要存入的数据首地址,第二参数为数据宽度。
取出数据,取出缓冲区最后的数据。第一参数为将要存入数据的指针,第二参数为数据宽度。
尽量提高效率。
...全文
397 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
luciferisnotsatan 2012-02-21
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 ypyf3000 的回复:]

C/C++ code
char data[10485760];

这样就够了 用全局静态的数组,比动态分配快
[/Quote]
没多大差别吧。只有不停的malloc,free才会慢。一次malloc哪会有差异。
luciferisnotsatan 2012-02-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 leon57 的回复:]

引用 3 楼 reyoung1110 的回复:

so easy.

记录一个尾指针,复制出去复制进来都用memcpy,整体代码不会超过50行的。

我也做的是直接malloc 10MB内存,结果面试官说效率太低了。
[/Quote]
连续10M内存,不malloc,那用什么?
ketet 2012-02-21
  • 打赏
  • 举报
回复
char data[10485760]; 

这样就够了 用全局静态的数组,比动态分配快
赵4老师 2012-02-21
  • 打赏
  • 举报
回复
仅供参考
//循环向a函数每次发送200个字节长度(这个是固定的)的buffer,
//a函数中需要将循环传进来的buffer,组成240字节(也是固定的)的新buffer进行处理,
//在处理的时候每次从新buffer中取两个字节打印
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <process.h>
#include <io.h>
//Log{
#define MAXLOGSIZE 10000000
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
char logstr[16000];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
void Lock(CRITICAL_SECTION *l) {
EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
LeaveCriticalSection(l);
}
void LogV(const char *pszFmt,va_list argp) {
struct tm *now;
struct timeb tb;

if (NULL==pszFmt||0==pszFmt[0]) return;
if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;
ftime(&tb);
now=localtime(&tb.time);
sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec );
sprintf(mss,"%03d",tb.millitm);
printf("%s %s.%s %s",datestr,timestr,mss,logstr);
flog=fopen(logfilename1,"a");
if (NULL!=flog) {
fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
if (ftell(flog)>MAXLOGSIZE) {
fclose(flog);
if (rename(logfilename1,logfilename2)) {
remove(logfilename2);
rename(logfilename1,logfilename2);
}
flog=fopen(logfilename1,"a");
if (NULL==flog) return;
}
fclose(flog);
}
}
void Log(const char *pszFmt,...) {
va_list argp;

Lock(&cs_log);
va_start(argp,pszFmt);
LogV(pszFmt,argp);
va_end(argp);
Unlock(&cs_log);
}
//Log}
#define ASIZE 200
#define BSIZE 240
#define CSIZE 2
char Abuf[ASIZE];
char Cbuf[CSIZE];
CRITICAL_SECTION cs_HEX ;
CRITICAL_SECTION cs_BBB ;
struct FIFO_BUFFER {
int head;
int tail;
int size;
char data[BSIZE];
} BBB;
int No_Loop=0;
void HexDump(int cn,char *buf,int len) {
int i,j,k;
char binstr[80];

Lock(&cs_HEX);
for (i=0;i<len;i++) {
if (0==(i%16)) {
sprintf(binstr,"%03d %04x -",cn,i);
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
} else if (15==(i%16)) {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
sprintf(binstr,"%s ",binstr);
for (j=i-15;j<=i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
Log("%s\n",binstr);
} else {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
}
}
if (0!=(i%16)) {
k=16-(i%16);
for (j=0;j<k;j++) {
sprintf(binstr,"%s ",binstr);
}
sprintf(binstr,"%s ",binstr);
k=16-k;
for (j=i-k;j<i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
Log("%s\n",binstr);
}
Unlock(&cs_HEX);
}
int GetFromRBuf(int cn,CRITICAL_SECTION *cs,FIFO_BUFFER *fbuf,char *buf,int len) {
int lent,len1,len2;

lent=0;
Lock(cs);
if (fbuf->size>=len) {
lent=len;
if (fbuf->head+lent>BSIZE) {
len1=BSIZE-fbuf->head;
memcpy(buf ,fbuf->data+fbuf->head,len1);
len2=lent-len1;
memcpy(buf+len1,fbuf->data ,len2);
fbuf->head=len2;
} else {
memcpy(buf ,fbuf->data+fbuf->head,lent);
fbuf->head+=lent;
}
fbuf->size-=lent;
}
Unlock(cs);
return lent;
}
void thdB(void *pcn) {
char *recv_buf;
int recv_nbytes;
int cn;
int wc;
int pb;

cn=(int)pcn;
Log("%03d thdB thread begin...\n",cn);
while (1) {
Sleep(10);
recv_buf=(char *)Cbuf;
recv_nbytes=CSIZE;
wc=0;
while (1) {
pb=GetFromRBuf(cn,&cs_BBB,&BBB,recv_buf,recv_nbytes);
if (pb) {
Log("%03d recv %d bytes\n",cn,pb);
HexDump(cn,recv_buf,pb);
Sleep(1);
} else {
Sleep(1000);
}
if (No_Loop) break;//
wc++;
if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc);
}
if (No_Loop) break;//
}
}
int PutToRBuf(int cn,CRITICAL_SECTION *cs,FIFO_BUFFER *fbuf,char *buf,int len) {
int lent,len1,len2;

Lock(cs);
lent=len;
if (fbuf->size+lent>BSIZE) {
lent=BSIZE-fbuf->size;
}
if (fbuf->tail+lent>BSIZE) {
len1=BSIZE-fbuf->tail;
memcpy(fbuf->data+fbuf->tail,buf ,len1);
len2=lent-len1;
memcpy(fbuf->data ,buf+len1,len2);
fbuf->tail=len2;
} else {
memcpy(fbuf->data+fbuf->tail,buf ,lent);
fbuf->tail+=lent;
}
fbuf->size+=lent;
Unlock(cs);
return lent;
}
void thdA(void *pcn) {
char *send_buf;
int send_nbytes;
int cn;
int wc;
int a;
int pa;

cn=(int)pcn;
Log("%03d thdA thread begin...\n",cn);
a=0;
while (1) {
Sleep(100);
memset(Abuf,a,ASIZE);
a=(a+1)%256;
if (16==a) {No_Loop=1;break;}//去掉这句可以让程序一直循环直到按Ctrl+C或Ctrl+Break或当前目录下存在文件No_Loop
send_buf=(char *)Abuf;
send_nbytes=ASIZE;
Log("%03d sending %d bytes\n",cn,send_nbytes);
HexDump(cn,send_buf,send_nbytes);
wc=0;
while (1) {
pa=PutToRBuf(cn,&cs_BBB,&BBB,send_buf,send_nbytes);
Log("%03d sent %d bytes\n",cn,pa);
HexDump(cn,send_buf,pa);
send_buf+=pa;
send_nbytes-=pa;
if (send_nbytes<=0) break;//
Sleep(1000);
if (No_Loop) break;//
wc++;
if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc);
}
if (No_Loop) break;//
}
}
int main() {
InitializeCriticalSection(&cs_log );
Log("Start===========================================================\n");
InitializeCriticalSection(&cs_HEX );
InitializeCriticalSection(&cs_BBB );

BBB.head=0;
BBB.tail=0;
BBB.size=0;

_beginthread((void(__cdecl *)(void *))thdA,0,(void *)1);
_beginthread((void(__cdecl *)(void *))thdB,0,(void *)2);

if (!access("No_Loop",0)) {
remove("No_Loop");
if (!access("No_Loop",0)) {
No_Loop=1;
}
}
while (1) {
Sleep(1000);
if (No_Loop) break;//
if (!access("No_Loop",0)) {
No_Loop=1;
}
}
Sleep(3000);
DeleteCriticalSection(&cs_BBB );
DeleteCriticalSection(&cs_HEX );
Log("End=============================================================\n");
DeleteCriticalSection(&cs_log );
return 0;
}
呵-呵呵 2012-02-21
  • 打赏
  • 举报
回复
多谢各位的解答,附上VC++下malloc 10MB内存的profile。

Profile: Function timing, sorted by time
Date: Tue Feb 21 13:40:41 2012


Program Statistics
------------------
Command line at 2012 Feb 21 13:40: "E:\malloc\Debug\malloc"
Total time: 62.051 millisecond
Time outside of functions: 55.306 millisecond
Call depth: 1
Total functions: 1
Total hits: 1
Function coverage: 100.0%
Overhead Calculated 1267
Overhead Average 1267

Module Statistics for malloc.exe
--------------------------------
Time in module: 6.745 millisecond
Percent of time in module: 100.0%
Functions in module: 1
Hits in module: 1
Module function coverage: 100.0%

Func Func+Child Hit
Time % Time % Count Function
---------------------------------------------------------
6.745 100.0 6.745 100.0 1 _main (malloc.obj)

krinde 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 leon57 的回复:]

引用 3 楼 reyoung1110 的回复:

so easy.

记录一个尾指针,复制出去复制进来都用memcpy,整体代码不会超过50行的。

我也做的是直接malloc 10MB内存,结果面试官说效率太低了。
[/Quote]


那应该如何? 该怎样分配内存?
downmooner 2012-02-20
  • 打赏
  • 举报
回复
至少有两个函数:
存入数据,将存入的数据放在缓冲区数据的尾部
取出数据,取出缓冲区最后的数据.
呵-呵呵 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 downmooner 的回复:]

要求FIFO 是不是写错了.按照后面的加入,取出数据完全是栈。
[/Quote]

呃,是队列,没错。取最前面的数据。
呵-呵呵 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 reyoung1110 的回复:]

so easy.

记录一个尾指针,复制出去复制进来都用memcpy,整体代码不会超过50行的。
[/Quote]
我也做的是直接malloc 10MB内存,结果面试官说效率太低了。
downmooner 2012-02-19
  • 打赏
  • 举报
回复
要求FIFO 是不是写错了.按照后面的加入,取出数据完全是栈。
qq120848369 2012-02-19
  • 打赏
  • 举报
回复
很明显是个字节流环形队列, 需要注意队列满的判定方法: 剩余内存小于 < 阈值, 则认为队列满.
caddor 2012-02-19
  • 打赏
  • 举报
回复
reyoung1110 2012-02-19
  • 打赏
  • 举报
回复
so easy.

记录一个尾指针,复制出去复制进来都用memcpy,整体代码不会超过50行的。
ryfdizuo 2012-02-19
  • 打赏
  • 举报
回复
题目要求是在连续内存上操作,用链表不能满足要求吧。
Isnis-fallen 2012-02-19
  • 打赏
  • 举报
回复
队列

queue.h

#ifndef _mytest_queue
#define _mytest_queue

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

typedef struct _mytest_QueueData
{
int num; // 存储在节点中的数据长度
char stu[100];
}mytest_QueueData;

typedef struct _mytest_Node
{
mytest_QueueData *data;
struct _mytest_Node *next; // 队列中的下一个节点地址
}mytest_NODE;

typedef struct _mytest_Queue
{
mytest_NODE *head; // 队列的头部
mytest_NODE *end; // 队列的尾部
int count; // 队列长度
}mytest_QUEUE;

void mytest_Init(mytest_QUEUE *queue )
{
if( NULL == queue )
return ;
queue->head = NULL;
queue->end = NULL;
queue->count = 0;
}

void mytest_Push(mytest_QUEUE *queue, mytest_QueueData *queue_data)
{
if( NULL == queue || NULL == queue_data )
return;
mytest_NODE *new_node = (mytest_NODE *)malloc(sizeof(mytest_NODE) );
if( NULL == new_node )
return;
new_node->data=(mytest_QueueData *)malloc(sizeof(mytest_QueueData));
if( NULL == new_node->data )
return;
memcpy(new_node->data,queue_data,sizeof(mytest_QueueData));
new_node->next = NULL;
if( queue->head == NULL )
{
queue->head = new_node;
queue->end = new_node;
}
else
{
queue->end->next = new_node;
queue->end = new_node;
}
queue->count ++;
}

mytest_QueueData *mytest_Front(mytest_QUEUE *queue )
{
if( NULL == queue )
return NULL;
if( NULL == queue->head )
return NULL;
mytest_QueueData *queuedata_tmp = queue->head->data;
return queuedata_tmp;
}

void mytest_Pop( mytest_QUEUE *queue )
{
if( NULL == queue )
return;
if( NULL == queue->head )
return;
mytest_NODE *node_tmp = queue->head;
mytest_QueueData *queuedata_tmp=node_tmp->data;
queue->head = node_tmp->next;
if( NULL == queue->head )
queue->end = NULL;
queue->count --;
free(queuedata_tmp);
free(node_tmp);
queuedata_tmp=NULL;
node_tmp=NULL;
}

void mytest_FreeQueue(mytest_QUEUE *queue )
{
if( queue )
return;
mytest_NODE *tmp_node1 = queue->head;
while(tmp_node1)
{
mytest_NODE *tmp_node2 = tmp_node1;
free(tmp_node1->data);
free(tmp_node1);
tmp_node1->data = NULL;
tmp_node1 = NULL;
tmp_node1 = tmp_node2->next;
}
}

#endif







test.c



#include <stdio.h>
#include "include/queue.h"
mytest_QUEUE qe;

void test()
{
mytest_QueueData qd;
int i;

for(i=0;i<1000000;i++)
{
sprintf(qd.stu,"%d:fffzcvz",i);
qd.num=i;
mytest_Push(&qe,&qd);
}
while(qe.count>0)
{
printf("%s\n",mytest_Front(&qe)->stu);
mytest_Pop(&qe);
}
printf("end1 \n");
}

int main()
{

mytest_Init(&qe);
int n=0;
while(n++<1000)
test();

printf("fff");
return 0;
}

史上最全的android和java面试文档集。包括有: java程序员面试宝典.txt Java面试宝典2011版-1C,Java基础部分.doc 三大框架面试题.zip 技术面试题.zip Android笔试题C语言终极面试宝典.doc Java笔试题目汇总.pdf Java面试宝典_2010.doc 面试全攻略100题.doc 智力题和答案.doc C语言面试题大汇总.doc 计算机专业必备笔试面试锦囊.doc 笔试.rar 面试题集合.zip C语言面试题大汇总1.doc 面试题集合 java面试题 JAVA面试题集合(项目2部).chm 华为笔试题大全(史上最齐全).doc JAVA题库.doc java面试题.zip Java面试宝典2011版-1A,Java基础部分.doc jsp笔试题全集.doc Java学习笔记(必看经典).doc android和java面试大全.rar JAVA精华.doc JAVA经典算法50题1.doc Android笔试题库.rar Java面试宝典2011版-1B,Java基础部分.doc Java工程师试题(机构招聘)20100526.doc C语言面试题大汇总面试题及答案.doc Java23种设计模式(总结)1.doc JAVA_WEB面试笔试题.doc 实施人员初试题20091009.doc 笔试智力题.doc C语言面试题.doc 2011android面试题目及其答案大全.doc java基础笔试题.doc 安卓鄙视题附答案.txt 史上最全的android面试题库.docx android工程师笔试试卷.doc Android面试题(详细答案).doc Android内测题.doc Java面试题2.doc JAVA面试题80页.doc 智力题.doc JAVA 综合面试题.pdf 分类后的葵花宝典 葵花宝典.doc 九阴真经.doc Java面试题1.doc 葵花宝典-数据库类.doc JAVA面试题集锦.doc Java程序员,面试必读.txt 软通动力招聘测试题.doc java面试葵花宝典.doc 新建 文本文档 (2).txt 125条常见的JAVA面试题.doc NET易筋经.doc 葵花宝典-Java Web类.doc 葵花宝典-基础类.doc 葵花宝典-数据库类1.doc 技术测评java.doc 技术测评.net.doc 瑞星笔试题(15道).doc 汉端笔试题(7页).doc 一道测试notepad笔试题.doc 奇虎面试题.doc 喜安科 面试题.doc 北京博彦科技笔试+面试.doc 清华同方开发的面试题 (有兴趣的看一下了 !) 中软的面试题(转贴).doc 亚控科技比试题.doc 神州泰岳测试试题(笔试)转贴.doc 一家通讯公司的面试题目.doc 软件测试工程师试题发布版.doc 某公司的面试试题.doc 一个外包测试公司笔试题!.doc 时力科技面试题.doc 合力金桥的笔试题.doc 一道数据库的笔试题目.doc 传视数码公司的面试题.doc 美国英网软件公司题目.doc 软件测试工程师测试试题大集合(二)包括答案.d 波尔世通的笔试+面试.doc 瓦瑟笔试题(限男性).doc 软通动力面试笔答.doc 常见的测试题(转贴).doc 北京大学计算机科学技术研究所.doc 联合网视面试题.doc 缺陷的等级划分,一个经常被问到的问题.doc 软件测试工程师笔试试题(大集合).doc 千像互动的笔试.doc 2012java面试题分析大全.doc JAVA面试题集合(项目2部).chm JAVA经典算法50题1.doc Java工程师试题(机构招聘)20100526.doc Java23种设计模式(总结)1.doc 实施人员初试题20091009.doc 框架图.png struts面试题 hibernate面试题 三大框架.txt JAVA题库.doc

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧