|
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. |
From abductedonut's post:
Ah, and just for some light reading... Here's a post by Linus Torvalds... the creator of the Linux operating system. I think this is hilariously interesting!
Actually, the "operating system" you're talking about is called GNU. Linux is the kernel.
While I'm at it, here's a link to a talk by the initiator of the GNU operating system: + Show Spoiler +
|
shifting bits and comparing bit per bit is really useful when you work with hardware registers, like in micopocessors in machinery, because often how and what they do is determined by what bits are set to 1 or 0.
|
|
On February 02 2015 20:27 Dagobert wrote:While I'm at it, here's a link to a talk by the initiator of the GNU operating system: + Show Spoiler +https://www.youtube.com/watch?v=Ag1AKIl_2GM "when you bring cookies to school you can't keep them to yourself, you've got to share them" - in a talk about freedom. Not that I dislike the idea, but he really doesn't make a lot of compelling arguments.
|
On February 02 2015 23:34 Nesserev wrote:Show nested quote +On January 31 2015 13:40 Nesserev wrote:Poll: Vote on the book that you would like to read:Facts and Fallacies of Software Engineering (0) 0% Coders at Work (0) 0% Programming Pearls (2) 22% Code Complete (7) 78% 9 total votes Your vote: Vote on the book that you would like to read: (Vote): Facts and Fallacies of Software Engineering (Vote): Coders at Work (Vote): Programming Pearls (Vote): Code Complete
Well, seems like the first book will be Code Complete. With 960 pages, Code Complete is kind of a beast to conquer, which is both terrifying and exciting. It's rather cheap on amazon (if you're used to academic prices), and it's such a popular book that you can probably get it at your campus library. Well, I don't think anyone will have any problems getting their hands on a copy. I wonder if most people will be able to finish it in only one month  I have that book and have been chipping away at it since summer a bit at a time. It is a dense read to say the least.
|
Hey guys, I have a question NOT regarding an assignment this time. Nor it is a segfault :D So, I was there doing a realloc function for some int * arrays, and I wondered, would it be possible to do that for any type of array, with the same function.
# define TAB_TYPE char
TAB_TYPE **tab_realloc(TAB_TYPE **old_tab, int old_size, int new_size) { TAB_TYPE **res; int i;
if (!(res = (TAB_TYPE **)ft_memalloc(sizeof(TAB_TYPE *) * new_size))) return (NULL); i = 0; while (i < new_size) { if (i < old_size) { res[i] = old_tab[i]; ft_memdel((void **)&old_tab[i]); } else res[i] = NULL; i++; } ft_memdel((void **)old_tab); return (res); }
This is where I got, and it doesn't quite work. No problem if TAB_TYPE is an int, but for char some of the data is copied and then disappear for some reason. I would be interested in the reason, but that's not what I'm after. Is something like this even possible ? One would just define what type of data the arrays are made of, and the function does it work properly no matter said type ?
|
1. res[i] = old_tab[i] says set the res[i] to be the address to a TAB_TYPE that old_tab[i] points to. 2. ft_memdel((void **)&old_tab[i]) says free the memory at the address specified by old_tab[i].
that means res[i] now points (is an address) to memory that has been freed, and calls to res[i][x]* have undefined behavior.
*i think
|
Are you just trying to copy the contents of one array to another? Sounds like a job for memcpy.
res = malloc(sizeof(void*) * new_size); memset(res, 0, sizeof(void*) * new_size); if (new_size < old_size) memcpy(res, old_tab, sizeof(void*) * new_size); else memcpy(res, old_tab, sizeof(void*) * old_size); free(old_tab); return res;
|
On February 03 2015 15:03 meatpudding wrote: Are you just trying to copy the contents of one array to another? Sounds like a job for memcpy.
You called?
|
On February 03 2015 15:37 memcpy wrote:Show nested quote +On February 03 2015 15:03 meatpudding wrote: Are you just trying to copy the contents of one array to another? Sounds like a job for memcpy.
You called?
Haha!
On February 02 2015 23:34 Nesserev wrote:Show nested quote +On January 31 2015 13:40 Nesserev wrote:Poll: Vote on the book that you would like to read:Facts and Fallacies of Software Engineering (0) 0% Coders at Work (0) 0% Programming Pearls (2) 22% Code Complete (7) 78% 9 total votes Your vote: Vote on the book that you would like to read: (Vote): Facts and Fallacies of Software Engineering (Vote): Coders at Work (Vote): Programming Pearls (Vote): Code Complete
Well, seems like the first book will be Code Complete. With 960 pages, Code Complete is kind of a beast to conquer, which is both terrifying and exciting. It's rather cheap on amazon (if you're used to academic prices), and it's such a popular book that you can probably get it at your campus library. Well, I don't think anyone will have any problems getting their hands on a copy. I wonder if most people will be able to finish it in only one month 
There's no problem in having it for more than one month, it definitely is worth it. Also, it's the best thing Microsoft ever released.
|
On February 03 2015 15:37 memcpy wrote:Show nested quote +On February 03 2015 15:03 meatpudding wrote: Are you just trying to copy the contents of one array to another? Sounds like a job for memcpy.
You called?
Ahahahaha !!
[I have nothing else to contribute at this time]
|
On February 03 2015 15:03 meatpudding wrote:Are you just trying to copy the contents of one array to another? Sounds like a job for memcpy. res = malloc(sizeof(void*) * new_size); memset(res, 0, sizeof(void*) * new_size); if (new_size < old_size) memcpy(res, old_tab, sizeof(void*) * new_size); else memcpy(res, old_tab, sizeof(void*) * old_size); free(old_tab); return res;
Not quite. Let's say I have an array containing 5 arrays of int. I want to expand it to 6 arrays, so I figured I have to "realloc" it. That's the part I need for my assignment, but not what I was asking about. I'm wondering if one could write a function that would work whatever the type of array of array is, int, char... C wiki page says you can't have an array of void, so I thought this was not an option, hence why I used a macro TAB_TYPE (for which ARRAY_TYPE would be a better name I guess) that the user defines and that lets the function knows what type of data it's working with.
Dunno if that's clear...
|
I haven't done this in a while, but I'm curious.
My guess is: If you're copying the pointers and not the actual content, the type should not matter. Since you're basically only copying addresses.
[edit] But if you resize the array to be smaller than the previous one, you'll have leaks?
|
On February 03 2015 15:37 memcpy wrote:Show nested quote +On February 03 2015 15:03 meatpudding wrote: Are you just trying to copy the contents of one array to another? Sounds like a job for memcpy.
You called? That's pretty funny ;D
|
On February 03 2015 20:21 Cynry wrote:Show nested quote +On February 03 2015 15:03 meatpudding wrote:Are you just trying to copy the contents of one array to another? Sounds like a job for memcpy. res = malloc(sizeof(void*) * new_size); memset(res, 0, sizeof(void*) * new_size); if (new_size < old_size) memcpy(res, old_tab, sizeof(void*) * new_size); else memcpy(res, old_tab, sizeof(void*) * old_size); free(old_tab); return res;
Not quite. Let's say I have an array containing 5 arrays of int. I want to expand it to 6 arrays, so I figured I have to "realloc" it. That's the part I need for my assignment, but not what I was asking about. I'm wondering if one could write a function that would work whatever the type of array of array is, int, char... C wiki page says you can't have an array of void, so I thought this was not an option, hence why I used a macro TAB_TYPE (for which ARRAY_TYPE would be a better name I guess) that the user defines and that lets the function knows what type of data it's working with. Dunno if that's clear...
Perhaps you could use structs instead of arrays?
This might (or might not) help you. I don't know, I don't remember, was a long time ago when I've written it (or anything else in C) and what I'm showing here might even be an older (non-working) version of it (but who knows? Maybe you'll get lucky):
#include <stdio.h> #include <stdlib.h> #include <time.h>
struct DiceConfiguration { int DiceNumber; int DiceType; };
struct DiceThrowsResult { int DiceNumber; int Sum; int ThrowsResults[]; };
struct DiceConfiguration* GetConfiguration(); struct DiceThrowsResult* ThrowDices(struct DiceConfiguration *config); void GenerateRandomSeed(void); int ThrowDice(struct DiceConfiguration *config); void PrintDiceThrowsResult(struct DiceThrowsResult *result); void ReleaseMemory(struct DiceConfiguration *config, struct DiceThrowsResult *result);
int main(void) { struct DiceConfiguration *config; struct DiceThrowsResult *result;
GenerateRandomSeed();
config = GetConfiguration();
while(config != NULL) { result = ThrowDices(config);
PrintDiceThrowsResult(result);
ReleaseMemory(config, result);
config = GetConfiguration(); }
return 0; }
struct DiceConfiguration* GetConfiguration() { int diceNumber = 0; int diceType = 0;
printf("Enter the number of dice to throw (0 to quit): "); scanf("%d", &diceNumber);
if(diceNumber == 0) { return NULL; } else { printf("Enter dice type (sides): "); scanf("%d", &diceType);
if(diceType < 2) { printf("Dice can't have less than 2 sides!\n");
return NULL; } else { struct DiceConfiguration *config; config = malloc(sizeof(struct DiceConfiguration));
config->DiceNumber = diceNumber; config->DiceType = diceType;
return config; } } }
struct DiceThrowsResult* ThrowDices(struct DiceConfiguration *config) { struct DiceThrowsResult *result; result = malloc(sizeof(struct DiceThrowsResult) + config->DiceNumber * sizeof(int)); result->DiceNumber = config->DiceNumber;
for (int i = 0; i < config->DiceNumber; i += 1) { result->ThrowsResults[i] = ThrowDice(config); result->Sum += result->ThrowsResults[i]; }
return result; }
void GenerateRandomSeed(void) { srand((unsigned int) time(0)); }
int ThrowDice(struct DiceConfiguration *config) { return rand() % config->DiceType + 1; }
void PrintDiceThrowsResult(struct DiceThrowsResult *result) {
for (int i = 0; i < result->DiceNumber; i += 1) { printf("%d ", result->ThrowsResults[i]); }
printf("Sum: %d\n", result->Sum); }
void ReleaseMemory(struct DiceConfiguration *config, struct DiceThrowsResult *result) { if (config != NULL) { free(config); config = NULL; }
if (result != NULL) { free(result); result = NULL; } }
|
I just learned today that C++ does not have a 'switch' for strings. And maybe even enums? That's mediocre. :/
|
Hyrule18982 Posts
C++ doesn't have strings. It has a string class.
|
On February 04 2015 03:36 darkness wrote: I just learned today that C++ does not have a 'switch' for strings. And maybe even enums? That's mediocre. :/ switch for enums works.
|
On February 04 2015 03:36 tofucake wrote: C++ doesn't have strings. It has a string class.
So yes, that's still a string. I don't care if it was even required to do silly stuff like:
switch (string.c_str())
The point is it doesn't work, and C#/Java both have it.
|
On February 04 2015 03:38 darkness wrote:Show nested quote +On February 04 2015 03:36 tofucake wrote: C++ doesn't have strings. It has a string class. So yes, that's still a string. I don't care if it was even required to do silly stuff like: switch (string.c_str())
The point is it doesn't work, and C#/Java both have it.
Typically it's done based on switching on the first character then matching strings from there.
Switch statements are compiled into jump tables, which are the result of comparing a register to an immediate value, and branching if they match. The jump table is stored in the .text section next to the comparisons, and the branching executes incredibly fast as many CPUs have instructions just for this execution structure.
The way it works in C# and Java is to compile it into a lengthy if-elseif type branching statement, so instead of switch having a different meaning in the code, its just syntactic sugar. Not that syntactic sugar isn't nice...
|
|
|
|