System level Programming study(1)
1. 内存分配导致无意 修改数据,无限循环:
#define ARRAY_SIZE 10
void natural_numbers (void)
{
int i;
int array[ARRAY_SIZE];
i = 1;
![]()
while (i <= ARRAY_SIZE)
{
array[i] = i - 1;
i = i + 1;
}
}
2. C语言中,string是character array ,必须明确终止,错误程序:
#include <stdio.h>
#include <string.h>
#define MAXLINE_LENGTH 80
char Buffer[MAXLINE_LENGTH];
char * readString(void)

{
int nextInChar;
int nextLocation;
printf("Input> ");
nextLocation = 0;
while ((nextInChar = getchar()) != '\n' &&
nextInChar != EOF)
{
Buffer[nextLocation++] = nextInChar;
}
return Buffer;
}

int main(int argc, char * argv[]) 

{
char * newString;

do
{
newString = readString();
printf("%s\n", newString);
} while (strncmp(newString, "exit", 4));
return 0;
}
memset(Buffer,NULL,MAXLINE_LENGTH*sizeof(char));3.指针引用:
#include <stdio.h>
#include <iostream.h>
void Initialize (char * a, char * b)

{
a[0] = 'T'; a[1] = 'h'; a[2] = 'i';
a[3] = 's'; a[4] = ' '; a[5] = 'i';
a[6] = 's'; a[7] = ' '; a[8] = 'A';
a[9] = '\0';
b = a;
cout<<b<<endl;//this is A
b[8] = 'B';
cout<<b<<endl;//this is B
}
#define ARRAY_SIZE 10
char a[ARRAY_SIZE];
char b[ARRAY_SIZE];
int main(int argc, char * argv[])

{
Initialize(a, b);
cout<<b<<endl; // print nothing
return 0;
}