0

I am trying to do some manual paging for a small EEPROM with 8-Byte Page Write Buffer. The code I created works as I wanted in an online tool. But, when I implemented it within my programming environment, it gives me hard time particularly getting proper values based in parsed pages.

You may check yourself if you like; Click here for the code

or you can see from here as well;

#include <stdint.h>
#include <string.h>
#include <stdio.h>

int main(){
  uint8_t * buffer =  "123456789ABCDEFGHIJKLM";
  uint8_t length = strlen(buffer);
  uint8_t pagesize = 8;
  uint8_t pagecount = length / pagesize;
  
  
  if ((length % pagesize) >= 0){
    pagecount++;
  }
  
  char memRet[8];
  char *retVal = memRet;
  
  for (uint8_t p = 1; p < pagecount + 1; p++){
    printf("Page # %d => ", p); 
    retVal = GetPageParsed(buffer, pagesize, p);
    printf("GetPageParsed : %s \n", retVal);  
  }
  
  return 0;
}

uint8_t * GetPageParsed(uint8_t * buffer, uint8_t pageSize, uint8_t page){
  char MemVal[8]; // If pageSize is used to determine the size of the array dynamically, for some reason, the array filled up with extra values!
  char *RetVal = MemVal;
  uint8_t i = 0;
  
  uint8_t StartAddress = pageSize * (page - 1);
  for (int p = StartAddress; p < pageSize * page; p++){
    RetVal[i] = buffer[p];
    i++;
  } 
  return RetVal;
}

When I run it online, I have this outcome;

Page # 1 => GetPageParsed : 12345678
Page # 2 => GetPageParsed : 9ABCDEFG
Page # 3 => GetPageParsed : HIJKLM

But, when I run inside my program;

Page # 1 => 12345678\001\b\240/ Page # 2 => 9ABCDEFG\002\b\240/ Page # 3 => HIJKLM

And it is exactly the same code. But, in my program, it turns out different. Even though I dictate I need 8 bytes by allocating memory by char arrays (char memRet[8]; and char MemVal[8];)

I am pretty sure I miss something. I appreciate your valuable inputs from now.

Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81
Sener
  • 335
  • 4
  • 17
  • what is your programming environment? on linux with gcc it works fine, try to add the header libraries? – Antonin GAVREL Mar 09 '21 at 17:36
  • 1
    An array of chars is a string. A string without a termination NULL may cause undefined behaviour. – paladin Mar 09 '21 at 17:55
  • @Antonin, Thank you for your input. I am using Segger Embedded Studio Pro. It has Segger compiler and GCC and some others as well. Library headers were already in place. You may see in the online compiler. It was exactly same as in my environment. – Sener Mar 09 '21 at 19:03
  • @paladin, You are right about it. Thank you. I didn't think about that NULL terminator, again! I just added a line `RetVal[i] = '\0';` right before `return RetVal;`. And now it works as it should be. That onlineGDB fooled me one more time although it is quite handy to test out small things. – Sener Mar 09 '21 at 19:08
  • @Sener no problem, but to be honest, you should rethink your code, because there are some other logical errors too. – paladin Mar 09 '21 at 20:10
  • @paladin, what did you see particularly have logical error(s)? Would you please elaborate a bit? – Sener Mar 09 '21 at 20:54
  • @Sener You are mixing char with uint8_t, this may probably never a problem, but it might be a problem on some embedded devices. You should write your _GetPageParsed_-function before your _main_-function and you should stay to C-naming conventions. What you call a _buffer_ is actually your data. Variable _StartAddress_ is only uint8_t, you never check for an overflow. Same for _pagecount_, it isn't checked for an overflow. Using strings is not a good way, better use "normal arrays" for copying memory segments. You should do a lot more error checking. Also read about FIFO/FILO, and Overflow-bit. – paladin Mar 10 '21 at 06:47
  • @paladin, thank you for pointing my missing points, very valuable. In fact that I have already adapted some of suggestions as this was just proof of concept. However, I haven't thought about the overflows yet, very good point. Of course there must be some error handling here and there but, I like it that way up until I finalize the code so less noise when debugging.Yeah, strings are hard to deal and error prone, I will think about that as well. Thanks again. – Sener Mar 10 '21 at 12:51

0 Answers0