u/vorsamp - 16 Archived Voat Posts in v/programming
u/vorsamp
  • home
  • search

u/vorsamp

1 post · 15 comments · 16 total

Active in: v/programming (16)

  • ‹‹‹
  • ‹‹
  • ‹
  • 1
  • ›
  • ››
  • ›››
Comment on: [Why does my code work] I have created a parser in C and if I do not allocate and immediately free an array it breaks.

I have removed all uses of pointer allocation, but I am still interested in how to solve this. I could reject the size value, but maybe I would want to chain together some memory. That would prevent the loss of data, but would still leave me open to take all of my memory up in a single request, could I stop allocation at some predetermined threshold? No allocation can ask for more than 10% of the ram and at 90% capacity any allocation will fail, something like that.

1 16 Jun 2016 19:48 u/vorsamp in v/programming
Comment on: [Why does my code work] I have created a parser in C and if I do not allocate and immediately free an array it breaks.

I have changed all my arrays to be static. I have also followed your suggestion and made a basic atoi. Thank you again for your advice.

/* reads integer in the range inputArray[start, end) */
int inputRangeToInteger(int start, int end){
  int value = 0;
  for(int i=start; i<end; i++){
    value = value + (inputArray[i]-48)*pow(10, end-i-1); 
  } 
  return value; 
} 
1 16 Jun 2016 16:46 u/vorsamp in v/programming
Comment on: [Why does my code work] I have created a parser in C and if I do not allocate and immediately free an array it breaks.

So I have made everything a static array and it works brilliantly. I have also rewritten atoi, without changing how my parser behaves, it still tracks start and length of the values between unit seperators. I am sure the following code is trivial, but I just wanted to share how I am parsing my integers.

/* reads integer in the range inputArray[start, end) */
int inputRangeToInteger(int start, int end){
  int value = 0;
  for(int i=start; i<end; i++){
    value = value + (inputArray[i]-48)*pow(10, end-i-1); 
  } 
  return value; 
} 
1 16 Jun 2016 16:43 u/vorsamp in v/programming
Comment on: [Why does my code work] I have created a parser in C and if I do not allocate and immediately free an array it breaks.

I am confused, I am not multiplying anything.

0 16 Jun 2016 04:08 u/vorsamp in v/programming
Comment on: [Why does my code work] I have created a parser in C and if I do not allocate and immediately free an array it breaks.

This is very valuable and I will be sure to return and update you on how I've implemented parts of your solution.

Juuuuuuuust for fun, I don't have any type definitions in my C so there is no such thing as bool.

0 16 Jun 2016 03:58 u/vorsamp in v/programming
Comment on: [Why does my code work] I have created a parser in C and if I do not allocate and immediately free an array it breaks.

I did not think atoi assumed a char pointer ended with a null terminating character, \0. I think your suggestion which reads until a US will be effective. Thank you.

1 16 Jun 2016 03:48 u/vorsamp in v/programming
Comment on: [Why does my code work] I have created a parser in C and if I do not allocate and immediately free an array it breaks.

I have seen that calloc will automatically initialize my values to 0, I will read more into the security of this method of memory allocation.

Regarding the last part of your comment, u/tame also suggested I use arrays instead of pointers. I have been considering a refactor where I can minimize/eliminate usage of pointer allocation.

Regarding the newest part of your your comment, why am I calling atoi when my char pointer is most certainly not null terminated, has two reasons. 1) it works for any single digit string. 2) in the many pages of documentation on atoi, none of them have referenced the necessity for a null terminated string. I fucked up, but until you mentioned it, this information was not readily available. Thank you.

1 16 Jun 2016 03:30 u/vorsamp in v/programming
Comment on: [Why does my code work] I have created a parser in C and if I do not allocate and immediately free an array it breaks.

This comment is useful, thank you for telling me I am an idiot.

1 16 Jun 2016 02:36 u/vorsamp in v/programming
Comment on: [Why does my code work] I have created a parser in C and if I do not allocate and immediately free an array it breaks.

You have given very honest feedback. Do you think a switch statement would make my code more readable?

2 16 Jun 2016 02:32 u/vorsamp in v/programming
Comment on: [Why does my code work] I have created a parser in C and if I do not allocate and immediately free an array it breaks.

Yes, inputBufferLength starts at 0. Given valid input, which I have indicated you can assume, at least one integer appears before c==US, so inputBufferLenght++ is called at least once, so you can assume inputBufferLength is at least 1 when this section of the switch statement is called.

1 16 Jun 2016 02:22 u/vorsamp in v/programming
Comment on: [Why does my code work] I have created a parser in C and if I do not allocate and immediately free an array it breaks.

This comment is useless, but as per my post, thank you for telling me I am an idiot.

1 16 Jun 2016 02:19 u/vorsamp in v/programming
Comment on: [Why does my code work] I have created a parser in C and if I do not allocate and immediately free an array it breaks.

Thank you so much for your reply.

change allocCharBuffer() so that it allocates (numberOfChars+1) bytes

I have read about allocating strings, which in C is a character array, and allowing for \0 to take up the end, this did not help me, but I will double check that tomorrow.

check memory allocation/deallocation (if you're using it, which on a microcontroller you probably don't want to)

I was considering a refactor that would use arrays instead of pointers, this piece of your comment seems to affirm that. Can I get a little more information about this suggestion?

2 16 Jun 2016 02:16 u/vorsamp in v/programming
Comment on: [Why does my code work] I have created a parser in C and if I do not allocate and immediately free an array it breaks.

I have updated my post to indicate the assumption about input. There will always be an integer before a unit separator (US) is encountered. In C#, int and uint cannot be null.

my code that makes the request

1 16 Jun 2016 02:06 u/vorsamp in v/programming
Comment on: [Why does my code work] I have created a parser in C and if I do not allocate and immediately free an array it breaks.

For the purpose of my problem, you can assume input is always valid, there is always at least one integer before c==US.

2 16 Jun 2016 01:49 u/vorsamp in v/programming
[Why does my code work] I have created a parser in C and if I do not allocate and immediately free an array it breaks.
9 36 comments 16 Jun 2016 01:16 u/vorsamp (self.programming) in v/programming
Comment on: Excuses For Lazy Coders

"Works for me!"

0 07 Sep 2015 08:25 u/vorsamp in v/programming
  • ‹‹‹
  • ‹‹
  • ‹
  • 1
  • ›
  • ››
  • ›››

archive has 9,592 posts and 65,719 comments. source code.