C++ Invalid pointer values [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I would like to know which pointer values are invalid so i would not have to allocate new memory just to mark special chunk states(Memory consumption is critical). So i could use them for special states like
0x00000000 - would mean chunk is not loaded
0x00000001 - would mean chunk is empty
0x00000002 - chunk is full. And when some real stuff needs to be saved to the memory i would do new Chunk(...);

I would suggest just using a struct that contains a pointer and an enum. But if for some reason that's inconvenient, just allocate some small structures and use their addresses just to indicate magic pointer values. (Of course, don't ever free them.)
You can also use the address of static objects. Like this:
static int chunk_not_loaded_i, chunk_empty_i, chunk_full_i;
void *chunk_not_loaded = &chunk_not_loaded_i;
void *chunk_full = &chunk_full_i;
if (some_chunk == chunk_not_loaded)
...

Assigning exact values to the pointer is quite unstable and error-prone. That way, your code would be tight to exact hardware architecture(s). For example, some platforms have 0x00000000 as absolutely valid address.
So the fact that address is assigned or not is not related to numeric value of the pointer (at common case).

0x00000000 is equivalent to NULL, which should be replaced by nullptr in C++11 (which does not have a specified numerical value - though, most implementations treat it as 0 to make backwards compatibility easy - that is not guaranteed, though).
It is the only "special" pointer value. All other values are treated as valid pointer values (meaning attempting to use them would attempt to dereference the pointer - and likely will have bad consequences for values like 0x00000001 or 0x00000002). It sounds like you need a container (e.g. pool) that has a state (which could be an enum or some other value you desire). Alternatively, you could use boost::optional<T> or std::pair<T*, bool> to mark pointers as valid or invalid.

Related

Which is better, to define the variable inside the loop or outside, with huge loop times [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I need to use an array in a loop, and the loop time is very huge.
Case 1: define the array outside the for-loop and pass it to fun2
void fun1(){
int temp[16];
for(int i = 0;i <times; i++)
{
fun2(temp);
}
}
void fun2(int[]& temp){
/** do something with temp*/
}
Case 2: define the array in fun2:
void fun1() {
for (int i = 0; i < times; i++)
{
fun2();
}
}
void fun2() {
int temp[16];
/** do something with temp */
}
fun1 will be called very often. In this situation, which is better?
Does Case 2 have some influence on performance?
If you look for an answer to the general case, the answer is, "it depends." If you want an answer to your specific example, the answer is that the second version will be more efficient.
Ask yourself:
Is there a cost to construction / destruction / reuse?
In your example, there is none (except adjusting the stack pointer, which is extremely cheap). But if it was an array of objects, or if you had to initialize the array to a specific value, that changes.
How does the cost of parameterization factor in?
This is very minor but in your first case, you pass a pointer to the function. If the function is not inlined, this means that the array can only be accessed through that pointer. This takes up one register which could be used otherwise. In the second example, the array can be accessed through the stack pointer, which is basically free.
It also affects alias and escape analysis negatively which can lead to less efficient code. Basically, the compiler has to write values to memory instead of keeping them in registers if it cannot prove that a following memory read may not refer to the same value.
Which version is more robust?
The second version ensures that the array is always properly sized. On the other hand, if you pass an object whose constructor may throw an exception, constructing outside the function may allow you to throw the exception at a more convenient location. This could be significant for exception safety guarantees.
Is there a benefit in deallocating early?
Yes, allocation and deallocation are costly, but early destruction may allow some reuse. I've had cases where deallocating objects early allowed reuse of the memory in other parts of the code which improved use of the CPU cache.
depends on what you want to achieve..in this case, i'm assuming you are looking for performance which case 2 would be the better option as the function would create the variable on the fly instead of trying to get the variable globally then its value.

How does a compiler string together addresses in a linked list? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
How does a compiler know which nodes to point to next in a linked list since each node's next or previous can be anywhere in the heap. Can i attach a specific memory address say 0x00000001 to a pointer and bind it to that address?
Also, can you bind a specific address to a pointer?
Yes. In fact, that is precisely the purpose of a pointer. Memory is addressed by numbers. The first byte is in the address 0, the next is 1 and so on. A pointer is essentially an object that stores a memory address, which is internally simply a number.
In following example, we store the address of the object i into a pointer:
int i;
int* ptr = &i;
How does a compiler string together addresses in a linked list
A linked list node is simply a structure with a pointer to the next (and previous in case of doubly linked list) node. The address of the next node is stored in the pointer.
how does the compiler allocate [...] these addresses during runtime?
In whatever way the language implemnetation chooses to allocate. The language does not specify "how" memory is allocated. The way will differ depending on the storage duration class of the object.
Typically in practice, the language implementation will ask the operating system to map some memory. Variables with automatic storage are stored in what is called the "call stack", and dynamic objects are stored in "free store". How either of these are implemented is outside the scope of my answer. I would suggest studying how operating systems and compilers are made. It's a wide topic.
how does the compiler [...] reference these addresses during runtime?
It stores the address. There are CPU instructions for accessing memory at given address.
Can I for example , bind 0x00000001 to node3?
I don't know what "node3" is, but you can point at the address 1 if you want to. There's not much you can do with the pointer unless there actually is an object at the address. There's no way of creating objects into unallocated memory, and there is no way of allocating memory from arbitrary address in standard C++.

How to change the malloc.c header contents? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Before the user data area begins there are some header contents in the memory block allocated by using malloc.
buffer = (char*) (malloc (i));
int j;
for(j=1;j<9;j++){
printf("before data: %d\n",*(buffer-j));
Output what i got is
before data: 0
before data: 0
before data: 0
before data: 35
before data: 0
before data: 0
before data: 0
before data: 0
I would like to have an extra field in the header which I will be using to set some values. I have tried modifying malloc.c library for setting up custom header but nothing worked. I would like to know if there is any way I can do this.
Your code contains an undefined behavior as you try to access memory you didn't allocate. It may happens, on a given platform, that the memory allocator put the header just before the allocated memory but you can't rely on such.
Create your own memory de-allocation routine to add some more space before, as:
void *my_malloc(size_t s)
char *p = malloc(s+what_you_need);
if (p!=NULL) return (void *)(p+what_you_need);
return (void *)p;
}
void my_free(void *p) {
free(((char *)p)-what_you_need);
}
You may also keep attention on alignment requirement.
If you want to replace malloc, you can do that. Look at how it is done in tcmalloc and jemalloc and do that in your own code.
Before the user data area begins there are some header contents in the memory block allocated by using malloc.
This is wrong, or could be wrong in general.
A clever malloc implementation often handle differently blocks of various sizes. Sometimes, the bytes just before the malloc-ed zone are not used by the malloc implementation.
A possible way of implementing malloc might be to reserve several different large blocks (e.g. by mmap(2)-ing megabytes sized segments) and handle differently small allocation and larger ones, and comparing addresses (against the boundaries of these segments) to compute sizes.
In particular, some malloc implementations are handling allocation of two words cells specifically. They could determine (in their free implementation) the size of a zone e.g. by comparing addresses. For example, a malloc might be coded with the (simplistic) hypothesis that all addresses of the form 0x10yyyyyy where y is an arbitrary hex digit are heap allocation of pairs of words. Of course the details are much more complex in real life, but you got the idea: computation of the size of a heap allocated zone does not have to use some prefix data, it can be done with other ways.
And several malloc implementations do handle malloc of two-words sized cells (very often used for linked lists, or for Lisp "cons" cells) particularly.

Why are memory addresses in pointers important [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I personally believe I have a firm grasp of pointers, but let's say
int* pMyPointer;
int number = 1000;
pMyPointer = &number;
cout << pMyPointer << endl;
pMyPointer might return a memory address of 0037FBB0, but why does that matter? How can this be useful while programming?
Object identity.
If you have two pointers or references, how can you tell if using one could affect the other? Simply printing the current value of all the data members won't tell you if they are the same object or clones/copies.
So when you're debugging, you become very interested in whether the addresses stored in different pointers are the same, which requires you to inspect those address values.
This is a very important information in static memory management.
In userspace application development, this information might be irrelevant to the most developers, but they are very important for the low-level developer. Remember, programming languages with static memory manager will always be lower level languages.
This also the main point in how pointers work. They store memory addresses to where they point.
You can also create a pointer like this: int *mptr = (int*)0x13371234;. This creates a pointer which points to the int at 0x13371234.
It also gives you information about where your stuff in stored, and this can also be used to determine the location of the stack, if for any reason inline assembly is not allowed. If you use malloc, then it is generally not a must-need information.
A typical use is to create linked data structures like lists and trees.
For example, in a binary tree each node contains a pointer to its two children.
As far as the pointer goes, the actual value only rarely means much in itself. It's usually used as a "magic cookie" -- an essentially "magic" value gives you access to some particular variable. In a typical case you save an address into a pointer, then dereference the pointer to get to the item at that address, without ever examining (or even caring about) the value of the pointer itself.
There are a few exceptions to this general rule though. For one example, some memory allocators use the address of a block to track not only the location of the block, but also the block's size. By starting with a block aligned to a large boundary, and always splitting blocks by powers of 2, the whole address tells the location of the block, and the lower bits of the address tell the size of the block that must have been allocated to get to that address.
The latter are definitely the exceptions though. The typical case is that the value of the pointer means nothing beyond giving access to the item at that address.

how are the memory cells organized on a RAM and how is it related to C++? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
As far as I can understand, RAM is organized like a net of rows and columns of cells, each cell containing 1 byte. Also, each cell is label with an address memory written in hexadecimal system. Is this so? Now, when running a c++ program, I suppose it uses the RAM as a mean of storage. In this case, as the char type on c++ is the basic unit of storage, is this size of a char exactly the same as the cell (1 byte)?, does the size of a char depends on the size of a cell (in case the size of a cell is not 1 byte)?, does it depend on the compiler? Thank you so much.
It is easy to visualize RAM as a net of rows and columns. This is how most CS classes teach students as well and for most purposes this would do well at a conceptual level.
One thing you must know while writing C++ programs is the concept of 2 different memories: stack and heap. Stack is memory that stores variables when they come in scope. When they go out of scope, they are removed. Think of this as a stack implementation (FIFO).
Now, heap memory is slightly more complicated. This does not have anything to do with scope of the variable. You can set a fixed memory location to contain a particular value and it will stay there until you free it up. You can set the heap memory by using the 'new' keyword.
For instance: int* abc = new int(2);
This means that the pointer abc points to a heap location with the value '2'. You must explicitly free the memory using the delete keyword once you are done with this memory. Failure to do so would cause memory leaks.
In C, the type of a character constant like a is actually an int, with size of 4. In C++, the type is char, with size of 1. The size is NOT dependent on compiler. The size of int, float and the like are dependent on the configuration of your system (16/32/64-bit). Use the statement:
int a=5;
cout<<sizeof(a)<<endl;
to determine the size of int in your system.