Giter VIP home page Giter VIP logo

Comments (4)

Renerick avatar Renerick commented on June 5, 2024

This is C code that is roughly equivalent to the code generated by V

#include <stdio.h>

typedef struct Cell Cell;
struct Cell {
	int value;
};

typedef struct Grid Grid;
struct Grid {
    Cell field[4][4];
};

void main() {
    Cell cells[4][4] = {
        { (Cell){.value = 1}, (Cell){.value = 1}, (Cell){.value = 1}, (Cell){.value = 1},  },
        { (Cell){.value = 1}, (Cell){.value = 1}, (Cell){.value = 1}, (Cell){.value = 1},  },
        { (Cell){.value = 1}, (Cell){.value = 1}, (Cell){.value = 3}, (Cell){.value = 1},  },
        { (Cell){.value = 1}, (Cell){.value = 1}, (Cell){.value = 1}, (Cell){.value = 1},  },
    };

    Grid g = {
        .field = { cells[0], cells[1], cells[2], cells[3], }
    };

    for (int i = 0; i < 4; i++) {
        printf("Pointer: %i Value: %i | ", cells[i], cells[i][0].value);
        printf("Pointer: %i Value: %i\n", g.field[i], g.field[i][0].value);
    }
}

It also shows similar problem. I believe, the way to fix it would be to generate proper recursive copying on initialization instead of shallow copy which is used right now, or to initialize the field separately with memcpy, whatever fits better with V philosophy. I'm not that familiar with inner workings of C array initialization, but it appears the "garbage" data are the actual pointers which are being interpreted as struct values. This also explains inconsistency of the behavior - depending on the size of the struct and amount of pointers, different fraction of struct values is affected.

from v.

GGRei avatar GGRei commented on June 5, 2024

For me, your analysis is good. I used your V code, which I transpiled into C and then I had fun modifying the code to do some tests ( it is simpler like your example than if the data were dynamic ):

VV_LOCAL_SYMBOL void main__main(void) {
	Array_fixed_Array_fixed_main__Cell_4_4 tile_field = {{(main__Cell){.value = 0,.fvalue = 0,.mut_value = 10,.mut_fvalue = 0,}, (main__Cell){.value = 0,.fvalue = 0,.mut_value = 10,.mut_fvalue = 0,}, (main__Cell){.value = 0,.fvalue = 0,.mut_value = 10,.mut_fvalue = 0,}, (main__Cell){.value = 0,.fvalue = 0,.mut_value = 10,.mut_fvalue = 0,}}, {(main__Cell){.value = 0,.fvalue = 0,.mut_value = 10,.mut_fvalue = 0,}, (main__Cell){.value = 0,.fvalue = 0,.mut_value = 10,.mut_fvalue = 0,}, (main__Cell){.value = 0,.fvalue = 0,.mut_value = 10,.mut_fvalue = 0,}, (main__Cell){.value = 0,.fvalue = 0,.mut_value = 10,.mut_fvalue = 0,}}, {(main__Cell){.value = 0,.fvalue = 0,.mut_value = 10,.mut_fvalue = 0,}, (main__Cell){.value = 0,.fvalue = 0,.mut_value = 10,.mut_fvalue = 0,}, (main__Cell){.value = 0,.fvalue = 0,.mut_value = 10,.mut_fvalue = 0,}, (main__Cell){.value = 0,.fvalue = 0,.mut_value = 10,.mut_fvalue = 0,}}, {(main__Cell){.value = 0,.fvalue = 0,.mut_value = 10,.mut_fvalue = 0,}, (main__Cell){.value = 0,.fvalue = 0,.mut_value = 10,.mut_fvalue = 0,}, (main__Cell){.value = 0,.fvalue = 0,.mut_value = 10,.mut_fvalue = 0,}, (main__Cell){.value = 0,.fvalue = 0,.mut_value = 10,.mut_fvalue = 0,}}};
	main__Grid b;
	memcpy(&b.cells, &tile_field, sizeof(tile_field));
	println(_SLIT("The original:"));
	println(Array_fixed_Array_fixed_main__Cell_4_4_str(tile_field));
	println(_SLIT("=========="));
	println(_SLIT("The struct:"));
	println(Array_fixed_Array_fixed_main__Cell_4_4_str(b.cells));
}

This works and solves the initial problem, but I wonder if this is the best way to solve this problem, and here's why:

memcpy is very efficient for copying data in memory from one block to another following certain rules to be respected, such as the data size, overlapping in memory etc... But if there are complex data like pointers in a structure, it seems to me that memcpy will copy the addresses of the pointers but not the pointed-to data itself? which can lead to problems in some cases.

If I am mistaken in my statements, please feel free to correct me.

For solving this problem and taking into account my previous explanation if it is correct, wouldn't it be better to use a method that ensures a deep and safe copy in case of, for example, complex structures containing pointers?

Would a loop copy allow for a safe deep copy ( with malloc if needed ), even in the case of pointers within a structure?

If someone has an explanation on this matter, I would be personally interested in knowing it. If my reasoning is wrong, please don't hesitate to correct me, it will be educational for me along the way! :)

from v.

Renerick avatar Renerick commented on June 5, 2024

memcpy is very efficient for copying data in memory from one block to another following certain rules to be respected, such as the data size, overlapping in memory etc... But if there are complex data like pointers in a structure, it seems to me that memcpy will copy the addresses of the pointers but not the pointed-to data itself? which can lead to problems in some cases.

I suggested memcpy because it seems to be generated by V when doing array assignment. I don't mind what exactly will be used, as long as it's consistent between assignment and initialization. My hunch however is to stick to memcpy for basic shallow copy and leave full deep cloning to be used explicitly when wanted by the user

from v.

Renerick avatar Renerick commented on June 5, 2024

Fixed 🚀 thanks!

from v.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.