Hundred battles c++ (8)

43. Write a function that finds the first position of a substring (m) in a string (n).

The KMP algorithm has the best efficiency, and the time complexity is O(n+m).

44. Memory allocation problems with multiple inheritance:

For example, class A: public class B, public class C {}

So what is the memory structure of A roughly like?

This is compiler-dependent, and different implementations may have different details.

It is quite simple if virtual functions and virtual inheritance are not considered; otherwise, it is quite complicated.

You can refer to "Deep Exploration of the C++ Object Model", or:

http://blog.csdn.net/wfwd/archive/2006/05/30/763797.aspx

45. How to judge that a singly linked list has a ring? (Note that flag bits cannot be used, and only two additional pointers can be used at most)

struct node { char val; node* next;}

bool check(const node* head) {} ​​//return false: no loop; true: loop

One way of O(n) is (make two pointers, one increments by one step each time, and the other increments by two steps each time, if there is a ring, the two must overlap, and vice versa):

bool check(const node* head)

{

if(head==NULL) return false;

node *low=head, *fast=head->next;

while(fast!=NULL && fast->next!=NULL)

{

low=low->next;

fast=fast->next->next;

if(low==fast) return true;

}

return false;

}

1. The information of a student is: name, student number, gender, age and other information. Use a linked list to link these student information together, give an age, and delete the student information whose age is equal to age in these linked lists.

code

#i nclude "stdio.h"

#i nclude "conio.h"

struct stu{

char name[20];

char sex;

int no;

int age;

struct stu * next;

}*linklist;

struct stu *framelist(int n)

{

int i;

//h is the head node, p is the previous node, s is the current node

struct stu *h,*p,*s;

h = (struct stu *)malloc(sizeof(struct stu));

h->next = NULL;

p=h;

for(i=0;i<n;i++)

{

s = (struct stu *)malloc(sizeof(struct stu));

p->next = s;

printf("Please input the information of the student: name sex no age n");

scanf("%s %c %d %d",s->name,&s->sex,&s->no,&s->age);

s->next = NULL;

Accessing fixed memory locations (Accessing fixed memory locations) C C++ Development

10. Embedded systems are often characterized by requiring the programmer to access a specific memory location. In a certain project, it is required to set the value of an integer variable whose absolute address is 0x67a9 to 0xaa66. The compiler is a pure ANSI compiler. Write code to accomplish this task.

This question tests whether you know that it is legal to typecast an integer to a pointer in order to access an absolute address. How this is accomplished varies with individual style. Typical similar code is as follows:

int *ptr;

ptr = (int *)0x67a9;

*ptr = 0xaa55;

A more obscure method is:

*(int * const)(0x67a9) = 0xaa55;

Even if your tastes are closer to the second option, I recommend that you use the first option when interviewing.

Interrupts

11. Interrupts are an important part of embedded systems, which has led many compiler developers to provide an extension—to allow standard C to support interrupts. The representative fact is that a new keyword __interrupt was created. The following code uses the __interrupt keyword to define an interrupt service subroutine (ISR), please comment on this code.

__interrupt double compute_area (double radius)

{

double area = PI * radius * radius;

printf(" Area = %f", area);

return area;

}

There are so many errors in this function that people don't know where to start:

1). ISR cannot return a value. If you don't understand this, then you won't be hired.

2). ISR cannot pass parameters. If you don't see this, your chances of getting hired are equal to the first item.

3). In many processors/compilers, floating point is generally not reentrant. Some processors/compilers need to push registers at the front of the stack, and some processors/compilers just don't allow floating point operations in the ISR. Also, ISRs should be short and efficient, it's not wise to do floating point operations in ISRs.

4). In line with the third point, printf() often has reentrancy and performance problems. If you lose the third and fourth points, I won't give you too much trouble. Needless to say, if you can get the last two points, your employment prospects are getting better and better.

Code examples

Guess you like

Origin blog.csdn.net/hebtu666/article/details/127205307