Profile

ys2310

Author:ys2310
2008年春にNew York Cityにあるふる〜い大学を卒業。


Categories


new postings


new comments


new trackbacks


monthly archeive


FC2ブログ 転職
DATE: CATEGORY:スポンサー広告
上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。
| BLOG TOP |
DATE: CATEGORY:C/C++ note
int *b = (int *)malloc(sizeof(int));
double *b2 = (double *)malloc(sizeof(double));
double *b3 = (double *)malloc(sizeof(int));

cout<<&b<<endl;
cout<<b<<endl;
cout<<sizeof(*b)<<endl;
cout<<sizeof(*b2)<<endl;
cout<<sizeof(*b3)<<endl;

output:
002DFE50
00B04CB0
4
8
8

上から分かるように、b2, b3 のheap memory の割り当てられたsize の合計はmalloc された大きさですが、ここの単位は宣言された型のsize です。
| BLOG TOP |
DATE: CATEGORY:C/C++ note
Q: why scanf("%d",&n) and gets(string) don't work together?

This is a general problem of the keyboard buffer. When scanf("%d",&n); is used and when an input is entered, the newline character("n") along with the input is stored in buffer. This buffer gets clear as the user presses "Enter" or if the buffer is full.

So when we use the scanf("%d",&n); the "n" is left in the buffer itself. So the buffer will fill the string with "". Hence it will not print anything in place of string.

The Following code will help in understanding this point.

#include
int main()
{
int n;
char string[20];

printf("String ::%s",string);

printf("Enter the number");
scanf("%d",&n);

printf("nEnter the string");
gets(string);

printf("Number ::%d",n);
printf("String ::%s",string);
if(strcmp(string,"") == 0)
printf("Null string");
system("pause");
return 0;
}

Here initially i am trying to print the contents of the string.... It gives garbage.Later
in the output Null string will be displayed. Which means the string is containing "".

Note :
When we change the order, ie. gets(string); and then scanf(%d",&n); we will not have this problem.

Solution :
The solution for such problems is to use fflush(stdin); This clears the keyboard buffer. Hence we can get the desired output. This fflush() should be used after entering the input. ie after scanf() and before gets().

#include
int main()
{
int n;
char string[20];

printf("String ::%s",string);
printf("Enter the number");
scanf("%d",&n);
fflush(stdin);
printf("nEnter the string");
gets(string);

printf("Number ::%d",n);
printf("String ::%s",string);
if(strcmp(string,"") == 0)
printf("Null string");
system("pause");
return 0;
}

[Quote]:http://www.geekinterview.com/question_details/55936

| BLOG TOP |
DATE: CATEGORY:C/C++ note
The #pragma preprocessor directive allows each compiler to implement compiler-specific features that can be turned on and off with the #pragma statement. For instance, your compiler might support a feature called loop optimization. This feature can be invoked as a command-line option or as a #pragma directive. To implement this option using the #pragma directive, you would put the following line into your code:

#pragma loop_opt(on)

Conversely, you can turn off loop optimization by inserting the following line into your code:

#pragma loop_opt(off)

consider an example

struct  test
{
    char a;
    int b;
    char c;
};

sizeof struct test is 12 instead of 6

if we declare #pragma() Before struct test
Then size will Be 6.

This directive is valid for MS compilers.

[Quote]:http://www.geekinterview.com/question_details/3385

| BLOG TOP |
DATE: CATEGORY:C/C++ note

When a variable is declared as being a pointer to type void it is known as a generic pointer. Since you cannot have a variable of type void, the pointer will not point to any data and therefore cannot be dereferenced. It is still a pointer though, to use it you just have to cast it to another kind of pointer first. Hence the term Generic pointer.

This is very useful when you want a pointer to point to data of different types at different times.

Here is some code using a void pointer:

#include <stdio.h>
int main()
{
    int  num[3] = {10,20,30};
    char name[30] = "Welcome to C World";
    int *pint = NULL;
    void *pvoid = NULL;
    int i;
   
   
    pint = &num;
    for ( i=0; i<3; i++ )
     printf("%d ",*(pint + i ));
     printf("n");
    
     // The same can be done using void pointer as follows.
     pvoid = &num;
    for ( i=0; i<3; i++ )
     printf("%d ",*((int *)pvoid + i ));
    
     // Same void pointer can be cast to char.
     printf("n");
     pvoid = name;
     for( i=0; i < strlen( name); i++ )
          printf("%c", *((char *) pvoid + i));
    getch();
}




[Quote]:http://www.geekinterview.com/question_details/58072

| BLOG TOP |
DATE: CATEGORY:C/C++ note
今日は型宣言の解読のコツを教えします。

例として、
const int a;                     //   変数a as const int 型
int const a;                     //   変数a of const int 型
const int * const a        //   変数 a of const な pinter to const な int 型
int *a[3]                           //  array a[3] of pointer to int
int (*a)[3]                        //   pointer to array of int

色分けからわかるように、右辺から解読していきtype specifier 直前まで解読していき、最後に左側を解読する手順である。これは実際に C/C++ compiler が使う手順とほぼ同じと考えられる。

cdecl command でテストをされると同じ結果が得られます。

int (*a)[3] は無理やり括弧をつけて、解読順番を変えているので先に(*a) はpointer と解釈されます。そのあと、残ったint [3] が配列とみなされるわけです。

reference operator & についても同じです。
int &a[3]
int (&a)[3]

試してみてください。

| BLOG TOP |

copyright © Manhattan life all rights reserved.Powered by FC2ブログ