Questions tagged [alloc]

207 questions
93
votes
8 answers

What's the advantage of using std::allocator instead of new in C++?

I've just read about std::allocator. In my opinion, it is more complicated to use it instead of using new and delete. With allocator we must explicitly allocate heap memory, construct it, destroy it, and then finally deallocate the memory. So why…
Mugurel
  • 1,829
  • 3
  • 17
  • 26
51
votes
6 answers

what is difference between alloc and allocWithZone:?

From forum discussion , seem like that the big difference is performance factor, allocWithZone: will alloc memory from particular memory area, which reduce cost of swapping. In practice, almost get no chance to use allocWithZone: , anyone can give…
Forrest
  • 122,703
  • 20
  • 73
  • 107
33
votes
2 answers

Is a malloc() needed before a realloc()?

Since I had read realloc will act as malloc if the size pointed is 0, I was using it without malloc(), provided the pointer was static, global, or explicitly set to NULL if automatic. However, I notice a lot of programmers try to set it or set it to…
j riv
  • 3,593
  • 6
  • 39
  • 54
27
votes
4 answers

Lazy instantiation in Objective-C/ iPhone development

Quick question... Well I understand that all properties start out as nil in Objective-C and that sending a message to nil does nothing, therefore you must initialize using [[Class alloc] init]; before sending a message to a newly created property. …
23
votes
5 answers

Objective-C asking for alloc on swift class

Some small steps to begin wrapping my head around Swift. I've basically ported an old class that simply finds the matching icon for a name and return the appropriate UIImage. The Swift part of things seems to be up and running, and looks (almost)…
T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32
22
votes
6 answers

is it necessary to call pointer = NULL when initializing?

when I create a pointer to certain struct, do I have to set it to NULL, then alloc it then use it? and why?
user1051003
  • 1,211
  • 5
  • 16
  • 24
16
votes
1 answer

What does it mean double free detected in tcache 2 while using mpz?

I use this program to store a mpz value but when I add a 0 ( 400000000000000000000000000000000000000 instead of 40000000000000000000000000000000000000 -> 38 0s instead of 37) I get free(): double free detected in tcache 2 Aborted (core…
user11903678
15
votes
2 answers

How to release static Objective-C variables

The StackOverflow question "using static keyword in objective-c when defining a cached variable" references code from Example 4 of Xcode's TableViewSuite that defines a static NSDateFormatter and calls alloc but never calls release. Shouldn't static…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
15
votes
7 answers

Why are alloc and init called separately in Objective-C?

Note: I'm relatively new to Objective-C and am coming from Java and PHP. Could someone explain to me why I always have to first allocate and then initialize an instance? Couldn't this be done in the init methods like this: + (MyClass*)init { …
André Hoffmann
  • 3,505
  • 1
  • 25
  • 39
11
votes
2 answers

Automatic Reference Counting Issue: Assigning retained object to unsafe_unretained variable; object will be released after assignment

I'm getting this warning "Automatic Reference Counting Issue: Assigning retained object to unsafe_unretained variable; object will be released after assignment" Here is the code .h @interface myObject : NSObject { } @property (assign) id…
zeroonnet
  • 175
  • 1
  • 2
  • 8
7
votes
4 answers

Why is it not recommended to allocate and initialize with id?

In the following example, what are the possible problems that can occur. id c = [Person alloc]; [c init];
Matthew Bischoff
  • 1,043
  • 11
  • 27
7
votes
6 answers

Why does [[NSError alloc] init]; in Xcode throw an error?

I have the following code in Xcode : NSError *error = [[NSError alloc] init]; NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; And it throws following error in the logs [NSError init]…
Ludo
  • 743
  • 1
  • 10
  • 25
7
votes
4 answers

C++ uninitialized array of class instances

I've been searching but couldn't find an answer to this. Is there a way to tell the new operator to not call the class constructors? MyObject* array = new MyObject[1000]; This will call MyObject() a thousand times! I want to fill the allocated…
Niklas R
  • 16,299
  • 28
  • 108
  • 203
7
votes
3 answers

Using alloc, init in ARC enabled projects

Actually I am working on a project with ARC enabled. I know using alloc and init is taking ownership of the object. I know, If I create a string like this NSString *myString = [[NSString alloc]initWithFormat:@"Something"]; then I need to release…
Dinesh Raja
  • 8,501
  • 5
  • 42
  • 81
6
votes
2 answers

can we override alloc and dealloc in objective C?

I know that this is rarely required to override the alloc or dealloc methods,but if required is it possible in iPhone programming?
User97693321
  • 3,336
  • 7
  • 45
  • 69
1
2 3
13 14