|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
Question I thought was clever on an old exam:
Implement the function free_array with prototype shown below that frees the dynamically-allocated memory associated with each array element in the first parameter passed to the function. The function needs to handle the scenario where two or more array entries are pointing to the same dynamically-allocated memory (only one free can be applied to a memory location). It is OK to modify the original array as part of your implementation, but you MAY NOT define a new array in the function and you may not allocate more memory. If you do you will lose a significant number of points.
void free_array(void *a[], int length) {
kinda a hard question since based on the exam length we are supposed to be able to write it in about 1 minute, lol
curious how many of you could get this in less than a minute
|
On April 20 2017 05:32 travis wrote:Question I thought was clever on an old exam: Implement the function free_array with prototype shown below that frees the dynamically-allocated memory associated with each array element in the first parameter passed to the function. The function needs to handle the scenario where two or more array entries are pointing to the same dynamically-allocated memory (only one free can be applied to a memory location). It is OK to modify the original array as part of your implementation, but you MAY NOT define a new array in the function and you may not allocate more memory. If you do you will lose a significant number of points. void free_array(void *a[], int length) {
kinda a hard question since based on the exam length we are supposed to be able to write it in about 1 minute, lol curious how many of you could get this in less than a minute
I don't do C manual memory management, but my guess is:
1. for loop 2. Check if null. a. if not, free element and then set it to null b. if null, go to next one
|
nope won't work you will potentially free the same memory 2 or more times
|
I don't see how it'll be freed more than once. It's set to null once you free 1 time. This isn't rocket science.
free(stuff); stuff = NULL;
Next time:
if (stuff) repeat
|
int *p, *j; p = malloc(2); j = p; ourfunction(array with p and j in it);
ourfunction(*array[]) { for(x = 0; x < total elements; x++) { if(array[x] != null) { free(array[x]); array[x] = null; } } }
j will not be null because it points to our malloc. j gets freed, then set to null p will not be null because it points to our malloc, our malloc will be freed a 2nd time (in error)
|
On April 20 2017 05:32 travis wrote: curious how many of you could get this in less than a minute
You either know how to do it right off the bat or you don't. If you do, you can do almost anything in a minute...
Also, won't the other pointer automatically be dereferenced when the memory it's pointing to goes away?
ie:
p & j are pointers to the same memory.
free(p); p = NULL; free(j); // nothing happens here since j is NULL at this point.
Essentially, what you could do is this:
ourfunction(*array[]) { for (x = 0; x < total elements; x++) { free(array[x]); // no need to check if array[x] is null since free doesn't care array[x] = NULL; } }
|
Same stuff with C++ delete, I just didn't want to assume free is smart. Either way, my suggestion should work.
The key is in what is mentioned above:
free(j); // nothing happens here since j is NULL at this point.
|
but j is not null at that point, j is still a pointer to the section of memory that was freed.
p points to null, j contains the address it was assigned when we did j = p
also the above example is bad too. you don't want to free a bunch of memory that was never allocated you'll get seg faults or worse
maybe you guys are referencing behavior of free in c++ ?
|
I see what you mean. Well, it seems you can't do much about it.
http://stackoverflow.com/a/8300866
Why would you end up with 2 different pointers pointing to the same thing? I don't remember doing this at work. Universities with dumb examples again.
|
I can't imagine why you would but this is the sort of crap they test us on, LOL
|
Russian Federation4235 Posts
On April 20 2017 06:47 travis wrote: but j is not null at that point, j is still a pointer to the section of memory that was freed.
p points to null, j contains the address it was assigned when we did j = p
also the above example is bad too. you don't want to free a bunch of memory that was never allocated you'll get seg faults or worse
maybe you guys are referencing behavior of free in c++ ?
The first thing that comes to mind without allocating memory would be casting the input to an array of ints, sorting it and just using continue if a[i] == a[i - 1].
Double free is a dangerous error and should be avoided at all costs. Furthermore, you can't tell if a pointer has been freed just by looking at it, otherwise, C programming would have been really easy. Freeing a nullptr is generally safe, but you have to set it to nullptr first.
|
Yes, it's stupid when people at universities decide to test meaningless, corner case which won't be used in real environment. Either way, my C experience is limited. I work with C++ which is very far away from C raw pointers nowadays.
|
Russian Federation4235 Posts
On April 20 2017 06:56 Shield wrote: Yes, it's stupid when people at universities decide to test meaningless, corner case which won't be used in real environment. Either way, my C experience is limited. I work with C++ which is very far away from C raw pointers nowadays. Well, a situation with an array of possibly aliasing pointers is possible in C++ too, albeit not very probable, I see no harm in putting that into test. You can alleviate aliasing pointers with shared_ptr, but that comes at a price you sometimes don't want to pay.
|
On April 20 2017 06:55 BluzMan wrote:Show nested quote +On April 20 2017 06:47 travis wrote: but j is not null at that point, j is still a pointer to the section of memory that was freed.
p points to null, j contains the address it was assigned when we did j = p
also the above example is bad too. you don't want to free a bunch of memory that was never allocated you'll get seg faults or worse
maybe you guys are referencing behavior of free in c++ ? The first thing that comes to mind without allocating memory would be casting the input to an array of ints, sorting it and just using continue if a[i] == a[i - 1]. Double free is a dangerous error and should be avoided at all costs. Furthermore, you can't tell if a pointer has been freed just by looking at it, otherwise, C programming would have been really easy. Freeing a nullptr is generally safe, but you have to set it to nullptr first.
well that's a way cooler solution than the given solution, I guess technically yours is faster than the given solution too
|
|
On April 20 2017 07:25 travis wrote:Show nested quote +On April 20 2017 06:55 BluzMan wrote:On April 20 2017 06:47 travis wrote: but j is not null at that point, j is still a pointer to the section of memory that was freed.
p points to null, j contains the address it was assigned when we did j = p
also the above example is bad too. you don't want to free a bunch of memory that was never allocated you'll get seg faults or worse
maybe you guys are referencing behavior of free in c++ ? The first thing that comes to mind without allocating memory would be casting the input to an array of ints, sorting it and just using continue if a[i] == a[i - 1]. Double free is a dangerous error and should be avoided at all costs. Furthermore, you can't tell if a pointer has been freed just by looking at it, otherwise, C programming would have been really easy. Freeing a nullptr is generally safe, but you have to set it to nullptr first. well that's a way cooler solution than the given solution, I guess technically yours is faster than the given solution too
This is indeed nice...
#include <stdio.h> #include <stdlib.h>
int compare(const void *a, const void *b);
int main(void) { int i = 0; int len = 2; int **ary = malloc(sizeof(int) * len); int *p = malloc(sizeof(int));; int *j = p;
ary[0] = p; ary[1] = j;
qsort(ary, len, sizeof(int), compare);
for (i; i < len; i++) { if (i > 0 && ary[i] != ary[i - 1]) { free(ary[i]); } }
free(ary); printf("We are free!\n");
return 0; }
int compare(const void *a, const void *b) { return (*(int*)a - *(int*)b); }
|
On April 20 2017 07:40 Nesserev wrote: So, I would like to check something with you guys. Today I installed an external harddrive for my father for daily automatic back-ups with the goal of keeping certain data safe from cryptolocker and such; however, I'm not sure whether or not I missed something, and I'm not a windows user :/.
The original setup was simple: - 1 account called 'daddy69' with admin rights and password (not really 'daddy69', but whatever) - everyone has access rights to external harddrive
After I was done, the setup was as follows: - 1 account called 'backup' with admin rights and password, access to external harddrive and daddy69's directories - 1 account called 'daddy69' with a password and no admin rights, can only detect but not access external harddrive - only admins have access rights to external harddrive
I wrote a batchfile that automates the whole process of backing up certain directories to a fresh directory on the external drive. Then I added an automated task to run that script once every day when he's supposed to be sleeping. It seems to be working fine.
Anything I should change, or any recommendations? Did I miss anything, and did I reach my goals? Is this setup robust enough to survive cryptolocker or ... ?
http://lifehacker.com/how-to-back-up-your-computer-automatically-with-windows-1762867473
|
**p; *k;
statement 1: p = &k; statement 2: *p = k;
both of these statements are the same?
|
Been a while since I programmed in C, but I doubt the statement 2 is valid syntax.
|
|
|
|
|