Search Results

Search found 13 results on 1 pages for 'm4design'.

Page 1/1 | 1 

  • Simple dynamic memory allocation bug.

    - by M4design
    I'm sure you (pros) can identify the bug's' in my code, I also would appreciate any other comments on my code. BTW, the code crashes after I run it. #include <stdlib.h> #include <stdio.h> #include <stdbool.h> typedef struct { int x; int y; } Location; typedef struct { bool walkable; unsigned char walked; // number of times walked upon } Cell; typedef struct { char name[40]; // Name of maze Cell **grid; // 2D array of cells int rows; // Number of rows int cols; // Number of columns Location entrance; } Maze; Maze *maz_new() { int i = 0; Maze *mazPtr = (Maze *)malloc(sizeof (Maze)); if(!mazPtr) { puts("The memory couldn't be initilised, Press ENTER to exit"); getchar(); exit(-1); } else { // allocating memory for the grid mazPtr->grid = (Cell **) malloc((sizeof (Cell)) * (mazPtr->rows)); for(i = 0; i < mazPtr->rows; i++) mazPtr->grid[i] = (Cell *) malloc((sizeof (Cell)) * (mazPtr->cols)); } return mazPtr; } void maz_delete(Maze *maz) { int i = 0; if (maz != NULL) { for(i = 0; i < maz->rows; i++) free(maz->grid[i]); free(maz->grid); } } int main() { Maze *ptr = maz_new(); maz_delete(ptr); getchar(); return 0; } Thanks in advance.

    Read the article

  • Is it a good idea to work on header files only, just at the start of the project?

    - by m4design
    To explain my point further, I'm a beginner in programming, and I'm working on a small project. Instead of separating the .cpp file from the header file, I'm implementing the code in the header files, and making one .cpp file for testing. I do this to have less files, hence easier navigation. Then later I'll separate the code as it should be. Will this cause any problems? should I continue doing that? Thanks.

    Read the article

  • Another dynamic memory allocation bug.

    - by m4design
    I'm trying to allocate memory for a multidimensional array (8 rows, 3 columns). Here's the code for the allocation (I'm sure the error is clear for you) char **ptr = (char **) malloc( sizeof(char) * 8); for (i = 0; i < 3; i++) ptr[i] = (char *) malloc( sizeof(char) * 3); The crash happens when I reference this: ptr[3][0]; Unhandled exception at 0x0135144d in xxxx.exe: 0xC0000005: Access violation writing location 0xabababab. Are there any recommended references/readings for this kind of subject? Thanks.

    Read the article

  • What could possibly cause this error when declaring an object inside a class?

    - by M4design
    I'm battling with this assignment :) I've got two classes: Ocean and Grid. When I declare an object of the Grid inside the Ocean: unsigned int sharkCount; Grid grid; The compiler/complainer says: error C2146: syntax error : missing ';' before identifier 'grid' Can you possibly predict what produces this error with the limited info I provided? It seems that as if the Ocean doesn't like the Grid class. Could this be because of the poor implementation of the grid class. BTW the Grid has a default constructor. Yet the error happens in compiling time!. EDIT: They're each in separate header file, and I've included the Grid.h in the Ocean.h.

    Read the article

  • What could possibly cause this error(when declaring an object inside a class) ? //noobie question

    - by M4design
    I'm battling with this assignment :) I've got two classes: Ocean and Grid. When I declare an object of the Grid inside the Ocean: unsigned int sharkCount; Grid grid; The compiler/complainer says: error C2146: syntax error : missing ';' before identifier 'grid' Can you possibly predict what produces this error with the limited info' I provided? It seems that as if the Ocean doesn't like the Grid class. Could this be because of the poor implementation of the grid class. BTW the Grid has a default constructor. Yet the error happens in compiling time!. Thanks. EDIT: They're each in separate header file, and I've included the Grid.h in the Ocean.h.

    Read the article

  • Am I doing something wrong here (references in C++)?

    - by m4design
    I've been playing around with references (I'm still having issues in this regard). 1- I would like to know if this is an acceptable code: int & foo(int &y) { return y; // is this wrong? } int main() { int x = 0; cout << foo(x) << endl; foo(x) = 9; // is this wrong? cout << x << endl; return 0; } 2- Also this is from an exam sample: Week & Week::highestSalesWeek(Week aYear[52]) { Week max = aYear[0]; for(int i = 1; i < 52; i++) { if (aYear[i].getSales() > max.getSales()) max = aYear[i]; } return max; } It asks about the mistake in this code, also how to fix it. My guess is that it return a local reference. The fix is: Week & max = aYear[0]; Is this correct/enough?

    Read the article

  • How can a FILE* (pointer to a struct) be tested (if == NULL)?

    - by m4design
    I was playing around with C, anyways I was thinking how can file pointer (which points to a struct type), be tested if NULL as for instant: FILE *cfPtr; if ( ( cfPtr = fopen( "file.dat", "w" ) ) == NULL ) I tried to do that myself, but an error occurs. struct foo{ int x; }; struct foo bar = {0}; if (bar == NULL) puts("Yay\n"); else puts("Nay"); error C2088: '==' : illegal for struct Here's the FILE deceleration in the stdio.h file: struct _iobuf { char *_ptr; int _cnt; char *_base; int _flag; int _file; int _charbuf; int _bufsiz; char *_tmpfname; }; typedef struct _iobuf FILE;

    Read the article

  • A few questions about char pointers.

    - by m4design
    1- How does this work: char *ptr = "hi"; Now the compiler will put this string in the memory (I'm guessing the stack), and create a pointer to it? Is this is how it works? 2- Also if it is created locally in a function, when the function returns will the memory occupied by the string be freed? 3- Last but not least, why is this not allowed: ptr[0] = 'H'; ?

    Read the article

  • Simple OOP-related question.

    - by M4design
    This question came to my mind quite a few times. Let my explain my question through an example. Say I've got two classes: 1- Grid. 2- Cell. Now the location of the cell 'should' be stored in the grid class, not in the cell class itself. Say that the cell wanted to get its location through a method in the grid. How can it do that? Keep in mind that the cell was created/initialised by the Grid class. What good OO approach to solve this problem? Thank you

    Read the article

  • Is this a correct syntax (c code found on wikipedia)?

    - by m4design
    I just found this code on wikipedia. Link: http://en.wikipedia.org/wiki/Sizeof#Use The code: /* the following code illustrates the use of sizeof * with variables and expressions (no parentheses needed), * and with type names (parentheses needed) */ char c; printf("%zu,%zu", sizeof c, sizeof(int)); It states that: "The z prefix should be used to print it, because the actual size can differ on each architecture." I tried it on my compiler, but it gives the following result: 'zu,zu'

    Read the article

1