开发者

int* variable or int variable

I am working in C++ and have been using pointers a lot lately. I found that there are a fe开发者_StackOverflow中文版w ways to initialize the chunks of memory that I need to use.

void functioncall(int* i)
{
    *i = *i + 1;
}

int main(){
    int* a = (int*)malloc(sizeof(int));
    int az = 0;

    functioncall(a);
    functioncall(&az);
}

Notice that the first variable int* a is declared as a pointer and then I malloc the memory for it. But, with az it is not a pointer but when calling the function I get the address of the memory.

So, my question is: is there a preferred way =, or is there any penalties one over the other?


int* a = (int*)malloc(sizeof(int));

This allocates memory on the heap. You have to deallocate it on your own, or you'll run into memory leaks. You deallocate it by calling free(a);. This option is most definitely slower (since the memory has to be requested and some other background stuff has to be done) but the memory may be available as long as you call free.


int az = 0;

This "allocates" memory on the stack, which means it gets automatically destroyed when you leave the function it is declared (unless for some really rare exceptions). You do not have to tidy up the memory. This option is faster, but you do not have control over when the object gets destroyed.


a is put onto the heap, az is on the stack. The heap you are responsible to freeing the memory. With the stack when it goes out of scope it is automatically free. So the answer is when you want the data to be placed and if you require if at the end of the scope.

PS You should use new in C++


In general you should avoid dynamic memory allocations (malloc, calloc, new) when it's reasonably easy: they are slower than stack allocations, but, more importantly, you must remember to free (free, delete) manually the memory obtained with dynamic allocation, otherwise you have memory leaks (as happens in your code).


I'm not sure what you're trying to do, but there is almost never a reason for allocating a single int (nor an array of int, for that matter). And there are at least two errors in your functioncall: first, it fails to check for a null pointer (if the pointer can't be null, pass by reference), and second, it doesn't do anything: it increments the copy of the pointer passed as an argument, and then dereferences the initial value and throws out the value read.


Allocating small variables directly on the stack is generally faster since you don't have to do any heap operations. There's also less chance of pointer-related screwups (e.g., double frees). Finally, you're using less space. Heap overheads aside, you're still moving a pointer and an int around.


The first line (int* a = ...) is called dynamically-allocated variable, it is usually used if you don't know before the runtime that how much variables you needed, or if you need it at all.

The second line (int az = 0) is called automatic variable, it is used more regularly.


int az = 0;
functioncall(a);

This is okay, as far as behavior is concerned.

int* a = (int*)malloc(sizeof(int));
functioncall(&az);

This invokes undefined-behaviour (UB), inside the function, when you do *i++. Because malloc only allocates the memory, it does not initialize it. That means, *i is still uninitialized, and reading an uninitialized memory invokes UB; that explains why *i++ is UB. And UB, if you know, is the most dangerous thing in C++, for it means, anything can happen.

As for the original question, what would you prefer? So the answer is, prefer automatic variable over pointer (be it allocated with malloc or new).

Automatic means Fast, Clean and Safe.


func(typename* p)

pointer is call value

*p++ is *p and p++

if change this pointer , not change original.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜