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 8 years ago.
Improve this question
My question is a conceptual one(just for my knowledge).
Is it possible that we get a loop in memory addresses while using an array of pointers to point to multiple linked lists?
Suppose I have some code like this:
struct linkedList{
int data;
char name[20];
struct linkedList *next;
};
struct linkedList *head[10];
Is it possible that 2 or more pointers in above declaration may end up pointing to same addresses?
If so, how to prevent this situation?
Yes, of course it's possible. That would be called a "circular list", and is sometimes very useful.
It can be prevented by writing code to detect such a situation.
Related
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
I was asked a question during an C Language interview. the question is:
Can I change the address?
struct node * root;
root=(int*)malloc(sizeof(int));
printf("%d",root) =10128000 // new address: root=101590000
None of the C standard library's allocation functions allow you to specify the address where you want the allocated space to be. It would not make sense to do so, because it's very unlikely for the programmer to know or care what specific address they get, unless something known to them is already there, in which case it's not an available address.
You can, however, allocate a large block (e.g. via malloc), and then manually assign chunks of that block however you like. That allows you to choose your own addresses relative to the base of the allocated block. For example:
my_node *node_base = malloc(AS_MUCH_MEMORY_AS_I_NEED);
// ...
// malloc analog:
size_t an_index = choose_a_node_index_by_some_criteria();
my_node *node = node_base + an_index;
// free analog:
mark_index_available_again(an_index);
Of course, the devil is in the details, and those are both specific to your needs and more complex than I'm prepared to go into here. Overall, this is not something that a self-declared beginner really ought to be trying to do.
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 4 years ago.
Improve this question
I was in an interview recently where I was given a piece of paper with a few function signatures and asked to fill in the code, I was also instructed not to "allocate memory".
The question was relatively simple (Smallest value in a list) so I solved it recursively which the interviewers seemed unimpressed by, they seemed to suggest that I could have declared variables on the stack but i was nervous and regrettably didn't press the interviewer on it.
What does it mean to "allocate" memory in C?
Allocate memory means you keep for your program an amount of memory situated in the HEAP section. On the contrary, when you don't allocate memory, new variables are stored in the STACK section.
See What and where are the stack and heap?
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 6 years ago.
Improve this question
First off, I am well aware of the concepts behind using typedef on both names and anonymous structs. My question is how a C library should expose its types.
Typically, I would simply typedef an anonymous struct, or a named struct when I need forward declaration. However, this is an issue if a library with only typedefs is included in a project that prefers using the struct qualifier, which is no longer possible. Similarly, having both a typedef and a named struct may still pollute the general namespace for such projects.
Should a library, then, only provide named structures and leave typedefing to the user?
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
What's the difference between generic pointer and void pointer?
void* gp;
No difference. void pointer is itself called generic pointer.
If you declare a pointer to void it's called Generic Pointer since you have to cast it to another kind of pointer first. Check the dictionary definition of generic. Both are the same, but this is just a 'description' of 'void pointer'.
This might seem useless but it has a very important use: you can use it multiple times to point at data of different types (int char etc..)
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 8 years ago.
Improve this question
I have a pointer to an wide char array, which I want to free.
What is the easiest way to do this?
That depends on how the memory was allocated.
If malloc() was used, pass the address of the allocated memory to free(). If it came from a third-party library, see that library's documentation. If the struct is actually a local or global variable, don't worry about cleaning it up at all.