Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3598702
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:16:30+00:00 2026-05-18T20:16:30+00:00

#include <stdio.h> #include <iostream> #include <stdlib.h> #include <string.h> using namespace std; void multiplier_matrice_vecteur (char

  • 0
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>

using namespace std;

void multiplier_matrice_vecteur (char * v0, char * A, int N)
{
    int i,j,k;
    char somme;

    for (i = 0; i < N; i++) //On fait N calculs car c'est une matrice 1xN
    {
        //On fait N additions + multiplications
        somme = 0;
        for (j = 0; j < 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", &nbr1, &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", &nbr1, &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 < N; i++)
    {
        for (int j = 0; j < 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;
}

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 :

v0 = new char(N);

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.

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 :

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

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…

I’d have to try on GCC but since I’m always using Visual C++ I want to solve the problem on this environment…

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:16:31+00:00Added an answer on May 18, 2026 at 8:16 pm

    You should use

    matrice = new char[nbr1 * nbr1];
    

    and remember to free with

    delete[] matrice;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <stdio.h> #include <iostream> #include <stdlib.h> #include <string.h> using namespace std; void multiplier_matrice_vecteur (char
/* ** talker.c -- a datagram client demo */ #include <stdio.h> #include <stdlib.h> #include
I use the following program to write to a fifo: #include <iostream> #include <fstream>
Hello Can somebody explain why second cout in func(char *p) doesn't work: #include <cstdlib>
I used the following guide (http://peterlombardo.wikidot.com/linux-daemon-in-c) and it works fine and beautifully, accept it
I'm migrating a multi threaded application from HP-UX to Solaris and so far, everything
I'm working on an application in C++ that threads and hands a bunch of
I've a program that does Block Nested loop join ( link text ). Basically
I have a linux applications which sends data over UDP protocol. It uses these
The whole error is: Error 1 error C2065: 'socklen_t' : undeclared identifier c:usersricharddocumentsvisual studio

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.