Comments (4)
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.
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.
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.
Fixed 🚀 thanks!
from v.
Related Issues (20)
- [x.json2] Add skippable struct fields @[json: '-'] HOT 1
- Confusing error message when using `is` with a constant rvalue HOT 4
- Better error message for memory allocation in vplayground HOT 1
- vlib: http.Server: different behavior between prod and non-prod for *invalid* Response object
- Builder error: bad map usage
- Different markdown rendering for enumerated list in module documentation HOT 2
- toml.parse_file does not panic when the file is missing HOT 2
- Will vweb have built-in Api document generation tools HOT 2
- Optional type matches incorrectly in $for field
- error: invalid operand types for binary operation, error: invalid aggregate type for register load HOT 2
- documentation generator seems to be off
- autofree: memory leak HOT 1
- Buffered Reader read_line() segfault when compiled with -prod HOT 4
- Strange error message with `@[flag]` enum
- `@[flag]` enums should support all 4 bitwise operators, instead of only 2
- add .args to compile time method HOT 4
- fmt: `i++ $if abc ? {` gets formatted, as `i++$ if abc ? {`, and fails to parse.
- typed fns do not match correctly in compile time code
- libpthread linked always HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from v.