Note that there are some explanatory texts on larger screens.

plurals
  1. PONULL pointer returned by new and malloc, why?
    primarykey
    data
    text
    <pre><code>#include &lt;stdio.h&gt; #include &lt;iostream&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; using namespace std; void multiplier_matrice_vecteur (char * v0, char * A, int N) { int i,j,k; char somme; for (i = 0; i &lt; N; i++) //On fait N calculs car c'est une matrice 1xN { //On fait N additions + multiplications somme = 0; for (j = 0; j &lt; N; j++) { somme += v0[i] * A[i * N + j]; } v0[i] = somme; } } int main(void) { bool premiereLignefaite = false; //Lire le fichier FILE * graphe = fopen("graphe.txt", "r"); //Fichier de sortie FILE * resultat = fopen("resultat.txt", "w"); int nbr1, nbr2; int N; char *matrice; //pointeur vers la matrice d'adjacence //Ligne lue static char ligne[50]; while (fgets(ligne, 50, graphe) != NULL) //retourne 0 quand on a end-of-file { //La premiere ligne est différente if (premiereLignefaite == false) { //Initialiser une matrice d'adjacence NxN sscanf(ligne, "%d %d", &amp;nbr1, &amp;nbr2); N = nbr1; matrice = new char(nbr1 * nbr1); //Memoire dynamique pour la matrice dadjacence n x n memset(matrice, 0, nbr1*nbr1); premiereLignefaite = true; continue; } //On construit notre matrice d'adjacence sscanf(ligne, "%d %d", &amp;nbr1, &amp;nbr2); matrice[nbr1 * N + nbr2 ] = 1; } printf("Matrice d'adjacence %dx%d : \n", N, N); //Affichage de la matrice d'adjacence for (int i = 0; i &lt; N; i++) { for (int j = 0; j &lt; N; j++) { printf("%c ", matrice[i * N + j] + '0'); } printf("\n"); } //Application de l'algo étapes par étapes double tolerance = 0.00001; //Niveau de tolérance de la méthode char * v0; //vecteur propre taille N char * v; //vecteur tampon int valeur; //valeur propre int valeur_tamp; //valeur propre tampon //Initialiser v0 v0 = new char(N); memset(v0, 1, N); //Initialiser A (déja fait) //Initialiser tolérance (deja fait) valeur = 0; while (1) { valeur_tamp = valeur; //Multiplication du vecteur par la matrice multiplier_matrice_vecteur(v0, matrice, N); } //Désallocation de la mémoire delete matrice; //Fermeture des fichiers etc fclose(graphe); fclose(resultat); return 0; } </code></pre> <p>So this is the program I have been coding since 2PM on my laptop. I am using Visual Studio 2008 and Windows 7 64 bits. I'm coding, everything's going fine. When I try to test my program, the line :</p> <pre><code>v0 = new char(N); </code></pre> <p>Gives me an exception. I try getting my memory with malloc and calloc and what do I get? A null pointer!! I have 4 gigs of ram on my machine and there's no way I can't get 9 bytes of memory here. I don't understand this at all. </p> <p>For those who have Visual Studio 2008 and want to test under the same environnement, you will need the file graphe.txt, here's that file :</p> <pre><code>9 20 0 1 0 2 1 0 1 2 1 3 1 5 2 0 2 1 2 3 3 1 3 2 3 4 4 3 5 1 5 6 5 7 6 5 6 8 7 5 8 6 </code></pre> <p>I thought that this was a machine problem, So I come back home, I try the program on my desktop computer and it's the same problem...</p> <p>I'd have to try on GCC but since I'm always using Visual C++ I want to solve the problem on this environment...</p> <p>EDIT: The following code now works. For some reason, if you use parentheses in the first new, it will work, but the next time you use new, it won't work!! If I put both new allocations with [] syntax, it works. If I put the first new allocation with [] and the second one with (), it also works. WEIRD. Anyways, i'll be using [] from now on... Thank you all.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload