1

I was wondering if someone could explain how I can use the data stored in a dynamically allocated memory buffer.

My aim is to read a text file, store this in said buffer (which works). Then loop through this buffer to gather and store the desired info in arrays.

The aim of my program is to take the desired info from a text file and store the information, coordinates, in respective x and y arrays.

The program worked perfectly fine with a const char array, shown below:

const char buffer[] =
"I have(60438.0,63493.0)loads of(-2100.0,63493.0) stuff "
"(87659.0,55553.0)to copy (-70048.0,-61111.0) to strings.";

I wish to move past this now.

I can see that buffer is pointing to the location in memory.. how do I use this data?

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

char *buffer;

#define buffer_begin (buffer)
#define buffer_end (buffer + sizeof(buffer))

/*global variables*/
const char a = '(';
const char b = ')';
int i = 1;
long pos = 0;
FILE* file;
long numbytes;

char xdest[sizeof(buffer)];
char ydest[sizeof(buffer)];

static char* copy_itx(const char* base, size_t len, char* out) {
    memcpy(out, base, len); // copy from 'base' of length 'last-first' to xptr
    out[len] = '\0';        // set everything after 
    return out + len + 1;
}

static char* copy_ity(const char* base, size_t len, char* out) {
    memcpy(out, base, len);
    out[len] = '\0';
    return out + len + 1;
}

/*main function*/
int main() {

    // read txt file and store in calloc buffer

    file = fopen("instance_info.txt","r"); /*open file*/

        if(file == NULL)/* quit if the file does not exist */
            return -1;

    fseek(file, 0L, SEEK_END); /* Get the number of bytes */
    numbytes = ftell(file);     

    fseek(file, 0L, SEEK_SET);  /* reset the file position indicator to 
                                the beginning of the file */

    buffer = (char*)calloc(numbytes, sizeof(char)); /* grab sufficient memory for the 
                                                       buffer to hold the text, 
                                                       calloc clears all rubbish memory values */
if(buffer == NULL){ /* memory error */
    perror("%s");
    return -1;
}

fread(buffer, sizeof(char), numbytes, file); /* copy all the text into the buffer, 
                                                reads 'numbytes' number of chars  */        
fclose(file); //close the file

printf("%s", buffer); 

i=0;
char* xptr = xdest;
char* yptr = ydest;
const char* first = NULL;
const char* last = NULL;

// GET X CORDS HERE

printf("\n");
printf("x cords:\n");

for (const char* it = buffer_begin; it != buffer_end; it++) { // for loop to iterate through buffer

    if (*it == '(') {   // if char pointer *it is '('

        first = it + 1; // set first char equal to open bracket
        last  = NULL;   // ensure last char still equal to a NULL char

    }

    if (*it == ',' && first != NULL) {  // if const char pointer *it is equal to ',' 
                                        //and first char isnt pointing to NULL

      last = it;        //set last char equal to current position
      char* next = copy_itx(first, last-first, xptr);   // call copy_itx function
      printf("%d: %s\n", i++, xptr);    // print char stored at xptr
      first = NULL;     // set first char back to NULL
      xptr = next;      // assign char pointer next to xptr

    }

}


// GET Y CORDS HERE

printf("\n");
printf("y cords:\n");
i=0;

for (const char* it = buffer_begin; it != buffer_end; it++) {
    if (*it == ',') {
        first = it + 1;
        last  = NULL;
    }
    if (*it == ')' && first != NULL) {
        last = it;
        char* next = copy_ity(first, last-first, yptr);
        printf("%d: %s\n", i++, yptr);
        first = NULL;
        yptr = next;
    }
}
return 0;
}

Any help is greatly appreciated.

Thanks

S.NewUser
  • 11
  • 3

2 Answers2

4

In the normal case, sizeof is a compile-time evaluation. It cannot be used as some sort of run-time function to get the size of allocated memory.

When you do char xdest[sizeof(buffer)]; you get a buffer the size of a char* pointer (likely 4 bytes), not a buffer the size of what you allocated with malloc.

You need to keep track of the allocated memory with a variable. If more than one buffer should have the same size, as determined in run-time, the malloc all of them.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • "When you do char xdest[sizeof(buffer)]; you get a buffer the size of a char* pointer (likely 4 bytes), not a buffer the size of what you allocated with malloc." I tried: xdest[numbytes]; ydest[numbytes]; this didnt work. I then tried to tried using malloc on xptr and yptr and this didnt work either. Am I doing something wrong? – S.NewUser Aug 21 '18 at 13:58
0

Try reorganizing the code a bit first in my opinion.

  1. Find pointer to '(' using strstr().
  2. Now look for next pointer to ')'
  3. The string in between '(' and ')' is a XY point. Parse it to fill your arrays.
  4. Copy pointer of ')' to '('

Continue 1,2,3,4 in loop till no more references to '(' in input.

If you want to use same structure then use different iteration counter

char* it = buffer;
for (int i = 0; i < numbytes ; i++) 
{
  if (it[i] == ',') 
  {
    ...
M. Owais
  • 1
  • 1