Delete struct written with memcpy [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have an assignment on memory blocks. We have a struct Record that I need to save in these memory blocks. Each block holds 5 Records, which i memcpy one after another in a list of blocks and an int CountOfRecords.
We also have a DeleteRecord function that deletes (duh) a particular record from the memory block. Now, other than reducing the Count and shifting all next Records forward as to practically delete that Record, is there any way to ACTUALLY delete what is written with memcpy? Like writing something like a NULL as a struct instance? Memmove does not seem to offer such an application.
EDIT: I write the records as such
//block is the pointer to block,int is for the Count, and record is placed
memcpy(block+sizeof(int)+sizeof(Record),&record,sizeof(Record));

You basically don't want to move data around but instead set the memory to zero, how about memset?
memset(block+sizeof(int)+sizeof(Record), 0, sizeof(Record));
But you somehow have to remember, that the record at this point is zeroed out (not used), best by some property.

Related

Finding unused memory in process memory [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 5 years ago.
Improve this question
I'm looking for a reliable way to find unused memory in a C program's process since I need to "inject" some data into somewhere without it corrupting anything.
Whenever I find an area with only zeros in it, that's a good sign. However, no guarantees: It can still crash. All the non-zero memory is most likely being used for sure so it cannot be overwritten reliably (most memory has some kind of data in it).
I understand that you can't really know (without having the application's source code for instance) but are there any heuristics that make sense such as choosing certain segments or memory looking a certain way? Since the data can be 200KB this is rather large and finding an appropriate address range can be difficult/tedious.
Allocating memory via OS functions doesn't work in this context.
Without deep knowledge of a remote process you cannot know that any memory that is actually allocated to that process is 'unused'.
Just finding writable memory (regardless of current contents) is asking to crash the process or worse.
Asking the OS to allocate some more memory in the other process is the way to go, that way you know the memory is not used by the process and the process won't receive that address through an allocation of its own.

C: How to read variables from a file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
What is the best way to create a function that reads each part of a file like described in the following picture and saves it to arrays and integers, it must read and save the second part (above word_count): word; orientation; row; col; points; jogador until a number(Turn) is read.
Start by creating 3 struct's. One for people. One for words and one to aggregate the full structure. For the third, you will need to decide which arrays can be sized at compile time and which need to use malloc or calloc to allocate space for the people or word structures.
Next write a function to populate a person from a one line string and one to populate a word from a one line string. You could use strchr to find the semicolons or for less error durability you might look at sscanf.
Finally write your loading function to read the file line by line detecting 'mode' changes by (strchr(line, ';') == -1), and calling the appropriate convert function. You can then return the aggregate structure as a pointer to a malloc'ed struct.
Don't forget to write a function, that takes that pointer, to dispose of everything you malloc'ed so that a caller does not need to know your allocation details and can just say "get me one from that file" followed by "throw this away".
Unfortunately, C is unlike Java or C# in that the heavy lifting is not built in or covered by copious included libraries. You need to find libraries or write low level code yourself.
Good luck with you project.

What are the advantages of using an array of ints vs allocating memory on the heap with malloc? [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 6 years ago.
Improve this question
I don't know if the question makes any sense but I'm new to C and concepts like the Heap so its a bit overwhelming for me right now. I've been reading a few articles about memory allocation using malloc() vs declaring variables That I need to know the amount of data I need in advance when I want to work with variables while with malloc() I don't.
I can allocate data at runtime as much as I need with malloc but how? Lets say I want to input temperature records of 100 consecutive days so I did something
like
int* temps=malloc(100*sizeof(int)).
Now while I was inputting data I realise that I needed to enter 110 days of records. How do I go about adding the additional data at runtime? Since I just allocated space for 100 ints. What difference would it have made if had I done int temps[100], I know I had to initialize the array again changing the int temps[100] to int temps[110] and then recompile the program again and starting the input all over again.
Now while I was inputting data I realise that I needed to enter 110
days of records. How do I go about adding the additional data at
runtime?
Use realloc.
What difference would it have made if had I done int temps[100]
You can't change this size anymore, it is fixed. You have allocated array of 100 integers, and you are done. While with above approach, you still can "resize" the array during run time.
Though note sometimes doing something like int x[100] can be fine depending on your situation, plus it saves you from memory management related issues. That said, use dynamic memory such as malloc, only when it is necessary (exactly because to avoid complications related to memory management).

C protecting against heap overflow [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 8 years ago.
Improve this question
Consider the following function that is supposed to validate passwords:
char *systemkey = ...... ;
int validate(char* key) {
char* k = malloc(16);
char* sk = malloc(16);
strcpy(sk,systemkey);
strcpy(k,key);
return (strncmp(k,sk,16) ==0);
}
If k and sk are allocated consecutively, that it's easy to break the function by supplying 2 identical blocks of 16 bytes each.
If I'm the one writing the compiler/malloc/free/OS, is there any way I can identify MOST of these type of hacks and prevent them?
EDIT
One possible solution is to put some sort of canary word between each two different allocations. Is there another way?
The best you can hope in use by and operating system is an implementation of malloc that randomizes the memory it returns. It doesn't prevent overflows, but makes exploiting them much harder. For large allocations a technique being used is to return the allocation aligned to the end of the page and leaving the next page explicitly unmapped as a guard page.
You can read a little bit on this page and a the links from it to see how OpenBSD implements malloc protection. As far as I'm aware this is the best you can get from a malloc in an operating system in wide use.
If I'm the one writing the compiler/malloc/free/OS, is there any way I can identify MOST of these type of hacks and prevent them?
One possibility is to use a “sound” static analyzer that, if used properly, can guarantee that your program does not access any invalid pointer for any execution. Here is one.
Another is to use dynamic instrumentation. Valgrind is an example of this approach.

c - Why do I get this SIGSEGV? How do I know how much memory a struct can get? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I pasted my code here: http://pastebin.com/tPvRHrkW
Compiled with gcc.
It seems the error occurred because I defined a struct too big for the compiler. I took the struct out into another single source file and tested it, no error occurred this time. So why am I getting SIGSEGV and is there any limit on the size of a struct?
There isn't a limit to the size of the struct, the problem is with how you're using it. MGraph is the huge structure type, and in two places you're using it in a manner that places it on the stack; once as a parameter to a function and again as a local variable. Stack space is often not something that is permitted to grow to huge proportions.
I would suggest two changes. First, use dynamic allocation for instances of this type. Second, pass pointers as parameters to it, rather than the actual data.
Generally you're only limited by available memory and the addressing capabilities of your system. However in your case you're declaring a local variable, which will be allocated on the stack. The stack is likely much more limited in capacity.
#define MAXV 20000 .. int edges[MAXV][MAXV];
is 20000 * 20000 * 4 ~ 1.5 Gigs of memory on stack.
You should probably use malloc & dynamically allocate instead.

Resources