Cannot assign values to a struct pointer, after pointer was successfully assigned - c

I am writing an early kernel heap to manage some memory allocations before the final heap is operational. When I assign an address(not used by anything else) to my struct pointer it works, the problem comes when I try to assign values to the struct members.
Before you answer, take in mind that I do not have any kind of C library which I can use. Debugging was done with QEMU and gdb through remote.
//The struct
typedef struct mem_block{
mblock_t *prev;
boolean used;
size_t size;
mblock_t *next;
} mblock_t;
//The file-local pointer(before its init)
mblock_t *eheap=NULL;
//Function that assigns the values
mblock_t *init_early_heap(address_t start, size_t size){
if(eheap==NULL){
if(size < sizeof(mblock_t)){
PANIC("Available memory below EHEAP requirement");
}
eheap = (mblock_t*)HEAP_START_ADDRESS;
eheap->prev = NULL;
eheap->next = NULL;
eheap->size = (size_t)(size - sizeof(mblock_t));
eheap->used = false;
print("\nCreated Early Heap at ");
print_hex_long(eheap);
print("\nSize in bytes: ");
print(itos(eheap->size));NL;
}
return eheap;
}
After the function returns, I get a pointer pointing at address 0xC0000000 and untouched members as eheap->size is 0(size parameter in the function is NOT 0).

Problem found: Cannot read whole address space (A20 on) in QEMU, works correctly with bit lower addresses.
The C code above is correct, now the problem remains with QEMU/GRUB.

Related

Issue with pointer arithmetic in my own malloc() function

I am currently working on implementing my own malloc() function. The one thing that seems to not be working is that I'm unable to correctly return the memory address of the beginning of my current block. My block struct looks like this and is of size 16:
typedef struct block {
size_t size;
struct block* next;
struct block* prev;
int free;
} block_t;
My malloc looks like this currently:
void *bmalloc(size_t size)
{
void * mem=0;
size_t alloc=size;
if (freelist==0)
{
freelist=&heap[0];
freelist->free=0;
freelist->prev=0;
freelist->size=MAX_HEAP_SIZE;
freelist->next=0;
//printf("is this happening?");
}
for (curr=freelist;curr!=NULL;curr=curr->next)
{
if (alloc<=curr->size && curr->free==0)
{
block_t *tmp=curr->next;
curr->free=1;
curr>size=MAX_HEAP_SIZE;
curr>next=curr+alloc+sizeof(block_t);
//curr->next->next=tmp;
curr->next->size=curr->size-alloc;
curr->next->free=0;
curr->next->prev=curr;
mem=curr+sizeof(block_t)
return mem;
}
}
}
Curr and Freelist are both block_t structs. I know the issue must lie in the step where I set mem=curr+sizeof(block_t), but I'm not really sure how to fix it. Upon some checking I noticed that the first allocation of memory returns an address 276 bytes away from the beginning of the array the blocks live on top of, and after the first allocation the blocks memory addresses are 512 bytes away.
Whenever you do pointer arithmetic, the values are always scaled by the size of the type the pointer points at. So when you do
mem = cur + sizeof(block_t);
since cur is a block_t *, the addition is scaled by sizeof(block_t) automatically. Which means that if sizeof(block_t) is 16, this will add 256 bytes to the pointer, which is not what you want. You probably just want
mem = cur + 1;
here, and similar changes elsewhere.

double linked list in c send data from function

This is my code:-
typedef struct Frame
{
char* name;
unsigned int duration;
char* path; // may need to scan (with fgets)
}frame_t;
typedef struct Link
{
frame_t* frame;
struct Link* next;
}link_t;
void addNewFrame(void)
{
link_t* newLink = (link_t**)malloc(sizeof(link_t*));
printf(" *** Creating new frame ***\n\n");
printf("Please insert frame path:\n");
// newLink->frame->name = (char*)malloc(sizeof(char) * MAX_LEN);
fgets(newLink->frame->name, MAX_LEN,stdin);
printf("%s", newLink->frame->name);
}
I just need to add a data to name variable in the "Frame" link list, please help me by reviewing this code.
You want to allocate the right types here:-
link_t* newLink = malloc(sizeof(link_t)); //Pointer to Link st
if(newLink){
newLink->frame = malloc(sizeof(frame_t)); //Pointer to frame member
if(newLink->frame){
newLink->frame->name = malloc(sizeof(char) * MAX_LEN); //Pointer to name member
if(newLink->frame->name){
//Rest of your code
}
}
}
EDIT:-
1. As pointed out in comments there's no need to cast the pointer returned by malloc()
2. Another very imp point you may want to check validity of the pointers before de-referencing them
First. You don't need to cast void * so (link_t **)malloc(... can be only malloc(....
Second. You allocated enough memory for a pointer not for a struct. I think you mean malloc(sizeof(link_t)) or even better malloc(sizeof(*newLink))
Third newLink->frame is a pointer so you need to allocate data for it too, newLink->frame = malloc(sizeof(frame_t))
Fourth newLink->frame->name is still a pointer so you need to allocate data for it too. newLink->frame->name = malloc(MAX_LEN)
The confusion that you are doing is pretty common. When you say type *something you are allocating a pointer to type in the stack. A pointer needs to point to somewhere else or NULL, or bad things happen. This applies to structures too. If your structure has a pointer member you need to point it to somewhere else. The somewhere else is where the real "type" object resides.
This also applies to arrays. If you say 'int foo[10]' you are allocating ten integers in the stack. If you say int *foo[10] you are allocating ten pointers in the stack. If you say int **foo you are allocating one pointer in the stack. Again all pointers need to be initialized, I mean, they need to point to some valid object, allocated somewhere else in the memory.
I hope this helps.
Some other points.
Always check pointer coming from malloc, if allocation failed you'll receive NULL. Dereferencing NULL will break your program.
Always initialize memory coming from malloc, fill it with zeros or something.
Don't use _t suffix, is POSIX reserved.
I didn't test any of this.

An unexpected strcpy segfault

I've discovered a segfault that I'm having trouble parsing. Lest you think I haven't searched, I don't think the issue is the same as in this question. I have the following typedef'd structure:
typedef struct usage usage;
struct usage{
char name[9];
int loc;
usage *next;
};
I'm reading data from a file that consists of a number K followed by K pairs (S,D) where S= an 8-character string [this is a variable name] and d= an integer [a memory location].
Here's the code that's causing the error:
void addUse(int index,char *nm, int addr){
usage *temp;
strcpy(temp->name,nm); //segfault here.
temp->loc = addr;
temp->next= NULL;
/* more processing */
}
To make this clearer, I am calling this function from a block where I have
int dummyIndex = 1;
char s1[9];
int val1;
scanf(" %s %d, s1, &val1);
addUse(dummyIndex, s1, val1);
It seems like in the question I linked to the issue is that they do not allocation the char on the heap. I am not sure what's happening here. Using identical calls to strcpy on another struct with a field char name[9] works just fine.
What am I missing? What have I over looked?
Thanks in advance!
You've forgotten to initialize the temp pointer, so it's pointing to random memory. When you then write
strcpy(temp->name, mm);
You're following a pointer to a random address and writing bytes there, hence the segfault.
The problem is in the following line of your code
usage *temp;
strcpy(temp->name,nm); //segfault here.
You are not initializing the temp pointer so it is taking a garbage value.
Try initializing it before strcpy
usage *temp = malloc(sizeof(usage));
Hope this helps.
Here's my issue:
in other calls I make sure to set temp = malloc(sizeof(struct)); not in this one so I'm trying to write to memory that hasn't been allocated!.
The problem with your code is clear, and the SegFault happens in the exact location where it should. The reason is that you are using memory you haven't previously allocated.
void addUse(int index,char *nm, int addr){
usage *temp;
strcpy(temp->name,nm); //segfault here.
// ...
}
When you declare "usage *temp;" you're creating a pointer temp of type usage. Terrific. But... where is the pointer pointing to? You don't know. The value of temp, the memory address it contains is the garbage that happens to be in the place it occupies in memory when it is created (other programming languages give a default value to uninitialized variables, but C is not in that group).
So... you're being lucky. Your program is best described as "undefined behaviour", since it is possible that temp contains a memory address that casually is unoccupied: your program could run pass this function, crash elsewhere, and you would not have a clue about why.
Another problem is that you should return the new struct usage, while your function is returning void.
This is the function as it should be:
usage * addUse(int index,char *nm, int addr){
usage *temp = malloc(sizeof(usage));
if ( temp == NULL) {
fprintf(stderr, "Not enough memory");
exit(EXIT_FAILURE);
}
strcpy(temp->name,nm); //segfault here.
temp->loc = addr;
temp->next= NULL;
/* more processing */
return temp;
}
I understand that you should link the pointer returned by addUse() to a linked list.
Hope this helps.

C generic linked-list

I have a generic linked-list that holds data of type void* I am trying to populate my list with type struct employee, eventually I would like to destruct the object struct employee as well.
Consider this generic linked-list header file (i have tested it with type char*):
struct accListNode //the nodes of a linked-list for any data type
{
void *data; //generic pointer to any data type
struct accListNode *next; //the next node in the list
};
struct accList //a linked-list consisting of accListNodes
{
struct accListNode *head;
struct accListNode *tail;
int size;
};
void accList_allocate(struct accList *theList); //allocate the accList and set to NULL
void appendToEnd(void *data, struct accList *theList); //append data to the end of the accList
void removeData(void *data, struct accList *theList); //removes data from accList
--------------------------------------------------------------------------------------
Consider the employee structure
struct employee
{
char name[20];
float wageRate;
}
Now consider this sample testcase that will be called from main():
void test2()
{
struct accList secondList;
struct employee *emp = Malloc(sizeof(struct employee));
emp->name = "Dan";
emp->wageRate =.5;
struct employee *emp2 = Malloc(sizeof(struct employee));
emp2->name = "Stan";
emp2->wageRate = .3;
accList_allocate(&secondList);
appendToEnd(emp, &secondList);
appendToEnd(emp2, &secondList);
printf("Employee: %s\n", ((struct employee*)secondList.head->data)->name); //cast to type struct employee
printf("Employee2: %s\n", ((struct employee*)secondList.tail->data)->name);
}
Why does the answer that I posted below solve my problem? I believe it has something to do with pointers and memory allocation. The function Malloc() that i use is a custom malloc that checks for NULL being returned.
Here is a link to my entire generic linked list implementation: https://codereview.stackexchange.com/questions/13007/c-linked-list-implementation
The problem is this accList_allocate() and your use of it.
struct accList secondList;
accList_allocate(&secondList);
In the original test2() secondList is memory on the stack. &secondList is a pointer to that memory. When you call accList_allocate() a copy of the pointer is passed in pointing at the stack memory. Malloc() then returns a chunk of memory and assigns it to the copy of the pointer, not the original secondList.
Coming back out, secondList is still pointing at uninitialised memory on the stack so the call to appendToEnd() fails.
The same happens with the answer except secondList just happens to be free of junk. Possibly by chance, possibly by design of the compiler. Either way it is not something you should rely on.
Either:
struct accList *secondList = NULL;
accList_allocate(&secondList);
And change accList_allocate()
accList_allocate(struct accList **theList) {
*theList = Malloc(sizeof(struct accList));
(*theList)->head = NULL;
(*theList)->tail = NULL;
(*theList)->size = 0;
}
OR
struct accList secondList;
accList_initialise(secondList);
With accList_allocate() changed to accList_initialise() because it does not allocate
accList_initialise(struct accList *theList) {
theList->head = NULL;
theList->tail = NULL;
theList->size = 0;
}
I think that your problem is this:
You've allocated secondList on the stack in your original test2 function.
The stack memory is probably dirty, so secondList requires initialization
Your accList_allocate function takes a pointer to the list, but then overwrites it with the Malloc call. This means that the pointer you passed in is never initialized.
When test2 tries to run, it hits a bad pointer (because the memory isn't initialized).
The reason that it works when you allocate it in main is that your C compiler probably zeros the stack when the program starts. When main allocates a variable on the stack, that allocation is persistent (until the program ends), so secondList is actually, and accidentally, properly initialized when you allocate it in main.
Your current accList_allocate doesn't actually initialize the pointer that's been passed in, and the rest of your code will never see the pointer that it allocates with Malloc. To solve your problem, I would create a new function: accList_initialize whose only job is to initialize the list:
void accList_initialize(struct accList* theList)
{
// NO malloc
theList->head = NULL;
theList->tail = NULL;
theList->size = 0;
}
Use this, instead of accList_allocate in your original test2 function. If you really want to allocate the list on the heap, then you should do so (and not mix it with a struct allocated on the stack). Have accList_allocate return a pointer to the allocated structure:
struct accList* accList_allocate(void)
{
struct accList* theList = Malloc( sizeof(struct accList) );
accList_initialize(theList);
return theList;
}
Two things I see wrong here based on the original code, in the above question,
What you've seen is undefined behaviour and arose from that is the bus error message as you were assigning a string literal to the variable, when in fact you should have been using the strcpy function, you've edited your original code accordinly so.. something to keep in mind in the future :)
The usage of the word Malloc is going to cause confusion, especially in peer-review, the reviewers are going to have a brain fart and say "whoa, what's this, should that not be malloc?" and very likely raise it up. (Basically, do not call custom functions that have similar sounding names as the C standard library functions)
You're not checking for the NULL, what if your souped up version of Malloc failed then emp is going to be NULL! Always check it no matter how trivial or your thinking is "Ah sher the platform has heaps of memory on it, 4GB RAM no problem, will not bother to check for NULL"
Have a look at this question posted elsewhere to explain what is a bus error.
Edit: Using linked list structures, in how the parameters in the function is called is crucial to the understanding of it. Notice the usage of &, meaning take the address of the variable that points to the linked list structure, and passing it by reference, not passing by value which is a copy of the variable. This same rule applies to usage of pointers also in general :)
You've got the parameters slightly out of place in the first code in your question, if you were using double-pointers in the parameter list then yes, using &secondList would have worked.
It may depend on how your Employee structure is designed, but you should note that
strcpy(emp->name, "Dan");
and
emp->name = "Dan";
function differently. In particular, the latter is a likely source of bus errors because you generally cannot write to string literals in this way. Especially if your code has something like
name = "NONE"
or the like.
EDIT: Okay, so with the design of the employee struct, the problem is this:
You can't assign to arrays. The C Standard includes a list of modifiable lvalues and arrays are not one of them.
char name[20];
name = "JAMES" //illegal
strcpy is fine - it just goes to the memory address dereferenced by name[0] and copies "JAMES\0" into the memory there, one byte at a time.

whats wrong in this program?

Am unable to run this code...
#include<cstdio>
int main()
{
struct a{
int b;
struct a *next;
};
typedef struct a no;
no *n;
n->b = 12;
n->next = NULL;
n->next->b = 12;
n->next->next = NULL;
printf("%d %d", n->b, n->next->b);
getchar();
return 0;
}
When you say:
no *n;
you get an uninitialised pointer. When you use that pointer, you get undefined behaviour.
You allocated space for a pointer to a structure, but you didn't allocate space for the actual structure. This means that you don't have a memory address for the structure you are using.
In addition, the pointer points to some random memory address because you didn't initialize it. As a result, you could be trying to read and write to memory that doesn't belong to you, which can cause your program or even your system to crash because of the undefined behavior that results.
As #Neil Butterworth said, you get an uninitialised pointer. This mean that this pointer could point to anywhere, thus giving an access violation error. The way to fix this is simple, just call malloc() before using that pointer. malloc() gives that pointer a valid and usable address, so no one will complain about that.
You're declaring a struct INSIDE a function.
Declare the struct OUTSIDE of the function.
The typedef should be declared outside the function too.
#include<cstdio>
struct a{
int b;
struct a *next;
};
typedef struct a no;
int main()
{
///... your code...
}
try something like this:
no *n = (no*)malloc(sizeof(no));
#include <cstdio>
/* declaring the struct in the function definition may be possible (I'm not sure,
actually, haha). Unless you have a GOOD reason, it's good practice to declare
structs, globals, typedefs, etc... outside the function */
typedef struct a{
int b;
struct a *next;
} no;
int main()
{
no *n;
/* Here, you have a pointer. Remember these are simply (generally) 32-bit values
defined in your stack space used to store a memory location which points to your
ACTUAL struct a! Depending on what USED to be in the stack space, this could
point ANYWHERE in memory, but usually you will find that it points to the NULL
memory location, which is just address "0". To get this to point to something,
you have to allocate empty space on your heap to store your struct... */
n = malloc(sizeof(no));
/* Now your pointer n points to an allocated 'struct a', and you can use it like
normal */
n->b = 12;
n->next = NULL;
/* You just set n->next, which is another 'no' pointer, to NULL. This means that
n->next points nowhere. So, just like above you have to malloc another instance
of the struct! */
n->next = malloc(sizeof(no));
/* NOW you can use n->next with no ill effects! */
n->next->b = 12;
n->next->next = NULL;
printf("%d %d", n->b, n->next->b);
getchar();
/* After you're done with your structs, you want to free them using the POINTERS
that reference them */
free(n->next);
free(n);
return 0;
}

Resources